SpringBoot 集成Redis

SpringBoot 集成Redis

  1. 添加 redis 依赖

    或将以下配置添加到 pom.xml 中:
xml 复制代码
<dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 配置 redis
java 复制代码
spring.redis.database=0 
spring.redis.port=6379 
spring.redis.host=82.157.146.10
# 可省略
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
  1. ⼿动操作 redis
java 复制代码
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
 // 在 redis 存储数据
    @RequestMapping("/setrs")
    public String setRedis(String name, String value) { 
        stringRedisTemplate.opsForValue().set(name, value, 
                30, TimeUnit.SECONDS);
        return "Set redis success.";
    }
 // 读取 redis 中的数据
    @RequestMapping("/getrs")
    public String getRedis(String name) {
        Object valObj = stringRedisTemplate.opsForValue().get(name); 
        if (valObj != null) return valObj.toString();
        return "Null";
    }
}
  1. 注解操作 redis

<<1>> 开启缓存

java 复制代码
@SpringBootApplication
@EnableCaching

<<2>> 注解操作 redis

java 复制代码
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisAnnotationController {

    @Cacheable(value = "spring:cache", key = "#name") 
    @RequestMapping("/setrs")
    public String setRedis(String name, String value) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(value)) 
 {
            return "请先输⼊ name 和 value";
        }
        return value;
    }

    @RequestMapping("/uprs")
    @CachePut(value = "spring:cache", key = "#name")
    public String updateRedis(String name, String value) {
        if (!StringUtils.hasLength(name) || !StringUtils.hasLength(value)) 
 {
            return "请先输⼊ name 和 value";
        }
        return value;
    }

    @RequestMapping("/delrs")
    @CacheEvict(value = "spring:cache", key = "#name") 
    public String delUser(String name) {
        return "delete success";
    }
}
相关推荐
qq_455760852 小时前
redis - 持久化
数据库·redis·缓存
其美杰布-富贵-李2 小时前
Spring Data Redis + Redisson 学习笔记
redis·学习·spring
企鹅侠客2 小时前
第07章—实战应用篇:List命令详解与实战(下)
windows·redis·log4j·list
qq_455760853 小时前
redis - 事务
数据库·redis·缓存
蜂蜜黄油呀土豆3 小时前
Redis 事务与持久化机制深度解析:原子性保证、Lua 脚本与 AOF / RDB 实现原理
redis·aof·rdb·lua脚本
zs宝来了3 小时前
Spring Cloud+Redis+Kafka高并发电商微服务系统源码深度解读
spring boot·redis·spring cloud·微服务·kafka·高并发·电商
C++chaofan4 小时前
JUC 并发编程从入门到精通(超详细笔记 + 实战案例)
java·jvm·spring boot·redis·后端·并发·juc
万象.4 小时前
redis通用命令与数据结构
数据结构·数据库·redis
liuc031718 小时前
AI下调用redis并调用deepseek
数据库·redis·mybatis
遇见火星18 小时前
Redis主从复制深度解析:数据高可用与负载均衡的核心方案
数据库·redis·缓存·负载均衡