基于StringRedisTemplate封装一个缓存工具类
Redis实战篇 | Kyle's Blog (cyborg2077.github.io)
目录
方法1:将任意Java对象序列化为JSON,并存储到String类型的Key中,并可以设置TTL过期时间
方法2:将任意Java对象序列化为JSON,并存储在String类型的Key中,并可以设置逻辑过期时间,用于处理缓存击穿问题
方法3:根据指定的Key查询缓存,并反序列化为指定类型,利用缓存空值的方式解决缓存穿透问题
方法4:根据指定的Key查询缓存,并反序列化为指定类型,需要利用逻辑过期解决缓存击穿问题
方法5:根据指定的Key查询缓存,并反序列化为指定类型,需要利用互斥锁解决缓存击穿问题
方法1:将任意Java对象序列化为JSON,并存储到String类型的Key中,并可以设置TTL过期时间
public void set(String key, Object value, Long time, TimeUnit timeUnit) {stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, timeUnit);}
方法2:将任意Java对象序列化为JSON,并存储在String类型的Key中,并可以设置逻辑过期时间,用于处理缓存击穿问题
public void setWithLogicExpire(String key, Object value, Long time, TimeUnit timeUnit) {//由于需要设置逻辑过期时间,所以我们需要用到RedisDataRedisData redisData = new RedisData();//redisData的data就是传进来的value对象redisData.setData(value);//逻辑过期时间就是当前时间加上传进来的参数时间,用TimeUnit可以将时间转为秒,随后与当前时间相加redisData.setExpireTime(LocalDateTime.now().plusSeconds(timeUnit.toSeconds(time)));//由于是逻辑过期,所以这里不需要设置过期时间,只存一下key和value就好了,同时注意value是ridisData类型stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));}
@Data
public class RedisData<T> {private LocalDateTime expireTime;private T data;
}
方法3:根据指定的Key查询缓存,并反序列化为指定类型,利用缓存空值的方式解决缓存穿透问题
- 改为通用方法,那么返回值就需要进行修改,不能返回
Shop
了,那我们直接设置一个泛型,同时ID的类型,也不一定都是Long
类型,所以我们也采用泛型。 - Key的前缀也会随着业务需求的不同而修改,所以参数列表里还需要加入Key的前缀
- 通过id去数据库查询的具体业务需求我们也不清楚,所以我们也要在参数列表中加入一个查询数据库逻辑的函数
- 最后再加上设置TTL需要的两个参数
- 那么综上所述,我们的参数列表需要
- key前缀
- id(类型泛型)
- 返回值类型(泛型)
- 查询的函数
- TTL需要的两个参数
public <R, ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit timeUnit) {//先从Redis中查,这里的常量值是固定的前缀 + 店铺idString key = keyPrefix + id;String json = stringRedisTemplate.opsForValue().get(key);//如果不为空(查询到了),则转为R类型直接返回if (StrUtil.isNotBlank(json)) {return JSONUtil.toBean(json, type);}if (json != null) {return null;}//否则去数据库中查,查询逻辑用我们参数中注入的函数R r = dbFallback.apply(id);//查不到,则将空值写入Redisif (r == null) {stringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}//查到了则转为json字符串String jsonStr = JSONUtil.toJsonStr(r);//并存入redis,设置TTLthis.set(key, jsonStr, time, timeUnit);//最终把查询到的商户信息返回给前端return r;
}
public Result queryById(Long id) {Shop shop = cacheClient.queryWithPassThrough(CACHE_SHOP_KEY, id, Shop.class, this::getById, CACHE_SHOP_TTL, TimeUnit.MINUTES);if (shop == null) {return Result.fail("店铺不存在!!");}return Result.ok(shop);
}
方法4:根据指定的Key查询缓存,并反序列化为指定类型,需要利用逻辑过期解决缓存击穿问题
public <R, ID> R queryWithLogicalExpire(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit timeUnit) {//1. 从redis中查询商铺缓存String key = keyPrefix + id;String json = stringRedisTemplate.opsForValue().get(key);//2. 如果未命中,则返回空if (StrUtil.isBlank(json)) {return null;}//3. 命中,将json反序列化为对象RedisData redisData = JSONUtil.toBean(json, RedisData.class);R r = JSONUtil.toBean((JSONObject) redisData.getData(), type);LocalDateTime expireTime = redisData.getExpireTime();//4. 判断是否过期if (expireTime.isAfter(LocalDateTime.now())) {//5. 未过期,直接返回商铺信息return r;}//6. 过期,尝试获取互斥锁String lockKey = LOCK_SHOP_KEY + id;boolean flag = tryLock(lockKey);//7. 获取到了锁if (flag) {//8. 开启独立线程CACHE_REBUILD_EXECUTOR.submit(() -> {try {R tmp = dbFallback.apply(id);this.setWithLogicExpire(key, tmp, time, timeUnit);} catch (Exception e) {throw new RuntimeException(e);} finally {unlock(lockKey);}});//9. 直接返回商铺信息return r;}//10. 未获取到锁,直接返回商铺信息return r;
}
方法5:根据指定的Key查询缓存,并反序列化为指定类型,需要利用互斥锁解决缓存击穿问题
public <R, ID> R queryWithMutex(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit timeUnit) {//先从Redis中查,这里的常量值是固定的前缀 + 店铺idString key = keyPrefix + id;String json = stringRedisTemplate.opsForValue().get(key);//如果不为空(查询到了),则转为Shop类型直接返回if (StrUtil.isNotBlank(json)) {return JSONUtil.toBean(json, type);}if (json != null) {return null;}R r = null;String lockKey = LOCK_SHOP_KEY + id;try {//否则去数据库中查boolean flag = tryLock(lockKey);if (!flag) {Thread.sleep(50);return queryWithMutex(keyPrefix, id, type, dbFallback, time, timeUnit);}r = dbFallback.apply(id);//查不到,则将空值写入Redisif (r == null) {stringRedisTemplate.opsForValue().set(key, "", CACHE_NULL_TTL, TimeUnit.MINUTES);return null;}//并存入redis,设置TTLthis.set(key, r, time, timeUnit);} catch (InterruptedException e) {throw new RuntimeException(e);} finally {unlock(lockKey);}return r;
}