問題描述
最近我們用Spring Cache + redis來做緩存蛙卤。在高并發(fā)下@Cacheable 注解返回的內(nèi)容是null粘姜。查看了一下源代碼,在使用注解獲取緩存的時(shí)候全跨,RedisCache的get方法會(huì)先去判斷key是否存在,然后再去獲取值亿遂。這了就有一個(gè)漏銅浓若,當(dāng)線程1判斷了key是存在的,緊接著這個(gè)時(shí)候這個(gè)key過期了蛇数,這時(shí)線程1再去獲取值的時(shí)候返回的是null挪钓。
RedisCache的get方法源碼:
public RedisCacheElement get(final RedisCacheKey cacheKey) {
Assert.notNull(cacheKey, "CacheKey must not be null!");
// 判斷Key是否存在
Boolean exists = (Boolean) redisOperations.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(cacheKey.getKeyBytes());
}
});
if (!exists.booleanValue()) {
return null;
}
// 獲取key對(duì)應(yīng)的值
return new RedisCacheElement(cacheKey, fromStoreValue(lookup(cacheKey)));
}
// 獲取值
protected Object lookup(Object key) {
RedisCacheKey cacheKey = key instanceof RedisCacheKey ? (RedisCacheKey) key : getRedisCacheKey(key);
byte[] bytes = (byte[]) redisOperations.execute(new AbstractRedisCacheCallback<byte[]>(
new BinaryRedisCacheElement(new RedisCacheElement(cacheKey, null), cacheValueAccessor), cacheMetadata) {
@Override
public byte[] doInRedis(BinaryRedisCacheElement element, RedisConnection connection) throws DataAccessException {
return connection.get(element.getKeyBytes());
}
});
return bytes == null ? null : cacheValueAccessor.deserializeIfNecessary(bytes);
}
解決方案
這個(gè)流程有問題,解決方案就是把這個(gè)流程倒過來耳舅,先去獲取值碌上,然后去判斷這個(gè)key是否存在。不能直接用獲取的值根據(jù)是否是NULL判斷是否有值浦徊,因?yàn)镽eids可能緩存NULL值馏予。
重寫RedisCache的get方法:
public RedisCacheElement get(final RedisCacheKey cacheKey) {
Assert.notNull(cacheKey, "CacheKey must not be null!");
RedisCacheElement redisCacheElement = new RedisCacheElement(cacheKey, fromStoreValue(lookup(cacheKey)));
Boolean exists = (Boolean) redisOperations.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(cacheKey.getKeyBytes());
}
});
if (!exists.booleanValue()) {
return null;
}
return redisCacheElement;
}
完整實(shí)現(xiàn):
重寫RedisCache的get方法
package com.xiaolyuh.redis.cache;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheElement;
import org.springframework.data.redis.cache.RedisCacheKey;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.util.Assert;
/**
* 自定義的redis緩存
*
* @author yuhao.wang
*/
public class CustomizedRedisCache extends RedisCache {
private final RedisOperations redisOperations;
private final byte[] prefix;
public CustomizedRedisCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration) {
super(name, prefix, redisOperations, expiration);
this.redisOperations = redisOperations;
this.prefix = prefix;
}
public CustomizedRedisCache(String name, byte[] prefix, RedisOperations<? extends Object, ? extends Object> redisOperations, long expiration, boolean allowNullValues) {
super(name, prefix, redisOperations, expiration, allowNullValues);
this.redisOperations = redisOperations;
this.prefix = prefix;
}
/**
* 重寫父類的get函數(shù)。
* 父類的get方法盔性,是先使用exists判斷key是否存在吗蚌,不存在返回null,存在再到redis緩存中去取值纯出。這樣會(huì)導(dǎo)致并發(fā)問題,
* 假如有一個(gè)請(qǐng)求調(diào)用了exists函數(shù)判斷key存在敷燎,但是在下一時(shí)刻這個(gè)緩存過期了暂筝,或者被刪掉了。
* 這時(shí)候再去緩存中獲取值的時(shí)候返回的就是null了硬贯。
* 可以先獲取緩存的值焕襟,再去判斷key是否存在。
*
* @param cacheKey
* @return
*/
@Override
public RedisCacheElement get(final RedisCacheKey cacheKey) {
Assert.notNull(cacheKey, "CacheKey must not be null!");
RedisCacheElement redisCacheElement = new RedisCacheElement(cacheKey, fromStoreValue(lookup(cacheKey)));
Boolean exists = (Boolean) redisOperations.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
return connection.exists(cacheKey.getKeyBytes());
}
});
if (!exists.booleanValue()) {
return null;
}
return redisCacheElement;
}
/**
* 獲取RedisCacheKey
*
* @param key
* @return
*/
private RedisCacheKey getRedisCacheKey(Object key) {
return new RedisCacheKey(key).usePrefix(this.prefix)
.withKeySerializer(redisOperations.getKeySerializer());
}
}
重寫RedisCacheManager
package com.xiaolyuh.redis.cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.Collection;
/**
* 自定義的redis緩存管理器
* @author yuhao.wang
*/
public class CustomizedRedisCacheManager extends RedisCacheManager {
private static final Logger logger = LoggerFactory.getLogger(CustomizedRedisCacheManager.class);
public CustomizedRedisCacheManager(RedisOperations redisOperations) {
super(redisOperations);
}
public CustomizedRedisCacheManager(RedisOperations redisOperations, Collection<String> cacheNames) {
super(redisOperations, cacheNames);
}
@Override
protected Cache getMissingCache(String name) {
long expiration = computeExpiration(name);
return new CustomizedRedisCache(
name,
(this.isUsePrefix() ? this.getCachePrefix().prefix(name) : null),
this.getRedisOperations(),
expiration);
}
}
配置Redis管理器
@Configuration
public class RedisConfig {
// redis緩存的有效時(shí)間單位是秒
@Value("${redis.default.expiration:3600}")
private long redisDefaultExpiration;
@Bean
public RedisCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
RedisCacheManager redisCacheManager = new CustomizedRedisCacheManager(redisTemplate);
redisCacheManager.setUsePrefix(true);
//這里可以設(shè)置一個(gè)默認(rèn)的過期時(shí)間 單位是秒
redisCacheManager.setDefaultExpiration(redisDefaultExpiration);
return redisCacheManager;
}
/**
* 顯示聲明緩存key生成器
*
* @return
*/
@Bean
public KeyGenerator keyGenerator() {
return new SimpleKeyGenerator();
}
}
源碼:
https://github.com/wyh-spring-ecosystem-student/spring-boot-student/tree/releases
spring-boot-student-cache-redis 工程
為監(jiān)控而生的多級(jí)緩存框架 layering-cache這是我開源的一個(gè)多級(jí)緩存框架的實(shí)現(xiàn)饭豹,如果有興趣可以看一下