Spring Boot Redis Configuration

Source: Internet
Author: User
Tags redis delete cache

Import dependency

<!--redis-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    < artifactid>spring-boot-starter-redis</artifactid>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactid>spring-boot-starter-cache</ Artifactid>
</dependency>
Redis yml
Spring:
  redis:
    database:0
    host:10.0.0.163
    port:6379
    timeout:3000
    pool:
      # Connection pool maximum connections (using negative values for No limit)
      max-active:8
      # Connection pool maximum blocking wait time (with negative values for No limit)
      max-wait:-1
      # maximum idle connections
      in the connection pool Max-idle:8
      # Minimum idle connection in the connection pool
      min-idle:0
@Configuration @EnableCaching public class Redisconfig extends Cachingconfigurersupport {@Value ("${redis-timeout}")

    Private Long redistimeout; Cache Manager @Bean Public CacheManager CacheManager (redistemplate redistemplate) {Rediscachemanager Cachemanag
        ER = new Rediscachemanager (redistemplate);
        Set cache Expiration Time Cachemanager.setdefaultexpiration (redistimeout);
    return CacheManager; } @Bean public redistemplate<string, string> redistemplate (Redisconnectionfactory factory) {Stringr
        Edistemplate template = new Stringredistemplate (factory);
        Setserializer (template);//Set Serialization tool Template.afterpropertiesset ();
    return template; } private void Setserializer (stringredistemplate template) {Jackson2jsonredisserializer jackson2jsonredisseri
        Alizer = 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); }
}

Write Redis as a toolkit for ease of use

@Component public class Redisutil {@Autowired private redistemplate redistemplate; /** * @Description: Bulk Delete cache * @Author: HJ * @Date: 17:13 2017/10/24 */public void Remove (final Str
        ing ... keys) {for (String Key:keys) {remove (key); }}/** * @Description: Bulk Delete Cache (wildcard) * @Author: HJ * @Date: 16:52 2017/10/24 */Public Voi
        D Removepattern (final String pattern) {set<serializable> keys = Redistemplate.keys (pattern);
    if (keys.size () > 0) redistemplate.delete (keys); }/** * @Description: Delete cache * @Author: HJ * @Date: 16:51 2017/10/24 */public void Remove (FINA
        L String Key) {if (Exists (key)) {Redistemplate.delete (key); }}/** * @Description: Determine if there is a corresponding value in the cache * @Author: HJ * @Date: 16:50 2017/10/24 */publi C Boolean exists (final String key) {return RedisTEmplate.haskey (key);  }/** * @Description: Read cache * @Author: HJ * @Date: 16:49 2017/10/24 */public Object get (final
    String key) {return Redistemplate.opsforvalue (). get (key); }/** * @Description: Write Cache * @Author: HJ * @Date: 16:48 2017/10/24 */public Boolean set (FINA
        L String key, Object value) {Boolean result = false;
            try {redistemplate.opsforvalue (). Set (key, value);
        result = true;
        } catch (Exception e) {e.printstacktrace ();
    } return result; }/** * @Description: Write cache (can configure expiration time) * @Author: HJ * @Date: 16:46 2017/10/24 * * Public Boolea
        N Set (Final String key, Object value, Long expiretime) {Boolean result = false;
            try {redistemplate.opsforvalue (). Set (key, value);
            Redistemplate.expire (Key, Expiretime, timeunit.seconds);
        result = true; } catch (ExCeption e) {e.printstacktrace ();
    } return result; }

}

The controller will not write, test the service

@Service public
class Testservice {

    @Autowired
    private testmapper testmapper;

    @Autowired
    private Redisutil redisutil;

    Public Test test1 (test test) {
        Test mytest = Testmapper.getonebyid (Test.getid ());
        Redisutil.set ("Test", mytest,10l);  10 seconds expired
        return mytest;
    }

    Public Test test2 () {
        return (test) redisutil.get ("test");
    }
}
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.