spring boot使用redis做数据缓存

本文详细介绍了如何在Spring Boot应用中集成Redis作为数据缓存,包括在pom.xml中添加依赖,配置Redis连接,创建Redis配置类,以及使用注解@Cacheable进行缓存操作的示例代码,通过测试验证了缓存功能的正确性。
摘要由CSDN通过智能技术生成

1 添加redis支持
在pom.xml中添加

1. <dependency>
2. <groupId>org.springframework.boot</groupId>
3. <artifactId>spring-boot-starter-redis</artifactId>
4. </dependency>
1.
2.
3.
4.
 

2 redis配置


1. package
2.   
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16. import
17.   
18. import
19.   
20.   
21. @Configuration
22. @EnableCaching
23. public class RedisConfig extends
24.   
25. @Bean
26. public
27. return new
28. @Override
29. public
30. new
31.                 sb.append(target.getClass().getName());  
32.                 sb.append(method.getName());  
33. for
34.                     sb.append(obj.toString());  
35.                 }  
36. return
37.             }  
38.         };  
39.   
40.     }  
41.   
42. @Bean
43. public
44. @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {  
45. return new
46.     }  
47.   
48. @Bean
49. public
50.             RedisConnectionFactory factory) {  
51. new
52. new Jackson2JsonRedisSerializer(Object.class);  
53. new
54.         om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);  
55.         om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);  
56.         jackson2JsonRedisSerializer.setObjectMapper(om);  
57.         template.setValueSerializer(jackson2JsonRedisSerializer);  
58.         template.afterPropertiesSet();  
59. return
60.     }  
61. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
 

3 redis服务器配置
1. # REDIS (RedisProperties)  
2. spring.redis.database= # database name  
3. spring.redis.host=localhost # server host  
4. spring.redis.password= # server password  
5. spring.redis.port=6379
6. spring.redis.pool.max-idle=8
7. spring.redis.pool.min-idle=0
8. spring.redis.pool.max-active=8
9. spring.redis.pool.max-wait=-1
10. spring.redis.sentinel.master= # name of Redis server  
11. spring.redis.sentinel.nodes= # comma-separated list of host:port pairs
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.


 

4 应用
测试两个实体类

1. package
2.   
3.   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值