SpringBoot--整合Redisson--方法/实例

原文网址:SpringBoot--整合Redisson--方法/实例_IT利刃出鞘的博客-CSDN博客

简介

        本文介绍SpringBoot整合Redisson的方法。

依赖

pom.xml

<dependency>
    <groupId>org.redisson</groupId>
    <artifactId>redisson-spring-boot-starter</artifactId>
    <version>3.16.6</version>
</dependency>

配置

详见 :SpringBoot--配置Redisson的方法_IT利刃出鞘的博客-CSDN博客

application.yml

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    # password:
    # database: 0     #指定数据库,默认为0
    # timeout: 3000   #连接超时时间,单位毫秒,默认为0。也可以这么写:3s
    # ssl: false      # 是否启用SSL连接,默认false
    # pool: #连接池配置
    #   max-active: 8 #最大活跃连接数,默认8个。
    #   max-idle: 8   #最大空闲连接数,默认8个。
    #   max-wait: -1  #获取连接的最大等待时间,默认-1,表示无限制,单位毫秒。
    #                 #默认值可能会因为获取不到连接,导致事务无法提交,数据库被锁,大量线程处于等待状态的情况。
    #   min-idle: 0   #最小空闲连接数,默认0。
    # sentinel:
    #   master: myMaster             #哨兵master
    #   nodes: host1:port,host2:port #哨兵节点
    # cluster:
    #   max-redirects:               # 集群模式下,集群最大转发的数量
    #   nodes: host1:port,host2:port # 集群节点

使用

下边是一个简单示例,更多用法见:Redisson--使用/教程/实例_IT利刃出鞘的博客-CSDN博客

package com.knife.test;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
    @Autowired
    private RedissonClient redissonClient;

    @GetMapping("/test")
    public String test(){
        RBucket<Object> bucket = redissonClient.getBucket("key1");
        // 此处可以是任意对象,不仅仅是字符串
        bucket.set("value1");
        
        String str1 = (String) rBucket.get();
        System.out.println(str1);// 此处会打印出:value1

        // 删除"key1"这一项数据
        bucket.delete();
        
        return "success";
    }
}

猜你喜欢

转载自blog.csdn.net/feiying0canglang/article/details/126526971