SpringBoot如何配置Redis高并发缓存
作者:FreshDream
时间:2023-04-27
来源:互联网
浏览:0
1.引入依赖org.springframework.bootspring-boot-starter-data-redis2.配置#启动redis#redis的数据库索引(默认为0)spring.redis.database=2#redis的服务器地址spring.redis.host=127.0.0.1#密码(没有就为空)spring.redis.password=#连接池的最大连接数spring.redis.jedis.pool.max-active=2000#连接池的最大阻塞等待时间(使用负值表示无限
1.引入依赖
org.springframework.boot spring-boot-starter-data-redis
2.配置
#启动redis #redis的数据库索引(默认为0) spring.redis.database=2 #redis的服务器地址 spring.redis.host=127.0.0.1 #密码(没有就为空) spring.redis.password= #连接池的最大连接数 spring.redis.jedis.pool.max-active=2000 #连接池的最大阻塞等待时间(使用负值表示无限制) spring.redis.jedis.pool.max-wait=-1 #连接池的最小空闲连接 spring.redis.jedis.pool.min-idle=50 #连接超时时间(毫秒) spring.redis.timeout=1000 #集群模式配置 #spring.redis.cluster.nodes=106.54.79.43:7001,106.54.79.43:7002,106.54.79.43:7003,106.54.79.43:7004,106.54.79.43:7005,106.54.79.43:7006
3.自动装配的对象
@AutowiredStringRedisTemplate stringRedisTemplate;//仅支持字符串的数据@AutowiredRedisTemplate redisTemplate;//支持对象的数据,但需要对对象进行序列化
4.序列化
什么是序列化?
序列化是将对象状态转换为可保持或传输的格式的过程。与序列化相对的是反序列化,它将流转换为对象。这两个过程结合起来,可以轻松地存储和传输数据。
为什么要序列化对象
把对象转换为字节序列的过程称为对象的序列化把字节序列恢复为对象的过程称为对象的反序列化
@Configuration@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisConfig {/**java项目www.1b23.com
* 对属性进行序列化和创建连接工厂
* @param connectionFactory
* @return
*/@Beanpublic RedisTemplate redisTemplate(LettuceConnectionFactory connectionFactory) {RedisTemplate template = new RedisTemplate<>();template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(new GenericJackson2JsonRedisSerializer());template.setConnectionFactory(connectionFactory);return template;}} 5.测试
//java项目www.1b23.com@RequestMapping("/user")@RestControllerpublic class UserController {@AutowiredStringRedisTemplate stringRedisTemplate;//仅支持字符串的数据@AutowiredRedisTemplate redisTemplate;//支持对象的数据,前提需要进行序列化@GetMappingpublic User user(){User user = new User();user.setId("1");user.setName("zhangshan");user.setPhone("133333333");//插入数据 stringRedisTemplate.opsForValue().set("1",user.toString());redisTemplate.opsForValue().set("user",user);// return stringRedisTemplate.opsForValue().get("1"); return (User)redisTemplate.opsForValue().get("user");}}
作者最新文章
vivo V80 曝光:10月发布,首推10倍人像变焦与蔡司夜景长焦
2026-09-08 17:00
PDF转Excel操作指南:极轻PDF在线工具使用步骤与结果核对
2026-09-02 19:02
Blender 3D动画制作入门:绑定、关键帧、灯光与渲染全流程
2026-09-02 11:10
PS教程:图层、选区、蒙版与调色核心操作
2026-09-02 10:12
iQOO Pro(12GB/128GB/5G全网通)忘了手机密码怎么办?
2026-08-25 15:47
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多
Windows 10
Windows
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式
Windows/macOS/Linux
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















