SpringBoot整合Memached
一、Memached介绍
Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载。它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态、数据库驱动网站的速度。Memcached基于一个存储键/值对的hashmap。其守护进程(daemon )是用C写的,但是客户端可以用任何语言来编写,并通过memcached协议与守护进程通信。
为了提高性能,memcached中保存的数据都存储在memcached内置的内存存储空间中。由于数据仅存在于内存中,因此重启memcached、重启操作系统会导致全部数据消失。另外,内容容量达到指定值之后,就基于LRU(Least Recently Used)算法自动删除不使用的缓存。memcached本身是为缓存而设计的服务器,因此并没有过多考虑数据的永久性问题。
二、安装步骤
windows安装
参考教材https://www.runoob.com/memcached/window-install-memcached.html
linux(Ubuntu/Debian)安装
要先安装libevent库
sudo apt-get install libevent ibevent-dev自动安装
sudo apt-get install memcached或者源代码的方式安装
从其官方网站(http://memcached.org)下载memcached
解压源码 tar -zxvf memcached-1.x.x.tar.gz
进入目录
配置 ./configure --prefix=/path/to/memcached
编译 make && make test
安装 sudo make install
更多参考https://www.runoob.com/memcached/memcached-install.html
安装完成后memcached已经启动了,我们先杀掉进程然后按照自己的方式作为后台服务启动。
/usr/bin/memcached -p 11211 -m 64m -d -u root
最后打开cmd,输入 telnet ip 11211来连接memcached进行测试。
注意: 我的自动安装后是在/usr/bin目录,实际情况要根据进程信息显示的目录为准。
- set foo 0 0 3
- bar
- STORED
- get foo
- VALUE foo 0 3
- bar
- END
- quit
三、完成整合
- 创建springboot项目memcached-demo
- 在 pomx 包中添加 spymemcached 的引用,Spymemcached 是一个采用 Java 开发的异步、单线程的 Memcached 客户端, 使用 NIO 实现。
- <dependency>
- <groupId>net.spy</groupId>
- <artifactId>spymemcached</artifactId>
- <version>2.12.2</version>
- </dependency>
- 在application.properties文件填写配置信息
memcached.ip=XXX
memcached.port=11211 - 对应的配置读取类MemcachedConfig
@Component
@ConfigurationProperties(prefix = "memcached")
public class MemcachedConfig {
private String ip;
private int port;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
- 项目启动时创建MemcachedClient实例
@SpringBootApplication
public class MemcachedDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MemcachedDemoApplication.class, args);
}
final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private MemcachedConfig memcachedConfig;
@Bean(name = "memcachedClient")
public MemcachedClient getMemcachedClient(){
try {
MemcachedClient client = new MemcachedClient(new InetSocketAddress(memcachedConfig.getIp(), memcachedConfig.getPort()));
return client;
} catch (IOException e) {
logger.error("初始化MemcachedClient失败,{}",e.getMessage());
e.printStackTrace();
}
return null;
}
}
- 编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class MemcachedDemoApplicationTests {
@Autowired
private MemcachedClient client;
@Test
public void contextLoads() {
client.set("testKey",1000,"testValue");
System.out.println("============testKey的值为:"+client.get("testKey"));
}
}
四、测试总结
运行测试类MemcachedDemoApplicationTests中的contextLoads方法,控制台输出如下:
2019-05-06 19:52:26.693 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=/198.13.40.234:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2019-05-06 19:52:27.312 INFO 10348 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-05-06 19:52:27.668 INFO 10348 --- [ main] cn.sp.MemcachedDemoApplicationTests : Started MemcachedDemoApplicationTests in 2.966 seconds (JVM running for 4.14)
============testKey的值为:testValue
2019-05-06 19:52:28.052 INFO 10348 --- [ Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2019-05-06 19:52:28.059 INFO net.spy.memcached.MemcachedConnection: Shut down memcached client
Memcached上手起来还是比较简单,但是还有些其他命令需要熟悉,以及多台Memcached 的使用问题。
代码地址,点击这里。
SpringBoot整合Memached的更多相关文章
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
随机推荐
- File syncing and sharing software with file encryption and group sharing, emphasis on reliability and high performance.
http://seafile.com/ showdoc haiwen/seafile: File syncing and sharing software with file encryption a ...
- access 连接数据库
前提条件 如果没有安装office的话,需要安装引擎 安装了office就不用安装引擎 连接数据库 Dim plMydb As Microsoft.Office.Interop.Access.Dao. ...
- 前后端通吃的单元测试---mocha
git clone https://github.com/shenggen1987/mocha-demo.git npm install front 前台测试代码 backend 后台测试代码 先安装 ...
- [Spring Batch 系列] 第一节 初识 Spring Batch
距离开始使用 Spring Batch 有一段时间了,一直没有时间整理,现在项目即将完结,整理下这段时间学习和使用经历. 官网地址:http://projects.spring.io/spring-b ...
- 关于<context:annotation-config/>配置
对于spring项目的一些配置,一直感到有些混乱,今天看到一前辈总结的特别好,把自己的理解贴在这里,有不当的地方,后续继续学习: 当我们使用@Autowired.@Required等这些注解时,就要在 ...
- Codeforces Round #383 (Div. 2) C. Arpa's loud Owf and Mehrdad's evil plan —— DFS找环
题目链接:http://codeforces.com/contest/742/problem/C C. Arpa's loud Owf and Mehrdad's evil plan time lim ...
- codeforces A. Nuts 解题报告
题目链接:http://codeforces.com/problemset/problem/402/A 题目意思:几经辛苦,终于体明题目噶意思了 = =,完全是考验一个人是否清醒的最简便方法- -! ...
- 自己实现c++中string 类
class String { public: String(const char *str = NULL);// 普通构造函数 String(const String &other);// 拷 ...
- Java并发实现线程阻塞原语LockSupport
LockSupport 和 CAS 是Java并发包中很多并发工具控制机制的基础,它们底层其实都是依赖Unsafe实现.LockSupport是用来创建锁和其他同步类的基本线程阻塞原语. 1.Lock ...
- 洛谷P4719 动态DP —— 动态DP(树剖+矩乘)
题目:https://www.luogu.org/problemnew/show/P4719 感觉这篇博客写得挺好:https://blog.csdn.net/litble/article/detai ...