redis:host:172.27.15.23 port:6379 database:0 pool:max-idle:20 Min-idle:1 max-active:20 max-wait:60000
import org.springframework.cache.CacheManager; import
org.springframework.cache.annotation.EnableCaching;
Import Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.data.redis.cache.RedisCacheManager;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; @Configuration @EnableCaching public class Redisconfig {@Bean public cachemanager CacheManager (redistemplate Red
Istemplate) {Redistemplate.setkeyserializer (New Genericjackson2jsonredisserializer ());
Redistemplate.setvalueserializer (New Genericjackson2jsonredisserializer ());
Rediscachemanager Rediscachemanager = new Rediscachemanager (redistemplate);
return rediscachemanager; }
}
import org.springframework.beans.factory.annotation.Autowired; import
Org.springframework.context.annotation.Bean;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.data.redis.core.RedisTemplate;
Import Org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
Import Org.springframework.data.redis.serializer.StringRedisSerializer; /** * Redis replaces the default serialized key and value object to resolve the server read garbled. (This problem does not have to be resolved, only for server read visual display.) * @time November 22, 2017 morning 11:00:53 **/@Configuration public class Redisconfig {@Autowired private Redis
Template redistemplate; @Bean public redistemplate Redistemplateinit () {//Set the instantiated object for the serialized key Redistemplate.setkeyseri
Alizer (New Stringredisserializer ());
Sets the instantiated object for serialization value Redistemplate.setvalueserializer (new Genericjackson2jsonredisserializer ());
return redistemplate; }
}
@Test public void Savemobilecodeinredis () {//1 phone number, code, expiration time, whether or not already used//code code = new Code ("15
818232009 ", 123456,0);
String mobile = "15818232009";
String code = "654321"; Long timeout = new Long (600); 600 seconds expired//Before the user issued the authentication code to determine whether there is no failure of the phone in the cache verification code valueoperations<string, string> operations = redistemplate.
Opsforvalue ();
if (! Redistemplate.haskey (MOBILE)) {System.out.println ("write cache"); Operations.set (Mobile, code, timeout, timeunit.seconds);//write Cache}//below use if (Redistemplate.haskey mo
Bile)) {if (Operations.get (mobile). Equals (code)) {System.out.println ("successful"); Long time = Redistemplate.getexpire (mobile, timeunit.milliseconds), and/or the expiration date based on key and converted to the specified unit System.out.printl
N ("Expired time remaining (ms) = =" +time+ "MS");
Redistemplate.delete (mobile); @Ignore public void @Test}}} Testredis () {print ();
Private list print () {valueoperations<string, list> list = Redistemplate.opsforvalue ();
list<department> results = list.get ("Departments:info:all");
for (Department d:results) {System.out.println (d);
return results; //1 get departmental data from the database//2 write an interceptor, block inserts or Update,delete or, monitor changes to update the department cache, but the problem is, if it is directly modify the database. For this, the nightly 0 o'clock update cache private void Setdepartmentsinredis () {//Practice returns String, List, Hash, Set, Zset list<
department> departments = Departmentmapper.selectall (); The preferred method is to use "Business Name: Object Name: Id:[Property]" as the key name Redistemplate.opsforvalue (). Set ("Echo:department:all", departments, 200,
Timeunit.seconds);//write cache, 200 seconds expired}