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";
    }
}
相关推荐
indexsunny19 小时前
互联网大厂Java面试实战:Spring Boot微服务在电商场景中的应用与挑战
java·spring boot·redis·微服务·kafka·spring security·电商
摇滚侠1 天前
阿里云安装的 Redis 在什么位置,如何找到 Redis 的安装位置
redis·阿里云·云计算
啦啦啦_99991 天前
Redis-2-queryFormat()方法
数据库·redis·缓存
forestsea1 天前
深入理解Redisson RLocalCachedMap:本地缓存过期策略全解析
redis·缓存·redisson
佛祖让我来巡山1 天前
Redis 为什么这么快?——「极速快递站」的故事
redis·redis为什么快?
啦啦啦_99991 天前
Redis-0-业务逻辑
数据库·redis·缓存
自不量力的A同学1 天前
Redisson 4.2.0 发布,官方推荐的 Redis 客户端
数据库·redis·缓存
fengxin_rou1 天前
[Redis从零到精通|第四篇]:缓存穿透、雪崩、击穿
java·redis·缓存·mybatis·idea·多线程
是阿楷啊1 天前
Java大厂面试场景:音视频场景中的Spring Boot与微服务实战
spring boot·redis·spring cloud·微服务·grafana·prometheus·java面试
笨蛋不要掉眼泪1 天前
Redis哨兵机制全解析:原理、配置与实战故障转移演示
java·数据库·redis·缓存·bootstrap