redis工具类(缓存的设置,缓存击穿,穿透)

bash 复制代码
#maven依赖
		<!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.12</version>
        </dependency>
java 复制代码
import cn.hutool.core.date.DateTime;
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 com.sun.org.apache.bcel.internal.generic.NEW;
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;

    /**
     * 添加数据到redis
     *
     * @param key   名称
     * @param value 内容
     * @param time  有效时间
     * @param unit  时间单位
     */
    public void set(String key, Object value, Long time, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(value), time, unit);
    }

    /**
     * 设置逻辑过期添加数据到redis
     *
     * @param key   名称
     * @param value 内容
     * @param time  超时时间
     * @param unit  超时时间单位
     */
    public void setWithLogicalExpire(String key, Object value, Long time, TimeUnit unit) {
        //设置逻辑过期时间
        RedisData redisData = new RedisData();
        redisData.setData(value);
        redisData.setExpireTime(LocalDateTime.now().plusSeconds(unit.toSeconds(time)));
        //写入redis
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(redisData));
    }

    /**
     * 缓存穿透
     * @param keyPrefix key的前缀
     * @param id id
     * @param type 返回类型
     * @param dbFallback 数据库查询
     * @param time 时间
     * @param unit 时间单位
     * @param <R> 返回值
     * @param <ID> ID
     * @return
     */
    public <R, ID> R queryWithPassThrough(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit unit) {
        String key = keyPrefix + id;
        //1.从redis查询商品缓存
        String json = stringRedisTemplate.opsForValue().get(key);
        //2.判断是否存在
        if (StrUtil.isNotBlank(json)) {
            //3。存在直接返回
            R content = JSONUtil.toBean(json, type);
            return content;
        }
        //判断是否是空值(上面已经判断了数据存在的情况,所以这里直接使用!=null就能查出空值)
        if (json != null) {
            return null;
        }
        //4.不存在,查询数据库
        R r = dbFallback.apply(id);
        //5.数据库不存在返回错误
        if (r == null) {
            //为了防止缓存穿透,将空值写入redis
            stringRedisTemplate.opsForValue().set(key, "", RedisConstants.CACHE_NULL_TTL, TimeUnit.MINUTES);
            //返回
            return null;
        }
        //6。存在写入redis
        stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(r), time, unit);
        //7.返回
        return r;
    }

    /**
     * 缓存击穿
     */
    private static final ExecutorService CACHE_THREAD_POOL = Executors.newFixedThreadPool(10);

    public <R,ID> R queryWithLogicExpiredTime(String keyPrefix, ID id, Class<R> type, Function<ID, R> dbFallback, Long time, TimeUnit unit) {
        String key = keyPrefix + id;
        //1.从redis查询商品缓存
        String json = stringRedisTemplate.opsForValue().get(key);
        //2.判断是否命中(未命中返回)
        if (StrUtil.isBlank(json)) {
            return null;
        }
        //2.1命中-判断是否过期
        RedisData redisData = JSONUtil.toBean(json, 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;
        try {
            //2.2过期-获取互斥锁
            boolean isLock = tryLock(lockKey);
            //3.判断锁是否拿到
            if (isLock) {
                //4.是-开启独立线程
                CACHE_THREAD_POOL.submit(() -> {
                    //重建缓存2步走,先查数据库,再写redis
                    //查询数据库
                    R r1 = dbFallback.apply(id);
                    //写入redis
                    this.setWithLogicalExpire(key,r1,time,unit);
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            //4.2释放锁
            delLock(lockKey);
        }
        //5.返回数据
        return r;
    }

    //利用redis中的setnx来设置锁
    private boolean tryLock(String key) {
        Boolean flag = stringRedisTemplate.opsForValue().setIfAbsent(key, "1", 10L, TimeUnit.SECONDS);
        //自动拆箱有可能返回空值,所以用工具类
        return BooleanUtil.isTrue(flag);
    }

    //删除锁
    private void delLock(String key) {
        stringRedisTemplate.delete(key);
    }
}
相关推荐
两点王爷14 小时前
PostgreSQL 常用 SQL 语句与 GIS 相关函数详解
数据库·后端
两点王爷15 小时前
PostgreSQL 好用又独特的特性与空间函数
数据库
梦帮科技15 小时前
AI 音乐产品的发布工程:验证门、数据发布、回滚与生产运维纪律
数据结构·数据库·架构·node.js·音视频·动态规划·推荐算法
TDengine (老段)16 小时前
TDengine 常见问题 TOP3
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
HanhahnaH16 小时前
Redis单线程和Tair多线程架构设计对比
分布式·缓存
螺蛳粉 螺蛳粉19 小时前
MySQL 分布式集群系列 · 第五篇——全方位对比:NDB、MGR、主从复制、分库分表怎么选?
数据库·分布式·mysql
wdfk_prog21 小时前
ROS教程07:从 ros::start() 顺着源码读懂 Master、XML-RPC 与 Topic 注册发现
运维·缓存·docker·容器·ros
Escalating_xu1 天前
【内存管理发展史】从 8086 实模式到分页:CPU 如何把虚拟地址变成物理地址
linux·缓存
wdfk_prog1 天前
用 Git Submodule + Sparse Checkout 管理 RT-Thread:内核、BSP、第三方库与业务代码分层实践
运维·缓存·docker·容器·ros
努力努力再努力wz1 天前
【Docker入门系列】镜像为什么能复用?一文吃透 Docker Image、Registry、运行时架构与常用命令
缓存·docker·容器