利用redis分布式锁 setnx 解决雪崩、穿透、击穿

本文探讨了Redis中出现的雪崩、穿透和击穿问题,并通过使用setnx命令和悲观锁策略,结合Spring Data RedisTemplate实现了一种解决方案。通过代码示例详细展示了如何在高并发场景下保护后端数据库,防止因缓存失效导致的流量冲击。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

``先聊一下概念:

redis雪崩:redis中大面积的key同时过期,导致请求越过redis到mysql 数据库收到高流量冲击,挂掉

redis穿透:redis 和mysql 都不存在查询的key,导致请求越过redis到mysql 数据库收到高流量冲击,挂掉

redis 击穿:redis 中key过期,,导致请求越过redis到mysql 数据库收到高流量冲击,挂掉

如何解决可以利用setnx 命令,下面的demo 用的是springData RedisTemplate

package com.test.one.springboottestpne.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
* setNx 悲观锁 解决雪崩 击穿 穿透问题
* 事物+监听 解决秒杀 库存卖超问题。
*/
@Component
public class RedisLockTest extends Thread {
@Override  
public void run() {  
    try {  
        setNx();  
    } catch (InterruptedException e) {  
        e.printStackTrace();  
    }  
}

@Autowired  
StringRedisTemplate stringRedisTemplate;  
public String setNx() throws InterruptedException {  
    System.out.println(Thread.currentThread().getName()+"开始获取缓存数据");  
    String key = stringRedisTemplate.opsForValue().get("key");  
    if (StringUtils.isNotBlank(key)){  
        System.out.println(Thread.currentThread().getName()+"开始获取缓存数据成功,返回");  
        return  key;  
    }  
    System.out.println(Thread.currentThread().getName()+"获取缓存数据失败");  
    Boolean aBoolean = stringRedisTemplate.opsForValue().setIfAbsent("1", "1");

    if (aBoolean){  
        System.out.println(Thread.currentThread().getName()+"拿到锁成功,重新设置缓存");  
        stringRedisTemplate.expire("1",1, TimeUnit.MINUTES);  
        //处理业务逻辑  
        key = stringRedisTemplate.opsForValue().get("key");  
        if (StringUtils.isNotBlank(key)){  
            System.out.println(Thread.currentThread().getName()+"---------返回缓存数据");  
            stringRedisTemplate.delete("1");  
            return  stringRedisTemplate.opsForValue().get("key");  
        }  
        //模拟查询数据库  
        Thread.sleep(2000);  
        stringRedisTemplate.opsForValue().set("key","11111111");  
        stringRedisTemplate.delete("1");  
        System.out.println(Thread.currentThread().getName()+"。。。。。。返回缓存数据");  
        return  stringRedisTemplate.opsForValue().get("key");  
    }else {  
        Thread.sleep(500);  
        setNx();  
    }  
    return null;  
}  
package com.test.one.springboottestpne;


import com.test.one.springboottestpne.utils.RedisLockTest;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


@RunWith(SpringRunner.class)
@SpringBootTest(value = {"classpath:application.properties"})
public class RedisTest {

        @Autowired
        RedisLockTest redisLockTest;


    @Test
    public void redisLock() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        for (int i = 0;i<100;i++){
            executorService.submit(redisLockTest);
        }
        Thread.sleep(1000000);

    }



}

 

 

}`

转载于:https://my.oschina.net/u/2325281/blog/2878663

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值