【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);
    }
}
相关推荐
movie__movie4 分钟前
秒杀库存扣减可以用redis原子自增么
数据库·redis·缓存
向着光芒的女孩36 分钟前
【IDEA】关不了的Proxy Authentication弹框探索过程
java·ide·intellij-idea
Filotimo_1 小时前
Spring Boot 整合 JdbcTemplate(持久层)
java·spring boot·后端
李慕婉学姐1 小时前
【开题答辩过程】以《“饭否”食材搭配指南小程序的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring·小程序
abments3 小时前
pgsql timestamp without time zone > character varying解决方案
java
sanggou3 小时前
大数据量查询处理方案 - 内存优化与高效展示
java
没有bug.的程序员3 小时前
Java 字节码:看懂 JVM 的“机器语言“
java·jvm·python·spring·微服务
白露与泡影3 小时前
2025年BAT面试题汇总:JVM+Spring+Dubbo+Redis+并发编程
jvm·spring·dubbo
-大头.3 小时前
深入理解 Java 内存区域与 JVM 运行机制
java·jvm
没有bug.的程序员3 小时前
JVM 整体架构:一套虚拟机的心脏与血管
java·jvm·spring boot·spring cloud·架构