微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringBoot如何配置Redis高并发缓存

今天小编给大家分享一下SpringBoot如何配置Redis高并发缓存的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

1.引入依赖

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

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<String, Serializable> redistemplate(LettuceConnectionFactory connectionFactory) {Redistemplate<String, Serializable> 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");}}

以上就是“SpringBoot如何配置Redis高并发缓存”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程之家行业资讯频道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐