零、前置文章知识
一、核心功能模块
1.1 带 TTL set
public void set(String key, Object value, Long time, TimeUnit unit){
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value),time,unit);
}
1.2 带 逻辑过期 set
public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit){
RedisData redisData = new RedisData(LocalDateTime.now().plusSeconds(unit.toSeconds(time)),value);
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));
}
1.3 get 避免缓存穿透(缓存空值)
public <R,ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type, Long time, TimeUnit unit, Function<ID,R> dbFallback){
String key = keyPrefix + id;
String cacheJson = stringRedisTemplate.opsForValue().get(key);
if (StrUtil.isNotBlank(cacheJson)){
return JSONUtil.toBean(cacheJson, type);
}
if(cacheJson != null){
return null;
}
R r = dbFallback.apply(id);
if(r == null){
stringRedisTemplate.opsForValue().set(key,"", RedisConstants.CACHE_NULL_TTL, TimeUnit.MINUTES);
return null;
}
set(key, r, time, unit);
return r;
}
1.4 get 避免缓存击穿(逻辑过期)
public <R,ID> R queryWithLogicExpire(String keyPrefix, ID id, Class<R> type, Long time, TimeUnit unit, Function<ID,R> dbFallback) {
String key = keyPrefix + id;
String cacheJson = stringRedisTemplate.opsForValue().get(key);
if (StrUtil.isBlank(cacheJson)) {
return null;
}
RedisData redisData = JSONUtil.toBean(cacheJson, RedisData.class);
R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
LocalDateTime expireTime = redisData.getExpireTime();
if(!expireTime.isAfter(LocalDateTime.now())){
return r;
}
String lockKey = RedisConstants.LOCK_SHOP_KEY+id;
boolean isLock = tryLock(lockKey);
if(isLock){
CACHE_REBUILD_EXXECUTOR.submit(()->{
R curR = dbFallback.apply(id);
setWithLogicExpire(key,curR,time,unit);
unLock(lockKey);
});
}
return r;
}
二. 完整代码及调用
2.1 完整代码
package com.hmdp.utils;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.hmdp.entity.Shop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
@Slf4j
@Component
public class CacheClient {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ExecutorService CACHE_REBUILD_EXXECUTOR = Executors.newFixedThreadPool(10);
public void set(String key, Object value, Long time, TimeUnit unit){
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value),time,unit);
}
public void setWithLogicExpire(String key, Object value, Long time, TimeUnit unit){
RedisData redisData = new RedisData(LocalDateTime.now().plusSeconds(unit.toSeconds(time)),value);
stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));
}
public <R,ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type, Long time, TimeUnit unit, Function<ID,R> dbFallback){
String key = keyPrefix + id;
String cacheJson = stringRedisTemplate.opsForValue().get(key);
if (StrUtil.isNotBlank(cacheJson)){
return JSONUtil.toBean(cacheJson, type);
}
if(cacheJson != null){
return null;
}
R r = dbFallback.apply(id);
if(r == null){
stringRedisTemplate.opsForValue().set(key,"", RedisConstants.CACHE_NULL_TTL, TimeUnit.MINUTES);
return null;
}
set(key, r, time, unit);
return r;
}
public <R,ID> R queryWithLogicExpire(String keyPrefix, ID id, Class<R> type, Long time, TimeUnit unit, Function<ID,R> dbFallback) {
String key = keyPrefix + id;
String cacheJson = stringRedisTemplate.opsForValue().get(key);
if (StrUtil.isBlank(cacheJson)) {
return null;
}
RedisData redisData = JSONUtil.toBean(cacheJson, RedisData.class);
R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);
LocalDateTime expireTime = redisData.getExpireTime();
if(!expireTime.isAfter(LocalDateTime.now())){
return r;
}
String lockKey = RedisConstants.LOCK_SHOP_KEY+id;
boolean isLock = tryLock(lockKey);
if(isLock){
CACHE_REBUILD_EXXECUTOR.submit(()->{
R curR = dbFallback.apply(id);
setWithLogicExpire(key,curR,time,unit);
unLock(lockKey);
});
}
return r;
}
private boolean tryLock(String key){
Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", RedisConstants.LOCK_SHOP_TTL, TimeUnit.SECONDS);
return BooleanUtil.isTrue(flag);
}
private void unLock(String key){
stringRedisTemplate.delete(key);
}
}
2.2 工具类调用
public Result queryById(Long id) {
Shop shop = cacheClient.queryWithLogicExpire(RedisConstants.CACHE_SHOP_KEY,id,Shop.class,RedisConstants.CACHE_SHOP_TTL,TimeUnit.MINUTES,(id2)->getById(id2));
if (shop == null) {
return Result.fail("店铺不存在!");
}
return Result.ok(shop);
}