RedisCacheConfig.java
1.92 KB
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
package cn.com.glsx.auditstatistics.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis config
*/
@Configuration
@EnableCaching
public class RedisCacheConfig {
@Autowired
RedisConnectionFactory redisConnectionFactory;
@Bean("redisTemplate")
public RedisTemplate<String, String> redisTemplate() {
StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);
//定义key序列化方式
RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
//定义value的序列化方式
template.setKeySerializer(redisSerializer);
// template.setValueSerializer(redisSerializer);
template.afterPropertiesSet();
return template;
}
@Bean("objectRedisTemplate")
public RedisTemplate<Object, Object> objectRedisTemplate() {
RedisTemplate template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
//定义key序列化方式
RedisSerializer<String> redisSerializer = new StringRedisSerializer();//Long类型会出现异常信息;需要我们上面的自定义key生成策略,一般没必要
//定义value的序列化方式
template.setKeySerializer(redisSerializer);
template.afterPropertiesSet();
return template;
}
}