Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.vcredit.lrh.db.redis; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.session.data.redis.RedisOperationsSessionRepository; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; /** * * @author Liu Jianwei * @date Aug 23, 2016 2:37:24 PM */ @Configuration @EnableCaching @ComponentScan @EnableRedisHttpSession @EnableConfigurationProperties({ RedisProperties.class }) public class RedisCacheConfiguration extends CachingConfigurerSupport { @Autowired private RedisProperties properties; @Bean(name = "redisConnectionFactory") public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(properties.getHost()); redisConnectionFactory.setPort(properties.getPort()); return redisConnectionFactory; } @Bean(name = "redisTemplate") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); return redisTemplate; } @Bean @DependsOn("redisConnectionFactory") public RedisOperationsSessionRepository sessionRepository(RedisConnectionFactory redisConnectionFactory) { RedisOperationsSessionRepository sessionRepository = new RedisOperationsSessionRepository( redisConnectionFactory); sessionRepository.setDefaultMaxInactiveInterval(properties.getTimeout()); return sessionRepository; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // Number of seconds before expiration. Defaults to unlimited (0) cacheManager.setDefaultExpiration(300); // Sets the default expire time (in seconds) return cacheManager; } }