一、环境准备
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的更多相关文章

  1. SpringBoot(十):SpringBoot的简单事务管理

    SpringBoot集成Mybatis之后,进行事务管理.SpringBoot使用事务非常简单,底层依然采用的是Spring本身提供的事务. 1.在入口类中使用注解@EnableTransaction ...

  2. springboot(十)SpringBoot消息中间件RabbitMQ

    github地址:https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service 1. ...

  3. SpringBoot进阶教程(二十九)整合Redis 发布订阅

    SUBSCRIBE, UNSUBSCRIBE 和 PUBLISH 实现了 发布/订阅消息范例,发送者 (publishers) 不用编程就可以向特定的接受者发送消息 (subscribers). Ra ...

  4. SpringBoot进阶教程(二十八)整合Redis事物

    Redis默认情况下,事务支持被禁用,必须通过设置setEnableTransactionSupport(true)为使用中的每个redistplate显式启用.这样做会强制将当前重新连接绑定到触发m ...

  5. SpringBoot进阶教程(二十六)整合Redis之共享Session

    集群现在越来越常见,当我们项目搭建了集群,就会产生session共享问题.因为session是保存在服务器上面的.那么解决这一问题,大致有三个方案,1.通过nginx的负载均衡其中一种ip绑定来实现( ...

  6. SpringBoot进阶教程(二十五)整合Redis之@Cacheable、@CachePut、@CacheEvict的应用

    在上一篇文章(<SpringBoot(二十四)整合Redis>)中,已经实现了Spring Boot对Redis的整合,既然已经讲到Cache了,今天就介绍介绍缓存注解.各家互联网产品现在 ...

  7. SpringBoot进阶教程(五十九)整合Codis

    上一篇博文<详解Codis安装与部署>中,详细介绍了codis的安装与部署,这篇文章主要介绍介绍springboot整合codis.如果之前看过<SpringBoot进阶教程(五十二 ...

  8. springboot(十四):springboot整合shiro-登录认证和权限管理(转)

    springboot(十四):springboot整合shiro-登录认证和权限管理 .embody{ padding:10px 10px 10px; margin:0 -20px; border-b ...

  9. Activiti7整合SpringBoot(十二)

    1 SpringBoot 整合 Activiti7 的配置 为了能够实现 SpringBoot 与 Activiti7 整合开发,首先我们要引入相关的依赖支持.所以,我们在工程的 pom.xml 文件 ...

  10. spring-boot(八) springboot整合shiro-登录认证和权限管理

    学习文章:springboot(十四):springboot整合shiro-登录认证和权限管理 Apache Shiro What is Apache Shiro? Apache Shiro是一个功能 ...

随机推荐

  1. 动态SQL与变量绑定

    有时候动态sql需要进行变量的赋值,这个时候就需要调用系统的存储过程sp_executesql了.使用中还是有些注意事项,代码如下: --字符型字段需声明为NVARCHAR类型 ),) --动态SQL ...

  2. Python中7个不一样的代码写法

    打印index 对于一个列表,或者说一个序列我们经常需要打印它的index,一般传统的做法或者说比较low的写法: 更优雅的写法是多用enumerate 两个序列的循环 我们会经常对两个序列进行计算或 ...

  3. js获取时间,循环执行任务,延迟执行任务

    一.获取时间 核心方法创建一个时间对象:new Date() 时间对象相关操作 时间对象.函数名 函数名 功能 getYear() 获取四位数的年份 getMonth() 获取2位数的月数, 这个是从 ...

  4. Netty服务端Channel注册Selector及绑定服务器端口

    当服务端Channel 创建并且初始化完成之后,会将其注册到 selector,通过语句config().group().register(channel)进行注册工作,该方法最终调用 Abstrac ...

  5. cmdb项目-1

    1.什么是cmdb 配置管理数据库 ,存储基础设备的各种信息配置等 CMDB可以存储并自动发现整个IT网络上的各种信息,比如一个IT网络上有多少台服务器.多少存储.设备的品牌.资产编号.维护人员.所属 ...

  6. [browser navigator],写了个检测游览器版本

    前些天胃不舒服打吊针了,真得准时吃饭各种啊,然后就是懒,就没在复习了,这次复习的内容是navigator //未知效果 // console.log('浏览器的次要版本' + navigator.ap ...

  7. vscode中js文件失去高亮/没有智能提示

    vscode中js文件失去高亮/没有智能提示 两步: 第一步:基本的语法高亮提示,需要将vetur删掉,然后把vscode的历史记录缓存删掉,重启vscode. 第二步:js的智能提示,使用插件typ ...

  8. React-router使用

    介绍 react-router被分为以下几部分: react-router是浏览器和原生应用中的通用部分. react-router-dom是用于浏览器的. react-router-native是用 ...

  9. LeetCode 5127. 删除被覆盖区间 Remove Covered Intervals

    地址 https://www.acwing.com/solution/LeetCode/content/7021/ 目描述给你一个区间列表,请你删除列表中被其他区间所覆盖的区间. 只有当 c < ...

  10. Go 字节 (byte) & 文字符号 (rune)

    byte 通过 byte 定义一个字节,字节必须使用单引号包起来,直接打印字节输出的是 ascii 码,需要通过格式化输出 byte 是 uint8 的别称,使用 byte 主要是为了区分字节和无符号 ...