【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);
    }
}
相关推荐
ye9021 分钟前
银河麒麟V10服务器版 + openGuass + JDK +Tomcat
java·开发语言·tomcat
武昌库里写JAVA23 分钟前
Oracle如何使用序列 Oracle序列使用教程
java·开发语言·spring boot·学习·课程设计
做题不NG1 小时前
大模型应用开发-LangChain4j
java
今天背单词了吗9801 小时前
算法学习笔记:7.Dijkstra 算法——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·开发语言·数据结构·笔记·算法
CHENWENFEIc2 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试
高兴达2 小时前
RPC--Netty客户端实现
java·spring·rpc
重庆小透明2 小时前
力扣刷题记录【1】146.LRU缓存
java·后端·学习·算法·leetcode·缓存
lang201509283 小时前
Reactor操作符的共享与复用
java
TTc_3 小时前
@Transactional事务注解的批量回滚机制
java·事务
wei_shuo4 小时前
飞算 JavaAI 开发助手:深度学习驱动下的 Java 全链路智能开发新范式
java·开发语言·飞算javaai