安装memcached服务端
http://www.runoob.com/memcached/window-install-memcached.html
1.pom.xml
<!-- https://mvnrepository.com/artifact/com.whalin/Memcached-Java-Client -->
<dependency>
<groupId>com.whalin</groupId>
<artifactId>Memcached-Java-Client</artifactId>
<version>3.0.2</version>
</dependency>
2.application.properties
#缓存机制配置
memcache.servers=127.0.0.1:11211
memcache.weights=5
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
3.配置SockIOPoolConfig
package com.example.demo;
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;
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;
}
private int minConn;
private int maxConn;
private long maintSleep;
private boolean nagle;
private int socketTO;
}
package com.example.demo;
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.stereotype.Component;
@Component
public class memcacheConfig {
@Autowired
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 MemCachedClient memCachedClient(){
return new MemCachedClient();
}
}
4.使用MemcacheClient
package com.example.demo;
import com.whalin.MemCached.MemCachedClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
@Autowired
MemCachedClient memCachedClient;
@Test
public void contextLoads() {
boolean i = memCachedClient.set("id", 123, 1000);
System.out.println(String.valueOf(i));
System.out.println(memCachedClient.get("id"));
}
}