【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);
    }
}
相关推荐
Li_7695324 小时前
Redis 进阶(一) —— 持久化
redis
Nonoas4 小时前
动态代理:发布订阅的高级玩法
java·ide·intellij-idea
程序员-周李斌5 小时前
Java 死锁
java·开发语言·后端
皮皮林5515 小时前
Prometheus+Grafana,打造强大的监控与可视化平台
java
JasmineWr5 小时前
CompletableFuture相关问题
java·开发语言
零雲6 小时前
java面试:知道java的反射机制吗
java·开发语言·面试
Psycho_MrZhang6 小时前
Redis 设计思想总结
数据库·redis·缓存
java1234_小锋6 小时前
Java进程占用的内存有哪些部分?
java
sxlishaobin6 小时前
Spring Bean生命周期详解
java·后端·spring
曹牧7 小时前
Java:Assert.isTrue()
java·前端·数据库