SpringBoot 集成Redis

本文介绍了如何在SpringBoot项目中集成Redis,包括添加依赖、配置连接参数,以及使用StringRedisTemplate进行手动操作和通过注解实现缓存控制的功能。
摘要由CSDN通过智能技术生成

SpringBoot 集成Redis

  1. 添加 redis 依赖
    在这里插入图片描述
    或将以下配置添加到 pom.xml 中:

<dependency>
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

  1. 配置 redis
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
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>> 开启缓存


@SpringBootApplication
@EnableCaching

<<2>> 注解操作 redis

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";
    }
}
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中集成Redis可以通过以下步骤实现: 1. 引入spring-boot-starter-data-redis依赖。在项目的pom.xml文件中,添加以下依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 这将自动引入与Redis集成所需的依赖项。 2. 在Spring Boot的核心配置文件application.properties中配置Redis连接信息。在该文件中添加以下配置项: ``` spring.redis.host=127.0.0.1 spring.redis.port=6379 spring.redis.password=123456 ``` 根据你的实际情况,将host、port和password替换为相应的值。这些配置将用于建立与Redis服务器的连接。 通过以上步骤,你就成功地在Spring Boot应用程序中集成Redis。现在,你可以使用Spring Data Redis的API来访问和操作Redis数据库。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot集成redis](https://blog.csdn.net/qq_43512320/article/details/122684865)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [springboot集成Redis](https://blog.csdn.net/m0_54853420/article/details/126515971)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值