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.
4. public class
5. private
6. private
7. private
8.
9. public
10. this.id = id;
11. this.province = province;
12. this.city = city;
13. }
14.
15. public
16. }
17.
18. public
19. return
20. }
21.
22. public void
23. this.id = id;
24. }
25.
26. public
27. return
28. }
29.
30. public void
31. this.province = province;
32. }
33.
34. public
35. return
36. }
37.
38. public void
39. this.city = city;
40. }
41. }
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.
1. package
2.
3.
4. public class
5. private
6. private
7. private
8.
9. public
10. this.id = id ;
11. this.firstName = firstName;
12. this.lastName = lastName;
13. }
14.
15. public
16. }
17.
18. public
19. return
20. }
21.
22. public void
23. this.id = id;
24. }
25.
26. public
27. return
28. }
29.
30. public void
31. this.firstName = firstName;
32. }
33.
34. public
35. return
36. }
37.
38. public void
39. this.lastName = lastName;
40. }
41. }
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.
使用演示
1. package
2.
3. import
4. import
5. import
6. import
7.
8. /**
9. * Created by wisely on 2015/5/25.
10. */
11. @Service
12. public class
13. @Cacheable(value = "usercache",keyGenerator = "wiselyKeyGenerator")
14. public
15. "无缓存的时候调用这里");
16. return new
17. }
18. @Cacheable(value = "addresscache",keyGenerator = "wiselyKeyGenerator")
19. public
20. "无缓存的时候调用这里");
21. return new
22. }
23. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10.
11. /**
12. * Created by wisely on 2015/5/25.
13. */
14.
15. @Controller
16. public class
17.
18. @Autowired
19. DemoService demoService;
20.
21. @RequestMapping("/test")
22. @ResponseBody
23. public
24. "wang","yunfei");
25. "anhui","hefei");
26. "若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
27. return "ok";
28. }
29. @RequestMapping("/test2")
30. @ResponseBody
31. public
32. "wang","yunfei");
33. "anhui","hefei");
34. "我这里没执行查询");
35. "user:"+"/"+user.getFirstName()+"/"+user.getLastName());
36. "address:"+"/"+address.getProvince()+"/"+address.getCity());
37. return "ok";
38. }
39. }
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.
5 检验
先访问http://localhost:8080/test 保存缓存
再访问http://localhost:8080/test2 调用缓存里的数据
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.
4. public class
5. private
6. private
7. private
8.
9. public
10. this.id = id;
11. this.province = province;
12. this.city = city;
13. }
14.
15. public
16. }
17.
18. public
19. return
20. }
21.
22. public void
23. this.id = id;
24. }
25.
26. public
27. return
28. }
29.
30. public void
31. this.province = province;
32. }
33.
34. public
35. return
36. }
37.
38. public void
39. this.city = city;
40. }
41. }
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.
1. package
2.
3.
4. public class
5. private
6. private
7. private
8.
9. public
10. this.id = id ;
11. this.firstName = firstName;
12. this.lastName = lastName;
13. }
14.
15. public
16. }
17.
18. public
19. return
20. }
21.
22. public void
23. this.id = id;
24. }
25.
26. public
27. return
28. }
29.
30. public void
31. this.firstName = firstName;
32. }
33.
34. public
35. return
36. }
37.
38. public void
39. this.lastName = lastName;
40. }
41. }
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.
使用演示
1. package
2.
3. import
4. import
5. import
6. import
7.
8. /**
9. * Created by wisely on 2015/5/25.
10. */
11. @Service
12. public class
13. @Cacheable(value = "usercache",keyGenerator = "wiselyKeyGenerator")
14. public
15. "无缓存的时候调用这里");
16. return new
17. }
18. @Cacheable(value = "addresscache",keyGenerator = "wiselyKeyGenerator")
19. public
20. "无缓存的时候调用这里");
21. return new
22. }
23. }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
1. package
2.
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10.
11. /**
12. * Created by wisely on 2015/5/25.
13. */
14.
15. @Controller
16. public class
17.
18. @Autowired
19. DemoService demoService;
20.
21. @RequestMapping("/test")
22. @ResponseBody
23. public
24. "wang","yunfei");
25. "anhui","hefei");
26. "若下面没出现“无缓存的时候调用”字样且能打印出数据表示测试成功");
27. return "ok";
28. }
29. @RequestMapping("/test2")
30. @ResponseBody
31. public
32. "wang","yunfei");
33. "anhui","hefei");
34. "我这里没执行查询");
35. "user:"+"/"+user.getFirstName()+"/"+user.getLastName());
36. "address:"+"/"+address.getProvince()+"/"+address.getCity());
37. return "ok";
38. }
39. }
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.
5 检验
先访问http://localhost:8080/test 保存缓存
再访问http://localhost:8080/test2 调用缓存里的数据
-----------------------------------
spring boot使用redis做数据缓存
https://blog.51cto.com/u_11979904/5948188
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)