spring boot使用 cache和redis,@Cacheable失效解决
2018-01-05 21:25
896 查看
先说一下如何配置spring boot+spring cache +redis
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.BUILD-SNAPSHOT</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> </dependencies>
application.property
server.contextPath=/a server.port=8081 spring.redis.database=0 spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password= spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.timeout=0
java配置
CacheConfig.java
import java.lang.reflect.Method; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.CacheResolver; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.cache.interceptor.SimpleCacheResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration @EnableCaching public class CacheConfig{ @Bean public KeyGenerator keyGenerator() { return 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(); } }; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory){ StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template);//设置序列化工具 template.afterPropertiesSet(); return template; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //设置缓存过期时间 cacheManager.setDefaultExpiration(10000); return cacheManager; } private void setSerializer(RedisTemplate template){ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = 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); } }
service层添加缓存
UserServiceImpl.java
import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import com.sda.test.service.UserService; @Service public class UserServiceImpl implements UserService { @Cacheable(value = "aa", key = "'1'") public String getUser() { // 该打印用来判断是否重复进入方法内 System.out.println("in"); // 如果return null 不会缓存,必须有返回值 return "1"; } }
写个controller调用
WebController.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.sda.test.service.UserService; @RestController public class WebController { @Autowired UserService userService; @RequestMapping("/s") public void name() { userService.getUser(); } }
浏览器输入http://127.0.0.1:8081/a/s
多次访问,查看控制台打印信息。会发现只有第一次访问会进入方法内,之后不再打印,因为直接读的redis缓存
遇到的问题
1.在RedisCacheConfig中引入了,引用有注解bean的bean,则cache不能用,举例来说
假如UserDAO.java 中有个方法用了缓存,如
UserDAO.java
@Cacheable(value = "aa", key = "'1'") public String getUser() { return "1"; }
而UserServiceImpl.java中引用了了UserDAO
UserServiceImpl.java
@Resource UserDAO userDAO;
一旦CacheConfig.java中同时存在UserDAO和UserServiceImpl ,那么cache缓存就失效了
CacheConfig.java
@Configuration @EnableCaching public class CacheConfig{ @Resource UserDAO userDAO; //UserService 中也有userDAO则cache失效。没有userDAO则可用 @Autowired UserService userService; @Bean public KeyGenerator keyGenerator() { return 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(); } }; } @Bean public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory){ StringRedisTemplate template = new StringRedisTemplate(factory); setSerializer(template);//设置序列化工具 template.afterPropertiesSet(); return template; } @Bean public CacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); //设置缓存过期时间 cacheManager.setDefaultExpiration(10000); return cacheManager; } private void setSerializer(RedisTemplate template){ Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = 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); } }
原因还不知道,应该跟aop有关,先记下来
相关文章推荐
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- springboot中使用redis,key序列化问题解决方案
- 在 spring boot 中使用spring cache + redis
- 解决maven工程中使用spring-boot后导致的profile多环境配置失效的问题
- spring boot 下 shiro+redis与mybatis redis cache冲突的解决办法
- spring4集成redis 并使用@cacheable注解
- Spring-Boot (四) cache/ehcache/redis-cache集成使用
- spring boot 中使用 Redis 出现 java.net.SocketTimeoutException: connect timed out 求解决!
- spring redis cache使用思考
- Spring Boot使用redis做数据缓存
- 3 Springboot中使用redis,redis自动缓存异常处理
- springboot结合redis使用CachingConfigurerSupport方法不能被继承
- spring boot使用redis
- spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用
- spring boot / cloud (七) 使用@Retryable来进行重处理
- jedis,spring-redis-data 整合使用,版本问题异常以及解决。
- REDIS学习(3.2)spring boot 使用redis作为缓存
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用