Springboot Redis Cache Objects

Source: Internet
Author: User

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

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.