添加XMemcached依赖:
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
<version>2.4.0</version>
</dependency>
属性配置文件application.yml
memcached:
servers: 192.168.0.x:11211 192.168.0.x:11211
poolSize: 10
sanitizeKeys: false
openCache: true # true为启动缓存 false为标准实现
属性类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lombok.Data;
/**
* memcached--分布式配置属性
* @author zj
*
*/
@Component
@ConfigurationProperties(prefix = "memcached")
@Data
public class XMemcachedProperties {
private String servers;
private int poolSize;
private boolean sanitizeKeys;
private boolean openCache;
}
构建类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.command.BinaryCommandFactory;
import net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator;
@Configuration
public class XMemcachedConfig {
@Autowired
private XMemcachedProperties xMemcachedProperties;
// 构建builder
@Bean
public MemcachedClientBuilder getXMBuilder(){
MemcachedClientBuilder memcachedClientBuilder = null;
try{
String servers = xMemcachedProperties.getServers();
System.out.println("servers="+servers);
memcachedClientBuilder = new XMemcachedClientBuilder(servers);
// 开启/关闭failure模式
memcachedClientBuilder.setFailureMode(false);
memcachedClientBuilder.setSanitizeKeys(xMemcachedProperties.isSanitizeKeys());
memcachedClientBuilder.setConnectionPoolSize(xMemcachedProperties.getPoolSize());
memcachedClientBuilder.setCommandFactory(new BinaryCommandFactory());
memcachedClientBuilder.setOpTimeout(3000);
memcachedClientBuilder.setSessionLocator(new KetamaMemcachedSessionLocator());
// 诸多XMemcached配置
return memcachedClientBuilder;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
// client
@Bean
public MemcachedClient getXMClient(MemcachedClientBuilder memcachedClientBuilder){
MemcachedClient memcachedClient = null;
try{
memcachedClient = memcachedClientBuilder.build();
return memcachedClient;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
service类型 实现memcached增删改查:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import lombok.extern.slf4j.Slf4j;
import net.rubyeye.xmemcached.MemcachedClient;
@Component
@Slf4j
public class XMemcachedService {
@Autowired
private MemcachedClient memcachedClient;
/**
* 新增
* @param key
* @param value
*/
public boolean showAdd(String key, String value){
try {
return memcachedClient.set(key, 0, value);
}catch (Exception e){
e.printStackTrace();
}
return false;
}
/**
* 新增-时间失效
* @param key
* @param value
* @param expire 过期时间,以秒为单位。最多30天。30岁以后*天,被视为确切日期的unix时间戳。
* @return
*/
public boolean showAddTime(String key, String value,Integer expire){
try {
return memcachedClient.set(key, expire, value);
}catch (Exception e){
e.printStackTrace();
}
return false;
}
/**
* 查询
* @param key
* @return
*/
public String showQuery(String key){
try {
return memcachedClient.get(key);
}catch (Exception e){
e.printStackTrace();
}
return "";
}
/**
* 删除
* @param key
* @return
*/
public boolean showDelete(String key){
try {
return memcachedClient.delete(key);
}catch (Exception e){
e.printStackTrace();
}
return false;
}
}
XMemcachedClient 的创建有比较多的可选项,因此提供了一个 XMemcachedClientBuilder 用于构建 MemcachedClient 。 MemcachedClient 是主要接口,操作 memcached 的主要方法都在这个接口里, XMemcachedClient 是它的一个实现。传入的 memcached 节点列表要求是类似 ”host1:port1 host2:port2 …” 这样的字符串,通过 AddrUtil.getAddresses 方法获取实际的 IP 地址列表。存储数据是通过 set 方法,它有三个参数,第一个是存储的 key 名称,第二个是 expire 时间(单位秒) ,超过这个时间 ,memcached 将这个数据替换出去, 0 表示永久存储(默认是一个月) ,第三个参数就是实际存储的数据,可以是任意的 java 可序列化类型 。 获取存储的数据是通过 get 方法,传入 key 名称即可。如果要删除存储的数据,这是通过 delete 方法,它也是接受 key 名称作为参数。 XMemcached 由于是基于 nio ,因此通讯过程本身是异步的, client 发送一个请求给 memcached ,你是无法确定 memcached 什么时候返回这个应答,客户端此时只有等待,因此还有个等待超时的概念在这里。客户端在发送请求后,开始等待应答,如果超过一定时间就认为操作失败,这个等待时间默认是一秒,上面例子展现的 3 个方法调用的都是默认的超时时间,这三个方法同样有允许传入超时时间的重载方法
测试类:
import javax.annotation.Resource;
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 cn.com.dop.memcache.service.MemcachedRunner;
import cn.com.dop.memcache.service.XMemcachedService;
import net.spy.memcached.MemcachedClient;
/**
*
* @author zj
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class testSetGet {
@Autowired
private XMemcachedService xMemcachedService;
@Test
public void testSetGet() {
xMemcachedService.showAdd("teacher", "good teacher !");
xMemcachedService.showAddTime("test-time", "测试定时失效!", 1000);
System.out.println("*********** "+xMemcachedService.showQuery("teacher"));
}
}
测试结果: