SpringBoot(十):SpringBoot整合Memcached
一、环境准备
memcached 1.4.5
SpringBoot 1.5.10.RELEASE
java_memcached-release_2.6.6.jar
memcached 1.4.5 windows 下载地址:http://www.runoob.com/memcached/window-install-memcached.html
danga memcached java client 下载地址:https://github.com/gwhalin/Memcached-Java-Client/downloads
pom依赖:
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6</version>
</dependency>
<dependency>
<groupId>com.danga</groupId>
<artifactId>java_memcached-release</artifactId>
<version>2.6.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/java_memcached-release_2.6.6.jar</systemPath>
</dependency>
二、项目结构

三、代码详情
application.yml :
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.20:3306/test?useSSL=false
username: root
password: root123
rabbitmq:
host: 192.168.1.123
port: 5672
username: admin
password: 1234
# virtual-host: /vhost_test
# publisher-confirms: true
## Memcache 配置 ##
memcache:
servers: 127.0.0.1:11211
failover: true
initConn: 100
minConn: 20
maxConn: 1000
maintSleep: 50
nagel: false
socketTO: 3000
aliveCheck: true
logging.level.com.demo.mapper: debug
MemcacheConfiguration.java
import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* @author zh
* @ClassName cn.saytime.config.config.MemcacheConfiguration
* @Description Memcache配置
*/
@Configuration
public class MemcacheConfiguration { @Value("${memcache.servers}")
private String[] servers;
@Value("${memcache.failover}")
private boolean failover;
@Value("${memcache.initConn}")
private int initConn;
@Value("${memcache.minConn}")
private int minConn;
@Value("${memcache.maxConn}")
private int maxConn;
@Value("${memcache.maintSleep}")
private int maintSleep;
@Value("${memcache.nagel}")
private boolean nagel;
@Value("${memcache.socketTO}")
private int socketTO;
@Value("${memcache.aliveCheck}")
private boolean aliveCheck; @Bean
public SockIOPool sockIOPool () {
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setFailover(failover);
pool.setInitConn(initConn);
pool.setMinConn(minConn);
pool.setMaxConn(maxConn);
pool.setMaintSleep(maintSleep);
pool.setNagle(nagel);
pool.setSocketTO(socketTO);
pool.setAliveCheck(aliveCheck);
pool.initialize();
return pool;
} @Bean
public MemCachedClient memCachedClient(){
return new MemCachedClient();
} }
测试类 SpringbootMemcacheApplicationTests.java
import com.danga.MemCached.MemCachedClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.Date; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMemcacheApplicationTests { @Autowired
private MemCachedClient memCachedClient; @Test
public void contextLoads() throws InterruptedException {
// 放入缓存
boolean flag = memCachedClient.set("a", 1); // 取出缓存
Object a = memCachedClient.get("a");
System.out.println(a); // 3s后过期
memCachedClient.set("b", "2", new Date(3000));
Object b = memCachedClient.get("b");
System.out.println(b); Thread.sleep(3000);
b = memCachedClient.get("b");
System.out.println(b); } }
先运行memcached,然后执行test,输出结果为:
1
2
null
测试memcached存取以及失效时间成功。
四、部分操作方法
set与add在key不存在时效果一致,add在key存在时不会成功。
set与replace在key存在时效果一致,replace在key不存在不会成功。
五、注意点
使用danga memcached设置失效时间有两种方式:
第一种
memCachedClient.set("xx", "xx", new Date(3000));
第二种
memCachedClient.set("xx", "xx", new Date(System.currentTimeMillis() + 3 * 1000));
对比两种形式,第一种是指定key在3s后过期,第二种是指定key在xxxx-xx-xx xx:xx:xx 时间点失效,如果服务器时间跟客户端时间不一致,就会跟想要的结果不一样,比如客户端现在时间点为2018-01-01 00:00:00,服务端时间为2018-01-01 00:00:10,服务端时间快10s,那么如果客户端使用第二种方式设置30s后失效,也就是2018-01-01 00:00:30失效,实际上客户端想要的是30s后失效,而服务端20s就将key失效了。
从上可以发现,最好是使用第一种形式,但是第一种形式在某些时间也会存在问题,比如如果设定的时间小于1s,会发现key会永久保存,不会在指定时间失效,原因可以通过源码得到。
说明当时间小于1s的时候,使用第一种方式会造成指定时间不生效,key永久存在,这种时间如果客户端服务端时间没有误差的时候,使用第二种形式。
关于memcached最大设置30天有效的情形暂时没有测试。
SpringBoot(十):SpringBoot整合Memcached的更多相关文章
- SpringBoot(十):SpringBoot的简单事务管理
SpringBoot集成Mybatis之后,进行事务管理.SpringBoot使用事务非常简单,底层依然采用的是Spring本身提供的事务. 1.在入口类中使用注解@EnableTransaction ...
- springboot(十)SpringBoot消息中间件RabbitMQ
github地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1. ...
- SpringBoot进阶教程(二十九)整合Redis 发布订阅
SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...
- SpringBoot进阶教程(二十八)整合Redis事物
Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...
- SpringBoot进阶教程(二十六)整合Redis之共享Session
集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...
- SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用
在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...
- SpringBoot进阶教程(五十九)整合Codis
上一篇博文<详解Codis安装与部署>中,详细介绍了codis的安装与部署,这篇文章主要介绍介绍springboot整合codis.如果之前看过<SpringBoot进阶教程(五十二 ...
- springboot(十四):springboot整合shiro-登录认证和权限管理(转)
springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...
- Activiti7整合SpringBoot(十二)
1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...
- spring-boot(八) springboot整合shiro-登录认证和权限管理
学习文章:springboot(十四):springboot整合shiro-登录认证和权限管理 Apache Shiro What is Apache Shiro? Apache Shiro是一个功能 ...
随机推荐
- c++ LeetCode (网易面试题和链表以及树篇) 五道算法例题代码详解(三)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11209807.html 一.1道网易c++的面试题 我当时第一时间的解答方案 #include ...
- SSM整合1(springMVC+mybatis)
整合目标:控制层采用springmvc.持久层使用mybatis实现 整合思路: Dao层: 1.SqlMapConfig.xml,空文件即可,但是需要文件头. 2.applicationContex ...
- Android App图片资源文件压缩利器McImage
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/195 Android App图片资源文件压缩利器McIma ...
- Django 资源 与 知识 Django中自建脚本并使用Django环境 model中的save()方法说明 filter()用法
Django 资源 与 知识 Django中自建脚本并使用Django环境 model中的save()方法说明 filter()用法 2018/11/06 Chenxin 资料说明 Django基础入 ...
- Violet音乐社区界面原型手册
目录 Violet音乐社区界面原型手册 一.引言 1.0 项目前阶段相关文档 1.1 编写目的 1.2 开发背景 二.界面原型展示 2.0 界面设计说明 2.1 首页 2.2 歌单/专辑/单曲界面 2 ...
- [内核同步]自旋锁spin_lock、spin_lock_irq 和 spin_lock_irqsave 分析【转】
转自:https://www.cnblogs.com/x_wukong/p/8573602.html 转自;https://www.cnblogs.com/aaronLinux/p/5890924.h ...
- Shadow Map -- 点阴影(全方位)
昨晚终于把点阴影(深度CubeMap)程序调通了,思想不难,基本就是在上节定向光阴影基础上稍作修改,但是CG程序不太方便Debug,需要输出中间效果图进行判断,耽搁了一会儿. 过程如下: 1.将深度渲 ...
- 201871010102-常龙龙《面向对象程序设计(java)》第一周学习总结
博文正文开头:(3分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/p/11435127.html 这个作业的要求在哪里 https://e ...
- openjdk源码下载
http://hg.openjdk.java.net/jdk8u/jdk8u60/jdk/file/935758609767 browse>zip
- day(66)作业
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...