Ix. Springboot Integrated Redis two buffer configuration

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.