【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);
    }
}
相关推荐
qfljg3 小时前
OpenRewrite 实战指南
java
Sylvia33.3 小时前
LOL实时数据接入深度解析:从WebSocket到完整数据模型
java·网络·python·websocket·网络协议
2601_962071578 小时前
【Java报错已解决】org.springframework.beans.factory.BeanCreationException
java·开发语言
zww89491119 小时前
酒馆预约系统开发实战:从需求分析到上线全流程指南
java·eclipse
莫得感情 o10 小时前
Redis 05 · 持久化:RDB 与 AOF 怎么保证数据不丢
redis·缓存
zww894911111 小时前
匿名树洞系统开发实战:从需求分析到部署指南
java·eclipse
curd_boy11 小时前
【Redis】Redis从缓存到AI向量平台
人工智能·redis·缓存
莫得感情 o12 小时前
Redis 02 · 数据结构:String 到 Stream 怎么选
redis
黑马程序员毕设12 小时前
基于Java的医院药品管理系统的优化设计与实现
java·开发语言·spring boot·小程序·架构·课程设计·毕设
木井巳13 小时前
【BFS/DFS 解决 FloodFill 算法】太平洋大西洋水流问题
java·算法·leetcode·深度优先·广度优先·宽度优先·推荐算法