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. ...
随机推荐
- tomcat servlet JSP common gateway interface 公共网关接口
Tomcat主要充当servlet/JSP容器,不过它却有大量的功能可以与传统的Web服务器相媲美,对公共网关接口(Common Gateway Interface)的支持就是其中之一. 传统的Web ...
- ORA-00257
删除归档日志文件的方法: http://www.blogjava.net/kuuyee/archive/2013/05/15/399287.html 惜分飞大大的博客:http://www.xifen ...
- The Little Match Girl,摘自iOS应用Snow White and more stories
Many years ago on a cold and snowy New Year's Eve, a poor little girl was wandering arround on the s ...
- TTimer源码研究
TTimerProc = procedure of object; IFMXTimerService = interface(IInterface) ['{856E938B-FF7B-4E13-85D ...
- Android Handle,Looper,Message消息机制
尊重原创,转载请标明出处 http://blog.csdn.net/abcdef314159 我们知道在Android中更新UI都是在主线程中,而操作一些耗时的任务则须要在子线程中.假设存在多个 ...
- Algorithm: Euler function
欧拉函数. phi(n)表示比n小的与n互质的数的个数,比如 phi(1) = 1; phi(2) = 1; phi(3) = 2; phi(4) = 2; phi(5) = 4; 性质: 1. 如果 ...
- 后台管理微服务(二)——docker的使用
1. docker概述 1.1 Docker是什么 Docker 是软件工业的集装箱技术 Docker 是一个容器引擎,docker提供了一套完整的容器解决方案. Docker 是一个能将开发的程序自 ...
- Ace(二)Demo示例
Client: #include "ace/Log_Msg.h" #include "ace/OS.h" #include "ace/Service_ ...
- NOIP2010_T4_引水入城 bfs+贪心
在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行 M 列的矩形,如上图所示,其中每个格子都代表一座城 市,每座城市都有一个海拔高度.为了使 ...
- linux /usr /var /etc 目录
/usr 目录是应用程序主要存放的目录.该目录中的二进制文件对系统启动和维护并非必要,因此整个 /usr 目录结构常会被存放到另一个分离的文件系统中.因为其(通常)具有很大的容量,/usr 有其自己的 ...