Spring Boot使用redis做数据缓存

1 添加redis支持

在pom.xml中添加

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

2 redis配置

  1. package com.wisely.ij.config;
  2. import com.fasterxml.jackson.annotation.JsonAutoDetect;
  3. import com.fasterxml.jackson.annotation.PropertyAccessor;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import org.springframework.cache.CacheManager;
  6. import org.springframework.cache.annotation.CachingConfigurerSupport;
  7. import org.springframework.cache.annotation.EnableCaching;
  8. import org.springframework.cache.interceptor.KeyGenerator;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.data.redis.cache.RedisCacheManager;
  12. import org.springframework.data.redis.connection.RedisConnectionFactory;
  13. import org.springframework.data.redis.core.RedisTemplate;
  14. import org.springframework.data.redis.core.StringRedisTemplate;
  15. import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
  16. import java.lang.reflect.Method;
  17. @Configuration
  18. @EnableCaching
  19. public class RedisConfig extends CachingConfigurerSupport{
  20. @Bean
  21. public KeyGenerator wiselyKeyGenerator(){
  22. return new KeyGenerator() {
  23. @Override
  24. public Object generate(Object target, Method method, Object... params) {
  25. StringBuilder sb = new StringBuilder();
  26. sb.append(target.getClass().getName());
  27. sb.append(method.getName());
  28. for (Object obj : params) {
  29. sb.append(obj.toString());
  30. }
  31. return sb.toString();
  32. }
  33. };
  34. }
  35. @Bean
  36. public CacheManager cacheManager(
  37. @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) {
  38. return new RedisCacheManager(redisTemplate);
  39. }
  40. @Bean
  41. public RedisTemplate<String, String> redisTemplate(
  42. RedisConnectionFactory factory) {
  43. StringRedisTemplate template = new StringRedisTemplate(factory);
  44. Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
  45. ObjectMapper om = new ObjectMapper();
  46. om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
  47. om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
  48. jackson2JsonRedisSerializer.setObjectMapper(om);
  49. template.setValueSerializer(jackson2JsonRedisSerializer);
  50. template.afterPropertiesSet();
  51. return template;
  52. }
  53. }

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 # connection port
  6. spring.redis.pool.max-idle=8 # pool settings ...
  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

4 应用

测试两个实体类

  1. package com.wisely.ij.domain;
  2. public class Address {
  3. private Long id;
  4. private String province;
  5. private String city;
  6. public Address(Long id,String province, String city) {
  7. this.id = id;
  8. this.province = province;
  9. this.city = city;
  10. }
  11. public Address() {
  12. }
  13. public Long getId() {
  14. return id;
  15. }
  16. public void setId(Long id) {
  17. this.id = id;
  18. }
  19. public String getProvince() {
  20. return province;
  21. }
  22. public void setProvince(String province) {
  23. this.province = province;
  24. }
  25. public String getCity() {
  26. return city;
  27. }
  28. public void setCity(String city) {
  29. this.city = city;
  30. }
  31. }
  1. package com.wisely.ij.domain;
  2. public class User {
  3. private Long id;
  4. private String firstName;
  5. private String lastName;
  6. public User(Long id,String firstName, String lastName) {
  7. this.id = id ;
  8. this.firstName = firstName;
  9. this.lastName = lastName;
  10. }
  11. public User() {
  12. }
  13. public Long getId() {
  14. return id;
  15. }
  16. public void setId(Long id) {
  17. this.id = id;
  18. }
  19. public String getFirstName() {
  20. return firstName;
  21. }
  22. public void setFirstName(String firstName) {
  23. this.firstName = firstName;
  24. }
  25. public String getLastName() {
  26. return lastName;
  27. }
  28. public void setLastName(String lastName) {
  29. this.lastName = lastName;
  30. }
  31. }

使用演示

  1. package com.wisely.ij.service;
  2. import com.wisely.ij.domain.Address;
  3. import com.wisely.ij.domain.User;
  4. import org.springframework.cache.annotation.Cacheable;
  5. import org.springframework.stereotype.Service;
  6. /**
  7. * Created by wisely on 2015/5/25.
  8. */
  9. @Service
  10. public class DemoService {
  11. @Cacheable(value = "usercache",keyGenerator = "wiselyKeyGenerator")
  12. public User findUser(Long id,String firstName,String lastName){
  13. System.out.println("无缓存的时候调用这里");
  14. return new User(id,firstName,lastName);
  15. }
  16. @Cacheable(value = "addresscache",keyGenerator = "wiselyKeyGenerator")
  17. public Address findAddress(Long id,String province,String city){
  18. System.out.println("无缓存的时候调用这里");
  19. return new Address(id,province,city);
  20. }
  21. }
  1. package com.wisely.ij.web;
  2. import com.wisely.ij.domain.Address;
  3. import com.wisely.ij.domain.User;
  4. import com.wisely.ij.service.DemoService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Controller;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.ResponseBody;
  9. /**
  10. * Created by wisely on 2015/5/25.
  11. */
  12. @Controller
  13. public class DemoController {
  14. @Autowired
  15. DemoS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值