本文主要是介绍springboot1.x自定义redisManager,解决并发问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
RedisCache的问题
RedisCache是先判断key是否存在,不存在返回null;存在再获取值,存在并发问题。当key存在并获取值时,缓存被别的线程删除了,则返回值就为null了。
修改方案:先从redis查询值,再判断key是否存在。
自定义RedisCacheManger
public class CustomizedRedisCacheManager extends RedisCacheManager {public CustomizedRedisCacheManager(RedisOperations redisOperations) {super(redisOperations);}public CustomizedRedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) {super(redisOperations, cacheNames);}@Overrideprotected Cache getMissingCache(String name) {long expiration = computeExpiration(name);return new CustomizedRedisCache(name, (this.isUsePrefix() ?
这篇关于springboot1.x自定义redisManager,解决并发问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!