054 redisson

news/2025/2/24 10:05:37

文章目录

    • 使用Redisson演示可重入锁
    • 读写锁
    • 信号量
    • 闭锁
    • 获取三级分类redisson分布式锁

package com.xd.cubemall.product.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRedissonConfig {

    /**
     * 注册RedissonClient对象
     */
    @Bean(destroyMethod="shutdown")
    RedissonClient redissonClient(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        RedissonClient redissonClient = Redisson.create(config);

        return redissonClient;

    }

}

使用Redisson演示可重入锁

    /**
     * 使用Redisson演示可重入锁
     * @return
     */
    @ResponseBody
    @GetMapping("/hello")
    public String hello(){
        //获取一把锁
        RLock lock = redissonClient.getLock("my-lock");
        //加锁
        lock.lock();
        try{
            System.out.println("加锁成功,执行业务。。。" + Thread.currentThread().getId());
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //解锁
            System.out.println("解锁。。。" + Thread.currentThread().getId());
            lock.unlock();
        }


        return "hello";
    }

读写锁

    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        //获取一把锁
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        // 加 写锁
        String s = "";
        RLock writeLock = lock.writeLock();

        try{
            //1.改数据加 写锁,读数据加 读锁
            writeLock.lock();
            System.out.println("写锁加锁成功..." + Thread.currentThread().getId());
            s = UUID.randomUUID().toString();
            Thread.sleep(15000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 释放锁
            writeLock.unlock();
            System.out.println("写锁。。释放。。"+Thread.currentThread().getId());

        }
        return s;
    }


    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw-lock");
        String s = "";
        //加 读锁
        RLock readLock = lock.readLock();
        readLock.lock();
        try {
            System.out.println("读锁...加锁成功..."+Thread.currentThread().getId());
            s = redisTemplate.opsForValue().get("writeValue");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            readLock.unlock();
            System.out.println("读锁。。释放。。。"+Thread.currentThread().getId());

        }
        return s;
    }

信号量

    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        //当车位减少为0时,还想获取车位,必须要等go()调用时,释放一个车位,才能进行执行
        semaphore.acquire();//占有一个车位

        return "ok";
    }

    @GetMapping("/go")
    @ResponseBody
    public String go() {
        RSemaphore semaphore = redissonClient.getSemaphore("park");
        semaphore.release();
        return "ok";
    }

闭锁

    @ResponseBody
    @GetMapping("/lockDoor")
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        door.await();//等待其他闭锁都完成
        return "关门了";
    }


    @ResponseBody
    @GetMapping("/gogogo/{id}")
    public String gogogo(@PathVariable("id") Long id) {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();//计数-1
        return id + "桌的人都走了。。。";
    }

redisson_157">获取三级分类redisson分布式锁


    @Autowired
    private RedissonClient redissonClient;
    /**
     * 获取三级分类(redisson分布式锁)
     * @return
     */

    public List<CategoryVo> getCategoryJsonFromWithRedissonLock() {

        String uuid = UUID.randomUUID().toString();
        //1.占分布式
        RLock lock = redissonClient.getLock("CategoryJson-lock");

        lock.lock();
        List<CategoryVo> dataFromDb = null;
        try {
            dataFromDb = getDataFromDb();
        } finally {
            lock.unlock();
        }

        return dataFromDb;




    }

http://www.niftyadmin.cn/n/5864198.html

相关文章

TCP初始化序列号为什么要不一样

区分不同的连接(包括一些历史连接)、确保数据的顺序性、防止重放攻击&#xff08;时间戳&#xff09; 初始化序列号 ISN M F(localhost, localport, remotehost, remoteport)。 M是一个32位的计时器&#xff0c;这个计时器每隔 4 微秒加1&#xff0c;循环一次4.55小时F 是一…

深入解析思科 IOS 路由交换设备缓冲区机制

目录 一、数据包缓冲管理 二、数据包缓冲池 Packet buffer pool 1. 静态缓冲池 vs 动态缓冲池 2. 公共池 vs 私有池 三、系统缓冲 System Buffers 1. Buffer elements 2. Public buffer pools 四、缓冲区输出 & 丢包计数详解 1. Show buffers 2. Show interfa…

uni-app集成sqlite

Sqlite SQLite 是一种轻量级的关系型数据库管理系统&#xff08;RDBMS&#xff09;&#xff0c;广泛应用于各种应用程序中&#xff0c;特别是那些需要嵌入式数据库解决方案的场景。它不需要单独的服务器进程或系统配置&#xff0c;所有数据都存储在一个单一的普通磁盘文件中&am…

goredis常见基础命令

基本操作 //删除键 exists,err: rdb.Exists(ctx,"key").Result() if err!nil{panic(err) } if exists>0{err rdb.Del(ctx,"key").Err()if err!nil{panic(err)} }string类型 //设置一个键值对 //0表示没有过期时间 err:rdb.Set(ctx,"key1",…

Layer2 扩容解决方案详解

Layer2 扩容解决方案详解 &#x1f504; 1. Layer2 基础概念 1.1 什么是 Layer2&#xff1f; Layer2 是建立在以太坊主网&#xff08;Layer1&#xff09;之上的扩容解决方案&#xff0c;它&#xff1a; 继承以太坊的安全性提供更高的交易吞吐量降低交易费用保持去中心化特性…

hot100_300. 最长递增子序列

hot100_300. 最长递增子序列 思路动态规划 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 […

细说STM32F407单片机2个ADC使用DMA同步采集各自的1个输入通道的方法

目录 一、示例说明 二、工程配置 1、RCC、DEBUG、CodeGenerator 2、USART6 3、TIM3 &#xff08;1&#xff09;Mode &#xff08;2&#xff09;参数设置 &#xff08;3&#xff09; TRGO &#xff08;4&#xff09;ADC1_IN0 1&#xff09;ADCs_Common_Settings 2&a…

nextjs项目搭建——头部导航

Header.tsx 在src/app/component路径下&#xff0c;创建Header.tsx use client;import Link from next/link; import { usePathname } from next/navigation; import Logo from ./Logo;const Header () > {const pathname usePathname();const navItems [{ label: 首页…