SpringBoot使用Hutool进行最简单的连接Redis进行缓存操作

前言:一直想用一个简单的连接Redis的Java工具,那就出现了Jedis,当我还没开始使用Jedis的时候,就听说了Hutool发现Hutool的使配置连接NoSql的Redis缓存变得简单(最起码是在我看来的)

原因:想用一个操作Redis不需要太多配置的工具类

实现

1:先导入Hutool依赖的坐标

        <!-- hutool-all模块 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.16</version>
        </dependency>

2:导入Jedis依赖的坐标

        <!-- jedis模块 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.3.2</version>
        </dependency>

3:导入redis.setting

#-------------------------------------------------------------------------------
# Redis客户端配置样例
# 每一个分组代表一个Redis实例
# 无分组的Pool配置为所有分组的共用配置,如果分组自己定义Pool配置,则覆盖共用配置
# 池配置来自于:https://www.cnblogs.com/jklk/p/7095067.html
#-------------------------------------------------------------------------------

#----- 默认(公有)配置
# 地址,默认localhost
host = localhost
# 端口,默认6379
port = 6379
# 超时,默认2000
timeout = 2000
# 连接超时,默认timeout
connectionTimeout = 2000
# 读取超时,默认timeout
soTimeout = 2000
# 密码,默认无
# password =
# 数据库序号,默认0
database = 0
# 客户端名,默认"Hutool"
clientName = Hutool
# SSL连接,默认false
ssl = false;

#----- 自定义分组的连接
[custom]
# 地址,默认localhost
host = localhost
# 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
BlockWhenExhausted = true;
# 设置的逐出策略类名, 默认DefaultEvictionPolicy(当连接超过最大空闲时间,或连接数超过最大空闲连接数)
evictionPolicyClassName = org.apache.commons.pool2.impl.DefaultEvictionPolicy
# 是否启用pool的jmx管理功能, 默认true
jmxEnabled = true;
# 是否启用后进先出, 默认true
lifo = true;
# 最大空闲连接数, 默认8个
maxIdle = 8
# 最小空闲连接数, 默认0
minIdle = 0
# 最大连接数, 默认8个
maxTotal = 8
# 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1
maxWaitMillis = -1
# 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
minEvictableIdleTimeMillis = 1800000
# 每次逐出检查时 逐出的最大数目 如果为负数就是 : 1/abs(n), 默认3
numTestsPerEvictionRun = 3;
# 对象空闲多久后逐出, 当空闲时间>该值 且 空闲连接>最大空闲数 时直接逐出,不再根据MinEvictableIdleTimeMillis判断  (默认逐出策略)
SoftMinEvictableIdleTimeMillis = 1800000
# 在获取连接的时候检查有效性, 默认false
testOnBorrow = false
# 在空闲时检查有效性, 默认false
testWhileIdle = false
# 逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1
timeBetweenEvictionRunsMillis = -1

4:配置完这些之后创建一个JavaBean来帮你创建Jedis对象

import cn.hutool.db.nosql.redis.RedisDS;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

/**
 * Jedis 配置类
 */
@Configuration
public class JedisConfig {

    /**
     * 交由Spring来处理Bean对象创建Jedis
     * @return Jedis对象
     */
    @Bean
    public Jedis getJedis() {
        return RedisDS.create().getJedis();
    }
}

5:创建好了之后就可以进行测试了下面是测试代码(里面有我自己设置的返回数据类R),代码中使用了Knife4j接口文档

import com.xssq.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

@RestController
@RequestMapping("/TestJedis")
@Api(tags = "redis测试Controller")
public class TestJedis {
    @Autowired
    private Jedis jedis;

    @GetMapping("/setJedis")
    @ApiOperation("使用Jedis存入数据到redis")
    public R testSetJedis() {
        try {
            jedis.set("1212", "redis存入去除这么简单");
        } catch (Exception e) {
            System.out.println(e);
            return R.fail("redis存入失败");
        }
        return R.ok("存入成功");
    }

    @GetMapping("/getJedis")
    @ApiOperation("使用Jedis从redis获取数据")
    public R testGetJedis() {
        String s = null;
        try {
            s = jedis.get("1212");
        } catch (Exception e) {
            System.out.println(e);
            return R.fail("redis取出失败");
        }
        return R.ok(s,"获取成功");
    }

6:测试结果

{
  "code": 200,
  "msg": "操作成功",
  "data": "存入成功"
}
{
  "code": 200,
  "msg": "获取成功",
  "data": "redis存入去除这么简单"
}

7:测试成功

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

幸识SQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值