解决缓存穿透的代码[最佳实践版]

介绍缓存穿透:

缓存穿透说简单点就是大量请求的 key 是不合理的,根本不存在于缓存中,也不存在于数据库中 。这就导致这些请求直接到了数据库上,根本没有经过缓存这一层,对数据库造成了巨大的压力,可能直接就被这么多请求弄宕机了。(key对应的数据在数据源并不存在,每次针对此key的请求从缓存获取不到,请求都会到数据源,从而可能会压垮数据源(mysql),若黑客利用此漏洞进行攻击可能压垮数据库。)

解决方案:

  • 1)简单解决法:缓存无效 key(缓存空对象结果,但注意超时时间不能过长)
    • 优点:操作简单,维护方便。缺点消耗内存,可能会发生数据短期不一致的问题
  • 2)系统解决法:布隆过滤器(将所有存在的数据都缓存,那么不存在的数据就会被拦截(存在误判5%左右))
    • 误判率:数组越小,误判的概率越大,数组越大误判率越小,但是同时带来了更多的内存消耗
    • 布隆过滤器的实现方案(1.Redisson,2.Guava)
    • 优点:内存占用较少,没有多余的key。缺点:实现复杂,存在误判可能
    • 介绍一下布隆过滤器?
      • 布隆过滤器主要是用于检索一个元素是否在一个集合中,我们当时使用的是redisson实现的布隆过滤器,它的底层主要是先去初始化一个较大的数组,里面存放的二进制,一开始都是0,当一个key来了之后经过3次hash计算,模于数组长度找到数据的下表然后表数组中的0改未1,这样的话,三个数组的位置就能表明一个key的存在,查找过程也是一样的,但是布隆过滤器也可能会产生一定的误判,我们一般可能设置这个误判率,一般在5%左右,不至于高并发情况下压倒数据库。
  • 3)接口限流
    • 根据用户或者 IP 对接口进行限流,对于异常频繁的访问行为,还可以采取黑名单机制,例如将异常 IP 列入黑名单。
  • 4)防止非法请求:检查非法请求,封禁其 IP 以及账号,防止它再次为非作歹。。

实践解决:

下方的代码是一个根据id查询帖子的代码,这里将对其使用redis进行改造,并给出一些能够预防缓存穿透的方案具体代码

/**
 * 根据 id 获取
 *
 * @param id
 * @return
 */
@GetMapping("/get/vo")
public BaseResponse<PostVO> getPostVOById(long id, HttpServletRequest request) {
    if (id <= 0) {
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = postService.getById(id);
    if (post == null) {
        throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
    }
    return ResultUtils.success(postService.getPostVO(post, request));
}

使用Redis添加基本缓存

@GetMapping("/get/vo")
public BaseResponse<PostVO> getPostVOById(long id, HttpServletRequest request) {
    if (id <= 0) {
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = (Post) redisTemplate.opsForValue().get(POST_BY_ID + id);
    if (post == null){
        post = postService.getById(id);
        if (post != null){
            redisTemplate.opsForValue().set(POST_BY_ID + id, post, POST_EXPIRE_TIME, TimeUnit.MINUTES );
        }
    }
    if (post == null) {
        throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
    }
    return ResultUtils.success(postService.getPostVO(post, request));
}

解决缓存穿透的方案:

1.添加空缓存

/**
 * 根据 id 获取
 *
 * @param id
 */
@GetMapping("/get/vo")
public BaseResponse<PostVO> getPostVOById(long id, HttpServletRequest request) {
    if (id <= 0) {
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }
    Post post = (Post) redisTemplate.opsForValue().get(POST_BY_ID + id);
    if (post == null){
        post = postService.getById(id);
        if (post != null){
            redisTemplate.opsForValue().set(POST_BY_ID + id, post, POST_EXPIRE_TIME, TimeUnit.MINUTES );
        }
    }
    if (post == null) {
        redisTemplate.opsForValue().set(POST_BY_ID + id, null, NULL_POST_EXPIRE_TIME, TimeUnit.MINUTES );
        throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
    }
    return ResultUtils.success(postService.getPostVO(post, request));
}

2.布隆过滤器防止缓存穿透

布隆过滤器是一种空间效率极高的概率型数据结构,用于判断一个元素是否在一个集合中。它可能会误判(即存在假阳性),但不会漏判。通过使用布隆过滤器,可以有效防止缓存穿透问题。

步骤

引入布隆过滤器库:

常用的布隆过滤器库有 Google Guava 和 Apache Commons。这里以 Google Guava 为例。

创建布隆过滤器:

在启动时初始化布隆过滤器,并将其存储在静态变量或单例中。

修改代码逻辑:

在从 Redis 获取数据之前,先检查布隆过滤器。

如果布隆过滤器中不存在该 id,则直接返回 NOT_FOUND_ERROR。

如果布隆过滤器中存在该 id,则继续从 Redis 或数据库中获取数据。

示例代码

引入依赖

在 pom.xml 中添加 Guava 依赖:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.0.1-jre</version>
</dependency>

初始化布隆过滤器

在启动类或配置类中初始化布隆过滤器

import com.google.common.hash.Funnel;
import com.google.common.hash.PrimitiveSink;
import com.google.common.math.IntMath;

import java.math.RoundingMode;

public class BloomFilterConfig {

    private static final int EXPECTED_INSERTIONS = 1000000; // 预期插入的数量
    private static final double FPP = 0.01; // 误判率

    private static final Funnel<Long> FUNNEL = new Funnel<Long>() {
        @Override
        public void funnel(Long from, PrimitiveSink into) {
            into.putLong(from);
        }
    };

    private static final int numBits = IntMath.ceilingNextPowerOfTwo(
            (int) (-EXPECTED_INSERTIONS * Math.log(FPP) / (Math.log(2) * Math.log(2))),
            RoundingMode.CEILING
    );

    private static final int numHashFunctions = (int) (numBits * Math.log(2) / -EXPECTED_INSERTIONS);

    private static final com.google.common.hash.BloomFilter<Long> bloomFilter =
            com.google.common.hash.BloomFilter.create(FUNNEL, EXPECTED_INSERTIONS, FPP);

    public static boolean mightContain(long id) {
        return bloomFilter.mightContain(id);
    }

    public static void put(long id) {
        bloomFilter.put(id);
    }
}

修改控制器代码

在控制器中使用布隆过滤器

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class PostController {

    @GetMapping("/get/vo")
    public BaseResponse<PostVO> getPostVOById(@RequestParam long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 检查布隆过滤器
        if (!BloomFilterConfig.mightContain(id)) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        Post post = (Post) redisTemplate.opsForValue().get(POST_BY_ID + id);
        if (post == null) {
            post = postService.getById(id);
            if (post != null) {
                redisTemplate.opsForValue().set(POST_BY_ID + id, post, POST_EXPIRE_TIME, TimeUnit.MINUTES);
            } else {
                // 如果数据库中也不存在,将 id 加入布隆过滤器
                BloomFilterConfig.put(id);
            }
        }
        if (post == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(postService.getPostVO(post, request));
    }
}

解释

布隆过滤器初始化:

EXPECTED_INSERTIONS:预期插入的数量。

FPP:误判率。

FUNNEL:定义如何将 long 类型的数据转换为哈希值。

bloomFilter:创建布隆过滤器实例。

控制器逻辑:

在从 Redis 获取数据之前,先检查布隆过滤器。

如果布隆过滤器中不存在该 id,则直接返回 NOT_FOUND_ERROR。

如果布隆过滤器中存在该 id,则继续从 Redis 或数据库中获取数据。

如果数据库中也不存在该 id,将 id 加入布隆过滤器,以防止后续的缓存穿透。

通过这种方式,可以有效防止缓存穿透问题,提高系统的性能和稳定性。

3.添加限流操作

为了防止缓存穿透,可以在获取缓存和数据库之前添加一个限流机制。

private static final long POST_EXPIRE_TIME = 60; // 缓存有效时间
private static final long NULL_POST_EXPIRE_TIME = 5; // 未找到的缓存有效时间

@Autowired
private RateLimiter rateLimiter; // 限流器
/**
 * 根据 id 获取
 *
 * @param id
 */
/**
 * 根据 id 获取
 *
 * @param id
 */
@GetMapping("/get/vo")
public BaseResponse<PostVO> getPostVOById(@RequestParam long id, HttpServletRequest request) {
    if (id <= 0) {
        throw new BusinessException(ErrorCode.PARAMS_ERROR);
    }

    // 限流检查
    if (!rateLimiter.tryAcquire()) {
        throw new BusinessException(ErrorCode.RATE_LIMIT_EXCEEDED);
    }

    Post post = (Post) redisTemplate.opsForValue().get(POST_BY_ID + id);
    if (post == null) {
        post = postService.getById(id);
        if (post != null) {
            redisTemplate.opsForValue().set(POST_BY_ID + id, post, POST_EXPIRE_TIME, TimeUnit.MINUTES);
        } else {
            redisTemplate.opsForValue().set(POST_BY_ID + id, null, NULL_POST_EXPIRE_TIME, TimeUnit.MINUTES);
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
    }
    return ResultUtils.success(postService.getPostVO(post, request));
}

主要修改点:

引入限流器:在 PostController 中添加了一个 RateLimiter 对象。

限流检查:在获取缓存和数据库之前,先进行限流检查。如果限流失败,则抛出 BusinessException 异常,提示用户请求频率过高。

依赖项:

RateLimiter:可以使用 Guava 提供的 RateLimiter 或者其他限流库(如 Resilience4j)。

BusinessException:自定义的业务异常类,用于处理各种业务错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值