SpringBoot2.0中如何集成Redis数据库实现缓存管理
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,小编给大家分享一下SpringBoot2.0中如何集成Redis数据库实现缓存管理,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一
千家信息网最后更新 2025年11月07日SpringBoot2.0中如何集成Redis数据库实现缓存管理
小编给大家分享一下SpringBoot2.0中如何集成Redis数据库实现缓存管理,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、Redis简介
Spring Boot中除了对常用的关系型数据库提供了优秀的自动化支持之外,对于很多NoSQL数据库一样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch。这些案例整理好后,陆续都会上传Git。
SpringBoot2 版本,支持的组件越来越丰富,对Redis的支持不仅仅是扩展了API,更是替换掉底层Jedis的依赖,换成Lettuce。
二、Spring2.0集成Redis
1、核心依赖
org.springframework.boot spring-boot-starter-data-redis
2、配置文件
# 端口server: port: 8008spring: application: # 应用名称 name: node08-boot-redis # redis 配置 redis: host: 127.0.0.1 #超时连接 timeout: 1000ms jedis: pool: #最大连接数据库连接数,设 0 为没有限制 max-active: 8 #最大等待连接中的数量,设 0 为没有限制 max-idle: 8 #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。 max-wait: -1ms #最小等待连接中的数量,设 0 为没有限制 min-idle: 0
这样Redis的环境就配置成功了,已经可以直接使用封装好的API了。
3、简单测试案例
import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.StringRedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.util.concurrent.TimeUnit;@RestControllerpublic class RedisController { @Resource private StringRedisTemplate stringRedisTemplate ; @RequestMapping("/setGet") public String setGet (){ stringRedisTemplate.opsForValue().set("cicada","smile"); return stringRedisTemplate.opsForValue().get("cicada") ; } @Resource private RedisTemplate redisTemplate ; /** * 设置 Key 的有效期 10 秒 */ @RequestMapping("/setKeyTime") public String setKeyTime (){ redisTemplate.opsForValue().set("timeKey","timeValue",10, TimeUnit.SECONDS); return "success" ; } @RequestMapping("/getTimeKey") public String getTimeKey (){ // 这里 Key 过期后,返回的是字符串 'null' return String.valueOf(redisTemplate.opsForValue().get("timeKey")) ; }}4、自定义序列化配置
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.io.Serializable;/** * Redis 配置 */@Configurationpublic class RedisConfig { private static final Logger LOGGER = LoggerFactory.getLogger(RedisConfig.class) ; /** * 序列化配置 */ @Bean public RedisTemplate redisTemplate (LettuceConnectionFactory redisConnectionFactory) { LOGGER.info("RedisConfig == >> redisTemplate "); RedisTemplate template = new RedisTemplate<>(); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new GenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); return template; }} 5、序列化测试
import com.boot.redis.entity.User;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;import java.util.ArrayList;import java.util.List;@RestControllerpublic class SerializeController { @Resource private RedisTemplate redisTemplate ; @RequestMapping("/setUser") public String setUser (){ User user = new User() ; user.setName("cicada"); user.setAge(22); List list = new ArrayList<>() ; list.add("小学"); list.add("初中"); list.add("高中"); list.add("大学"); user.setEducation(list); redisTemplate.opsForValue().set("userInfo",user); return "success" ; } @RequestMapping("/getUser") public User getUser (){ return (User)redisTemplate.opsForValue().get("userInfo") ; }} 以上是"SpringBoot2.0中如何集成Redis数据库实现缓存管理"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
配置
数据
数据库
支持
最大
序列
篇文章
限制
缓存
管理
内容
数量
时间
案例
测试
自动化
优秀
最小
有效
成功
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
不断网无服务器
算法工程师软件开发工程师
楚雄网络安全等级
ibm 7915服务器多少钱
长治网络技术质量
软件开发程度度量规范
分类数据库管理
数据库转换access
地铁逃生服务器怎么下载
服务器与设备的关系
java前端数据库
国家农药数据库
网络安全法律法规 例题
粮食绘画软件开发
数据库什么情况下需要用
淄博尚德网络技术有限公司
亿凯西安软件开发工资
navicat怎么连远程数据库
成都极致互动网络技术游戏
降序排序多关键字数据库
安联财险 网络安全险
宿州服务器机柜加工价格
数据库表字段更新
linux 服务器
数据库什么情况下需要用
jira 邮件服务器
黄浦区创新数据库服务商服务费
佛山专业的软件开发公司
软件开发销售 账务处理
支付宝扫脸支付软件开发