SpringBoot (ten): SpringBoot integrate Memcached

First, prepare the environment
memcached 1.4.5
SpringBoot 1.5.10.RELEASE
java_memcached-release_2.6.6.jar
memcached 1.4.5 Windows Download: http: //www.runoob.com/memcached/window-install-memcached.html

danga memcached java client Download: https: //github.com/gwhalin/Memcached-Java-Client/downloads

pom-dependent:

<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.5.6</version>
</dependency>

<dependency>
<groupId>com.danga</groupId>
<artifactId>java_memcached-release</artifactId>
<version>2.6.6</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/java_memcached-release_2.6.6.jar</systemPath>
</dependency>

二、项目结构

 

 


Third, the code details
application.yml:

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.20:3306/test?useSSL=false
username: root
password: root123

rabbitmq:
host: 192.168.1.123
port: 5672
username: admin
password: 1234
# virtual-host: /vhost_test
# publisher-confirms: true

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

logging.level.com.demo.mapper: debug


MemcacheConfiguration.java

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* @author zh
* @ClassName cn.saytime.config.config.MemcacheConfiguration
* @Description Memcache配置
*/
@Configuration
public class MemcacheConfiguration {

@Value("${memcache.servers}")
private String[] servers;
@Value("${memcache.failover}")
private boolean failover;
@Value("${memcache.initConn}")
private int initConn;
@Value("${memcache.minConn}")
private int minConn;
@Value("${memcache.maxConn}")
private int maxConn;
@Value("${memcache.maintSleep}")
private int maintSleep;
@Value("${memcache.nagel}")
private boolean nagel;
@Value("${memcache.socketTO}")
private int socketTO;
@Value("${memcache.aliveCheck}")
private boolean aliveCheck;

@Bean
public SockIOPool sockIOPool () {
SockIOPool pool = SockIOPool.getInstance();
pool.setServers(servers);
pool.setFailover(failover);
pool.setInitConn(initConn);
pool.setMinConn(minConn);
pool.setMaxConn(maxConn);
pool.setMaintSleep(maintSleep);
pool.setNagle(nagel);
pool.setSocketTO(socketTO);
pool.setAliveCheck(aliveCheck);
pool.initialize();
return pool;
}

@Bean
public MemCachedClient memCachedClient(){
return new MemCachedClient();
}

}

 

Test class SpringbootMemcacheApplicationTests.java

import com.danga.MemCached.MemCachedClient;
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 java.util.Date;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMemcacheApplicationTests {

@Autowired
private MemCachedClient memCachedClient;

@Test
public void contextLoads() throws InterruptedException {
// 放入缓存
boolean flag = memCachedClient.set("a", 1);

// 取出缓存
Object a = memCachedClient.get("a");
System.out.println(a);


// 3s后过期
memCachedClient.set("b", "2", new Date(3000));
Object b = memCachedClient.get("b");
System.out.println(b);

Thread.sleep(3000);
b = memCachedClient.get("b");
System.out.println(b);

}

}

 

First run memcached, then perform the test, the output is:

. 1
2
null
test memcached access success and failure time.

Four, part of the operation method
set consistent results when the add key does not exist, does not succeed when the add key exists.
set to replace consistent results when key exists, replace not succeed does not exist in the key.
Pay attention point
using danga memcached expiration time set in two ways:

第一种
memCachedClient.set("xx", "xx", new Date(3000));

第二种
memCachedClient.set("xx", "xx", new Date(System.currentTimeMillis() + 3 * 1000));


Comparison of the two forms, the first is to expire after a specified key 3s, the second is to specify the key in xxxx-xxxx xx: xx: xx time point of failure, if the server is inconsistent with the client time to time, you will want to follow the results to be different, such as the client time now point 2018-01-01 00:00:00 2018-01-01 00:00:10 server time, fast time server 10s, so if the client uses after the second failure mode settings 30s, that is, 2018-01-01 00:00:30 failure, in fact, the client wanted was to fail after 30s, and 20s will be key server fails.

Can be found from the best to use the first form, but the first form will be a problem at certain times, such as if the set time of less than 1s, you will find the key will be saved permanently without fail at a given time the reason can be obtained by source.

 

 

Description When the time is less than 1s, the first approach will result in the specified time does not take effect, key permanent, this time if the client server time when there is no error, use the second form.

About memcached maximum setting valid for 30 days of the case being not tested.

Guess you like

Origin www.cnblogs.com/2019lgg/p/11714491.html