As soon as you join Spring-boot-starter-data-redis, Springboot automatically recognizes and uses Redis as the cache container, using the following methods
Gradle Join Dependency
Compile ("Org.springframework.boot:spring-boot-starter-data-redis:${springbootversion}")
Enable caching in Redis configuration
Redis Custom key generation rule
@Bean public keygenerator wiselykeygenerator () { return to new Keygenerator () { @Override 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 (); } }; }
Sometimes we need to use redistemplate, so we can configure
@Bean Publicredisconnectionfactory redisconnectionfactory () {return Newjedisconnectionfactory (); } @Bean Publicredistemplate redistemplate (redisconnectionfactory redisconnectionfactory) {redistemplate RedisTemplate=Newredistemplate (); Redistemplate.setconnectionfactory (redisconnectionfactory); returnredistemplate; } @Bean PublicCacheManager CacheManager (redistemplate redistemplate) {return NewRediscachemanager (redistemplate); }
Full code
Packagecn.xiaojf.today.data.redis.configuration;ImportOrg.springframework.cache.CacheManager;Importorg.springframework.cache.annotation.EnableCaching;ImportOrg.springframework.cache.interceptor.KeyGenerator;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.data.redis.cache.RedisCacheManager;Importorg.springframework.data.redis.connection.RedisConnectionFactory;Importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;Importorg.springframework.data.redis.core.RedisTemplate;ImportJava.lang.reflect.Method;/*** Redis Cache configuration *@authorxiaojf 2016/12/7 10:29.*/@Configuration @enablecaching Public classredisconfiguration {@Bean Publickeygenerator Wiselykeygenerator () {return NewKeygenerator () {@Override Publicobject Generate (Object target, Method method, Object ... params) {StringBuilder sb=NewStringBuilder (); Sb.append (Target.getclass (). GetName ()); Sb.append (":" +method.getname ()); for(Object obj:params) {sb.append (":" +obj.tostring ()); } returnsb.tostring (); } }; } @Bean Publicredisconnectionfactory redisconnectionfactory () {return Newjedisconnectionfactory (); } @Bean Publicredistemplate redistemplate (redisconnectionfactory redisconnectionfactory) {redistemplate RedisTemplate=Newredistemplate (); Redistemplate.setconnectionfactory (redisconnectionfactory); returnredistemplate; } @Bean PublicCacheManager CacheManager (redistemplate redistemplate) {return NewRediscachemanager (redistemplate); }}
Examples of Use
PackageCn.xiaojf.today.sys.dao.impl;ImportCn.xiaojf.today.base.dao.impl.BaseDaoImpl;ImportCn.xiaojf.today.base.model.PageInfo;ImportCn.xiaojf.today.base.util.Asserts;ImportCn.xiaojf.today.sys.dao.SysUserDao;Importcn.xiaojf.today.sys.repository.SysUserRepository;ImportCn.xiaojf.today.sys.entity.SysUser;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.cache.annotation.CacheConfig;Importorg.springframework.stereotype.Repository;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set;/*** User data Access Interface implementation class *@authorxiaojf 2017/2/9 17:40.*/@Repository@CacheConfig (cachenames = {"Sysusercache"}) Public classSysuserdaoimplextendsBasedaoimpl<sysuser>ImplementsSysuserdao {@AutowiredPrivatesysuserrepository userrepository; PublicSysuserdaoimpl () {Super(Sysuserrepository.class); } @Override//@Cacheable (key = "#username") Publicsysuser Getbyusername (String username) {asserts.notnull (username,"User name cannot be empty"); Sysuser User=Userrepository.findfirstbyusername (username); returnuser; }}
The other use of spring cache is simple, can be directly crossing network or Baidu example
Springboot Redis Cache Objects