SpringBoot使用Redis注解

摘要: SpringBoot使用Redis注解

Springboot 使用Redis注解

配置yml

1
2
3
4
5
6
7
8
spring:
cache:
cache-names: mdc-cache
redis:
host: ${spring.redis.host}
password: ${spring.redis.password}
port: ${spring.redis.port}
timeout: ${spring.redis.timeout}

RedisConfiguration配置

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.paascloud.provider.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

/**
* The class Redis configuration.
* Created by paascloud.net@gmail.com
*/
@Configuration
@EnableCaching
public class RedisConfiguration extends CachingConfigurerSupport {

/**
* Key generator key generator.
*
* @return the key generator
*/
@Bean
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
StringBuilder sb = new StringBuilder();
sb.append(target.getClass().getName());
sb.append(method.getName());
for (Object obj : params) {
sb.append(obj.toString());
}
return sb.toString();
};

}

/**
* Cache manager cache manager.
*
* @param redisTemplate the redis template
*
* @return the cache manager
*/
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
return new RedisCacheManager(redisTemplate);
}

/**
* Redis template redis template.
*
* @param factory the factory
*
* @return the redis template
*/
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate template = new StringRedisTemplate(factory);
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}

web层

1
2
3
4
5
6
7
@PostMapping(value = "/get4City")
@ApiOperation(httpMethod = "POST", value = "查看四级地址")
public Wrapper<List<TreeNode>> get4City() {
logger.info("get4City - 获取四级地址");
List<TreeNode> treeNodeList = mdcAddressService.get4City();
return WrapMapper.ok(treeNodeList);
}

serviceImpl

1
2
3
4
5
6
7
8
@Override
@Cacheable(cacheNames="mdc-cache")
public List<TreeNode> get4City() {
List<MdcAddress> mdcAddresses = mdcAddressMapper.selectAll();
List<TreeNode> treeNodeList = buildGroupTree(mdcAddresses);
logger.info("treeNodeList={}", treeNodeList);
return treeNodeList;
}

小坑

1
java.lang.ClassCastException: org.springframework.cache.interceptor.SimpleKey cannot be cast to java.lang.String

解决办法

1
2
3
4
5
6
7
8
9
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
@Override
public KeyGenerator keyGenerator() { ... }

...
}

参考
http://blog.csdn.net/a67474506/article/details/52608855
http://wiselyman.iteye.com/blog/2184884

评论