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方法
spring boot整合memcache的更多相关文章
- Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询
摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...
- spring boot整合jsp的那些坑(spring boot 学习笔记之三)
Spring Boot 整合 Jsp 步骤: 1.新建一个spring boot项目 2.修改pom文件 <dependency> <groupId>or ...
- spring boot 系列之四:spring boot 整合JPA
上一篇我们讲了spring boot 整合JdbcTemplate来进行数据的持久化, 这篇我们来说下怎么通过spring boot 整合JPA来实现数据的持久化. 一.代码实现 修改pom,引入依赖 ...
- Spring Kafka和Spring Boot整合实现消息发送与消费简单案例
本文主要分享下Spring Boot和Spring Kafka如何配置整合,实现发送和接收来自Spring Kafka的消息. 先前我已经分享了Kafka的基本介绍与集群环境搭建方法.关于Kafka的 ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- spring boot整合Hadoop
最近需要用spring boot + mybatis整合hadoop,其中也有碰到一些坑,记录下来方便后面的人少走些弯路. 背景呢是因为需要在 web 中上传文件到 hdfs ,所以需要在spring ...
- Spring Boot整合Elasticsearch
Spring Boot整合Elasticsearch Elasticsearch是一个全文搜索引擎,专门用于处理大型数据集.根据描述,自然而然使用它来存储和搜索应用程序日志.与Logstash和K ...
- spring boot 整合 百度ueditor富文本
百度的富文本没有提供Java版本的,只给提供了jsp版本,但是呢spring boot 如果是使用内置tomcat启动的话整合jsp是非常困难得,今天小编给大家带来spring boot整合百度富文本 ...
- spring boot 整合quartz ,job不能注入的问题
在使用spring boot 整合quartz的时候,新建定时任务类,实现job接口,在使用@AutoWire或者@Resource时,运行时出现nullpointException的问题.显然是相关 ...
随机推荐
- 查看linux系统安装的服务
如何查看linux系统安装了哪些服务呢,因不同版本的操作系统可能使用的命令不一样或者有些命令在某些操作系统不可用,现列举一些常用查看命令(基于我的linux版本). 我的操作系统版本如下: 1.ser ...
- webapck imports-loader和exports-loader的使用
webapck imports-loader和exports-loader的使用
- iOS 版本更新迭代
开发中我们可能会遇到这样的需求,当AppStore中有新版本迭代更新,在用户点开APP的时候弹框提醒客户去AppStore更新APP.这里面就有个关键点,判断当前APP与AppStore中的版本高低, ...
- 压缩感知与稀疏模型——Convex Methods for Sparse Signal Recovery
第三节课的内容.这节课上课到半截困了睡着了,看着大家都很积极请教认真听讲,感觉很惭愧.周末不能熬太晚.这个博客就记录一下醒着时候听到的内容. Motivation 目前的时代需要处理的数据量维度可能很 ...
- 3dmax2019卸载/安装失败/如何彻底卸载清除干净3dmax2019注册表和文件的方法
3dmax2019提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dmax2019失败提示3dmax2019安装未完成,某些产品无法安装,也有时候想重新安装3 ...
- centos 7 下iptables参数详解
在红帽RHEL7系统中firewalld服务取代了iptables服务,如果我们不想用或者不习惯使用firewalld服务,请看下面的操作: iptables命令中则常见的控制类型有: ACCEPT: ...
- [LC] 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 多线程并发 了解 Thread
通过上一篇 多线程并发 (一) 了解 Java 虚拟机 - JVM 了解了java 虚拟机的构成以及对象的创建等.从Java虚拟机栈我们知道每当我们创建一个线程JVM就会给我们的线程分配一个私有的内存 ...
- CSS--沃顿商学院网页布局
源代码: <head> 右键CSS样式--附加样式表 </head> <body> <div id="dd"> <div id ...
- 对Design model的理解与Java design model的归纳
设计模式的起源是面向对象程序设计思想,是面向对象设计的精髓--抽象.面向对象通过类和对象来实现抽象,实现时产生了面向对象的三个重要机制:封装.继承.多态.正是这三个机制衍生出了各种各样的设计模式.在面 ...