Spring boot encapsulates the NoSQL database with the usual database support. Redis Introduction
Redis is the most widely used memory data store in the industry today. Memcached,redis supports richer data structures, such as hashes, lists, sets, etc., while supporting data persistence. In addition, Redis also provides some characteristics of the class database, such as transaction, HA, master-slave library. It can be said that Redis has some characteristics of caching system and database, so it has rich application scene. This article describes two typical scenarios for Redis in spring boot. How to use
1. Introduction of Spring-boot-starter-redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-redis</artifactid>
</dependency>
2. Add configuration file
# Redis (redisproperties)
# Redis Database index (default 0)
spring.redis.database=0
# Redis server address
spring.redis.host=192.168.0.58
# Redis Server connection Port
spring.redis.port=6379
# Redis Server connection password (default is null)
spring.redis.password=
# Connection Pool Maximum connection number (using negative values to indicate no limit)
spring.redis.pool.max-active=8
Connection pool Maximum blocking wait time (using negative values to indicate no limit)
spring.redis.pool.max-wait=-1
# Maximum idle connection in a connection pool
spring.redis.pool.max-idle=8
# Minimum idle connection in the pool
spring.redis.pool.min-idle=0
# Connection Timeout (ms)
spring.redis.timeout=0
3, add Cache configuration class
@Configuration @EnableCaching public class Redisconfig extends cachingconfigurersupport{@Bean public keygene Rator Keygenerator () {return new Keygenerator () {@Override The public object generate (object
Target, method method, Object ... params) {StringBuilder sb = new StringBuilder ();
Sb.append (Target.getclass (). GetName ());
Sb.append (Method.getname ());
for (Object obj:params) {sb.append (obj.tostring ());
return sb.tostring ();
}
};
@SuppressWarnings ("Rawtypes") @Bean public CacheManager CacheManager (redistemplate redistemplate) {
Rediscachemanager RCM = new Rediscachemanager (redistemplate);
Set cache expiration//rcm.setdefaultexpiration (60);//sec return RCM; @Bean public redistemplate<string, string> redistemplate (Redisconnectionfactory factory) {stringredistemplate template = new Stringredistemplate (factory);
Jackson2jsonredisserializer Jackson2jsonredisserializer = new Jackson2jsonredisserializer (Object.class);
Objectmapper om = new Objectmapper ();
Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY);
Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL);
Jackson2jsonredisserializer.setobjectmapper (OM);
Template.setvalueserializer (Jackson2jsonredisserializer);
Template.afterpropertiesset ();
return template; }
}
3, OK, then you can directly use the
@RunWith (Springjunit4classrunner.class)
@SpringApplicationConfiguration (application.class) Public
class Testredis {
@Autowired
private stringredistemplate stringredistemplate;
@Autowired
private redistemplate redistemplate;
@Test public
void Test () throws Exception {
stringredistemplate.opsforvalue (). Set ("AAA", "a");
Assert.assertequals ("A", stringredistemplate).