1. Creating the Cache configuration class
@Configuration @enablecaching Public classRediscacheconfigextendsCachingconfigurersupport {@Value ("${redis.cache.expiration}") PrivateLong expiration; /*** * Manage Cache*/@Bean PublicCacheManager CacheManager (Redistemplate<object, object>redistemplate) {Rediscachemanager CacheManager=NewRediscachemanager (redistemplate); Cachemanager.setdefaultexpiration (expiration);//set the default cache expiration time (global) seconds returnCacheManager; } /*** Custom Generated Redis key * When using @cacheable, if you do not specify key, use a key generated by the default key generator *@return */@Bean @Override Publickeygenerator Keygenerator () {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 PublicRedistemplate<string, string>redistemplate (Redisconnectionfactory Factory) {stringredistemplate template=Newstringredistemplate (Factory); Jackson2jsonredisserializer Jackson2jsonredisserializer=NewJackson2jsonredisserializer (Object.class); Objectmapper om=NewObjectmapper (); Om.setvisibility (Propertyaccessor.all, JsonAutoDetect.Visibility.ANY); Om.enabledefaulttyping (ObjectMapper.DefaultTyping.NON_FINAL); Jackson2jsonredisserializer.setobjectmapper (OM); Template.setvalueserializer (Jackson2jsonredisserializer); Template.afterpropertiesset (); returntemplate; }}
2. Use
1. Manual mode:
Jedis Tool Class
@Component Public classRedisutil {PrivateLogger Logger =Loggerfactory.getlogger (GetClass ()); @AutowiredPrivatestringredistemplate stringredistemplate; @AutowiredPrivateRedistemplate<object,object>redistemplate; Public voidset (string key, String value) {Valueoperations<string, string> Ops = This. Stringredistemplate.opsforvalue (); BooleanIsexsit = This. Stringredistemplate.haskey (key); if(isexsit) {Logger.warn ("Key value already exists"); }Else{ops.set (key, value); } } Publicstring Get (String key) {return This. Stringredistemplate.opsforvalue (). get (key); } /*** get all key values corresponding to HashKey *@paramKey Key *@returnthe corresponding multiple key values*/ PublicMap<object, object>hmget (String key) {return This. Redistemplate.opsforhash (). Entries (key); } /*** HashSet and set the time *@paramKey Key *@parammap corresponds to multiple key values *@paramTime (seconds) *@returntrue Success false failed*/ Public BooleanHmset (String key, Map<string,object> Map,LongTime ) { Try { This. Redistemplate.opsforhash (). Putall (key, map); if(time>0){ This. Redistemplate.expire (Key,time,timeunit.seconds); } return true; } Catch(Exception e) {e.printstacktrace (); return false; } } PublicBoolean exists (String key) {Boolean result=Boolean.false; Result= This. Stringredistemplate.haskey (key); returnresult; } Public voiddel (String key) { This. Stringredistemplate.delete (key); } Public voiddelhm (String key) { This. Redistemplate.delete (key); } Public BooleanLock (String key,intseconds) { Booleanresult = Redistemplate.getconnectionfactory (). getconnection (). SETNX (Key.getbytes (), "LOCKED". GetBytes ()); Redistemplate.expire (key, seconds, timeunit.seconds); returnresult; }}
2. Automatic mode:
Add @Cacheable annotations to implement cache additions
Adding @CacheEvict annotations for cache deletion
Specific implementation reference previous article: Viii. springboot integrated Redis
Ix. Springboot Integrated Redis two buffer configuration