学习redis在springboot环境下的部署,使用RedisTemplate操作。springboot版本为:2.0.4
pom.xml引用依赖
1 | <dependency> |
application.yml配置文件
加入redis配置。我使用Lettuce配置redis启动失败,原因未知。改用jedis后成功。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15spring:
redis:
host: 127.0.0.1
port: 6379
#意思是连接了1000毫秒后没接连成功就断开连接。
timeout: 1000
#Lettuce 当多线程使用同一连接实例时,是线程安全的 jedis不安全
jedis:
pool:
#最大空闲数
max-idle: 8
#连接池的最大数据库连接数。设为0表示无限制
max-active: 0
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
max-wait: 1000
RedisConfig.java
redis配置类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
59package com.vue.vuedemo.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.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
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;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
/**
* @Author shiyuquan
* @Date 2018/9/18 20:31
* @Description TODO
*/
public class RedisConfig {
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
// 设置缓存有效期
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(5));
return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheConfiguration).build();
}
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
//使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
serializer.setObjectMapper(mapper);
template.setValueSerializer(serializer);
//使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
}
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(factory);
return stringRedisTemplate;
}
}
使用RedisTemplate访问Redis数据结构
感谢大神的分享https://www.jianshu.com/p/7bf5dc61ca06/
RedisTemplate二次封装 RedisService.java
1 | package com.vue.vuedemo.utils; |