【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);
    }
}
相关推荐
葡萄成熟时 !8 小时前
JAVA 常用API学习笔记
java·笔记·学习
Rain的Java大神之路9 小时前
介绍一下分布式事务
java·分布式·后端·spring·spring cloud·架构·springcloud
吴声子夜歌9 小时前
Java面试题——基础(一)
java·开发语言
南城以南溫暖如初14710 小时前
外卖CPS实战指南:从技术选型到运营落地全流程解析
java·spring boot·mysql·架构·vue
傻啦嘿哟10 小时前
英雄联盟皮肤爬虫:爬取全皮肤价格与特效,做比价工具
java·c++·爬虫
码匠许师傅11 小时前
【C++ 面试真题】24. 聊聊 C++ 的时间日期处理
java·c++·面试
mqiqe12 小时前
AgentScope Java 2.0 Agent 状态存储(AgentStateStore)深度解析:构建可恢复、可扩展的智能体运行时
java·运维·网络
zhifou12345612 小时前
java 17升级安装
java·开发语言
用户37215742613512 小时前
如何使用 Java 将 Markdown 转换为 PDF(含自定义设置)
java
用户37215742613513 小时前
如何使用 Java 在 Word 文档中添加和删除水印:分步指南
java