spring boot整合memcache

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方法

转载于:https://www.cnblogs.com/gyli20170901/p/9564441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值