【Redis】Spring/SpringBoot 操作 Redis Java客户端

目录

  • [操作 Redis Java客户端](#操作 Redis Java客户端)
  • [SpringBoot 操作Redis 步骤](#SpringBoot 操作Redis 步骤)

操作 Redis Java客户端

1.Jedis

2.Lettuce(主流) <-Spring Data Redis

SpringBoot 操作Redis 步骤

1.添加Redis 驱动依赖

2.设置Redis 连接信息

java 复制代码
spring.redis.database=0
spring.redis.port=6379
spring.redis.host=127.0.0.1
# 可省略
spring.redis.lettuce.pool.min-idle=5
spring.redis.lettuce.pool.max-idle=10
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=1ms
spring.redis.lettuce.shutdown-timeout=100ms

3.根据Redis API 操作Redis

java 复制代码
package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class TestController {

    private final String _REDIS_KEY = "myapplication_test";

    // Spring Boot 自动装配机制
    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/setval")
    public void setVal(String val) {
        redisTemplate.opsForValue() // 得到操作 redis 的类型
                .set(_REDIS_KEY, val,1000, TimeUnit.SECONDS);
    }
    @RequestMapping("/getval")
    public String getValue() {
        return (String) redisTemplate.opsForValue()
                .get(_REDIS_KEY);
    }
}
相关推荐
熏鱼的小迷弟Liu22 分钟前
【Redis】Redis Zset实现原理:跳表+哈希表的精妙设计
数据库·redis·散列表
淋一遍下雨天1 小时前
Spark Streaming核心编程总结(四)
java·开发语言·数据库
琢磨先生David1 小时前
重构数字信任基石:Java 24 网络安全特性的全维度革新与未来防御体系构建
java·web安全·密码学
程序修理员1 小时前
技能点总结
java
Jennifer33K2 小时前
报错_NoSuchMethodException: cn.mvc.entity.User.<init>()
java
爱吃烤鸡翅的酸菜鱼2 小时前
【SpringMVC】概念引入与连接
java·开发语言·mysql
碎梦归途2 小时前
23种设计模式-行为型模式之策略模式(Java版本)
java·开发语言·jvm·设计模式·策略模式·行为型模式
三个蔡2 小时前
Java求职者面试:从Spring Boot到微服务的技术深度探索
java·大数据·spring boot·微服务·kubernetes
sniper_fandc2 小时前
JVM(Java虚拟机)详解
java·开发语言·jvm
小鸡脚来咯2 小时前
SpringBoot 常用注解通俗解释
java·spring boot·后端