欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > Spring Boot整合Redis

Spring Boot整合Redis

2025/9/19 11:27:58 来源:https://blog.csdn.net/rit8432499/article/details/148106830  浏览:    关键词:Spring Boot整合Redis

前言

Redis是一种高性能的键值对存储系统,广泛应用于缓存、会话管理、消息队列等场景。Spring Boot作为一个简化Spring应用开发的框架,与Redis的整合能够有效提升应用的性能和响应速度。本文将详细介绍如何在Spring Boot项目中整合Redis。

环境准备

在开始整合之前,确保已经安装并运行了Redis服务器。可以通过以下命令检查Redis服务器是否运行:

redis-server
​

如果Redis没有安装,可以参考官方文档进行安装。

创建Spring Boot项目

首先,创建一个新的Spring Boot项目。在项目的 pom.xml文件中添加Spring Boot和Redis的依赖:

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

配置Redis

在 src/main/resources目录下的 application.properties文件中添加Redis的配置:

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password= # 如果有密码,填入对应的密码
​

创建Redis配置类

为了更灵活地管理Redis的连接和操作,可以创建一个配置类 RedisConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(redisConnectionFactory);return template;}@Beanpublic StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {return new StringRedisTemplate(redisConnectionFactory);}
}
​

使用RedisTemplate操作Redis

在Spring Boot中,可以使用 RedisTemplate或 StringRedisTemplate来操作Redis数据。以下是一个简单的示例,展示如何使用 RedisTemplate进行基本的CRUD操作。

创建服务类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class RedisService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void setValue(String key, Object value) {redisTemplate.opsForValue().set(key, value);}public Object getValue(String key) {return redisTemplate.opsForValue().get(key);}public void deleteValue(String key) {redisTemplate.delete(key);}public void setValueWithExpiration(String key, Object value, long timeout, TimeUnit unit) {redisTemplate.opsForValue().set(key, value, timeout, unit);}
}
​

创建控制器

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/redis")
public class RedisController {@Autowiredprivate RedisService redisService;@PostMapping("/set")public void setValue(@RequestParam String key, @RequestParam String value) {redisService.setValue(key, value);}@GetMapping("/get")public String getValue(@RequestParam String key) {return (String) redisService.getValue(key);}@DeleteMapping("/delete")public void deleteValue(@RequestParam String key) {redisService.deleteValue(key);}@PostMapping("/setWithExpiration")public void setValueWithExpiration(@RequestParam String key, @RequestParam String value,@RequestParam long timeout) {redisService.setValueWithExpiration(key, value, timeout, TimeUnit.SECONDS);}
}
​

启动应用

确保主类位于包的顶层,并包含 @SpringBootApplication注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RedisApplication {public static void main(String[] args) {SpringApplication.run(RedisApplication.class, args);}
}
​

启动应用程序,并通过以下示例测试Redis的操作:

  • 设置值:

    curl -X POST "http://localhost:8080/redis/set?key=testKey&value=testValue"
    ​
    
  • 获取值:

    curl "http://localhost:8080/redis/get?key=testKey"
    ​
    
  • 删除值:

    curl -X DELETE "http://localhost:8080/redis/delete?key=testKey"
    ​
    
  • 设置具有过期时间的值:

    curl -X POST "http://localhost:8080/redis/setWithExpiration?key=testKey&value=testValue&timeout=60"

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词