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";
    }
}
相关推荐
wefg16 小时前
【Redis】初识 Redis
数据库·redis·缓存
周之瑞8 小时前
技术手记|Redis 与数据库事务不在一个世界:afterCommit、显式补偿还是 Canal?
redis
hacker_LeeFei8 小时前
Windows 下配置 Redis 8.10.2 开机自启:一次踩坑全记录
数据库·windows·redis
zl_dfq9 小时前
Redis 之 【持久化 与 事务】
数据库·redis
白露与泡影12 小时前
Redis 的“单线程”与“多线程”:一场关于性能与简洁的权衡
数据库·redis·php
行百里er15 小时前
Redis 性能优化——内存、慢查询、Big Key 与 Hot Key
redis·后端
yexianglunbai16 小时前
Redis 缓存详解:从原理到实战
数据库·redis·缓存
逃逸线LOF16 小时前
Redis快速入门
数据库·redis·缓存
j7~17 小时前
【Redis初阶】(篇二)《一文吃透 Redis:特性、应用场景、版本演进与安装配置全解析》
数据库·redis·缓存·redis的特性·redis的主要应用场景·redis重要文件及其作用·安装并启动redis
JavaEdge.17 小时前
Redis 连接断开后的自动重连操作
spring boot·redis·后端·lettuce·tcp keepalive