spring boot整合memcache

时间:2024-07-04 18:33:20

1.导入memcached客户端jar包

<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.2</version>
</dependency>

2.在application.yml设置memcache连接池配置属性值

memcache:
servers: 127.0.0.1:11211
failover: true
initConn: 100
minConn: 20
maxConn: 1000
maintSleep: 50
nagel: false
socketTO: 3000
aliveCheck: true

3.设置项目启动读取application.yml中memcache属性值

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "memcache")
public class SockIOPoolConfig { private String[] servers; private Integer[] weights; private int initConn; private int minConn; private int maxConn; private long maintSleep; private boolean nagle; private int socketTO; public String[] getServers() {
return servers;
} public void setServers(String[] servers) {
this.servers = servers;
} public Integer[] getWeights() {
return weights;
} public void setWeights(Integer[] weights) {
this.weights = weights;
} public int getInitConn() {
return initConn;
} public void setInitConn(int initConn) {
this.initConn = initConn;
} public int getMinConn() {
return minConn;
} public void setMinConn(int minConn) {
this.minConn = minConn;
} public int getMaxConn() {
return maxConn;
} public void setMaxConn(int maxConn) {
this.maxConn = maxConn;
} public long getMaintSleep() {
return maintSleep;
} public void setMaintSleep(long maintSleep) {
this.maintSleep = maintSleep;
} public boolean isNagle() {
return nagle;
} public void setNagle(boolean nagle) {
this.nagle = nagle;
} public int getSocketTO() {
return socketTO;
} public void setSocketTO(int socketTO) {
this.socketTO = socketTO;
}

4.初始化memcache客户端

import com.idelan.iot.client.Memcache;
import com.whalin.MemCached.MemCachedClient;
import com.whalin.MemCached.SockIOPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class MemcacheConfig { @Autowired
private SockIOPoolConfig sockIOPoolConfig; @Bean
public SockIOPool sockIOPool(){ //获取连接池的实例
SockIOPool pool = SockIOPool.getInstance();
//服务器列表及其权重
String[] servers = sockIOPoolConfig.getServers();
Integer[] weights = sockIOPoolConfig.getWeights();
//设置服务器信息
pool.setServers(servers);
pool.setWeights(weights);
//设置初始连接数、最小连接数、最大连接数、最大处理时间
pool.setInitConn(sockIOPoolConfig.getInitConn());
pool.setMinConn(sockIOPoolConfig.getMinConn());
pool.setMaxConn(sockIOPoolConfig.getMaxConn());
//设置连接池守护线程的睡眠时间
pool.setMaintSleep(sockIOPoolConfig.getMaintSleep());
//设置TCP参数,连接超时
pool.setNagle(sockIOPoolConfig.isNagle());
pool.setSocketConnectTO(sockIOPoolConfig.getSocketTO());
//初始化并启动连接池
pool.initialize();
return pool;
} @Bean
public Memcache memCachedClient(){
return new Memcache(new MemCachedClient());
}

5.编写Memcache类,实现memcache基本操作

import com.whalin.MemCached.MemCachedClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils; public class Memcache { private final static Logger logger = LoggerFactory.getLogger(Memcache.class); private MemCachedClient memCachedClient; public Memcache(MemCachedClient memCachedClient) {
this.memCachedClient = memCachedClient;
} /**
* 将netty客户端信息存入memcached
* @param obj
* @return
*/
public boolean addChannel(String key, Object obj) {
try {
if (obj == null) {
return false;
}
return memCachedClient.set(key, obj);
} catch (Exception e) {
logger.error("netty客户端插入memcache出错", e);
return false;
}
} /**
* 将netty客户端信息从memcached中移除
* @param key
* @return
*/
public boolean removeChannel(String key) {
try {
if (StringUtils.isEmpty(key)) {
return false;
} return memCachedClient.delete(key);
} catch (Exception e) {
logger.error("netty客户端移除memcache出错", e);
return false;
}
} /**
* 从memcache中获取netty客户端
* @param key
* @return
*/
public Object getChannel(String key) {
try {
if (StringUtils.isEmpty(key)) {
return null;
}
return memCachedClient.get(key);
} catch (Exception e) {
logger.error("从memcache中获取客户端出错", e);
return null;
}
} /**
* 检测memcache中设备是否登录过
* @param key
* @return
*/
public boolean exist(String key) {
try {
if (StringUtils.isEmpty(key)) {
return false;
} return memCachedClient.keyExists(key);
} catch (Exception e) {
logger.error("检测memcache中是否存在某个设备出错", e);
return false;
}
}

6.方法调用

@Autowired
private Memcache memcache;

通过@Autowired注入Memcache类来调用memcach方法