springboot-redis相关配置整理
1.pom.xml引入对应数据文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
2.yml配置redis连接信息
redis:
database: 0
port: 7000
jedis:
pool:
max-idle: 20
min-idle: 2
max-active: 50
max-wait: 3000
host: 192.168.1.140
timeout: 5000
1
2
3
4
5
6
7
8
9
10
11
3.RedisConfig配置
RedisConfig
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import org.springframework.cache.annotation.CachingConfigurerSupport;
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.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
// @Bean
// public RedisTemplate<Object,SendParameter> empRedisTemplate(RedisConnectionFactory redisConnectionFactory)
// throws UnknownHostException{
// RedisTemplate<Object,SendParameter> template = new RedisTemplate<Object, SendParameter>();
// template.setConnectionFactory(redisConnectionFactory);
// Jackson2JsonRedisSerializer<SendParameter> ser = new Jackson2JsonRedisSerializer<SendParameter>(SendParameter.class);
// template.setDefaultSerializer(ser);
// return template;
// }
@Bean
public RedisTemplate<String, List<Map<String, Object>>> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, List<Map<String, Object>>> template = new RedisTemplate<String, List<Map<String, Object>>>();
template.setConnectionFactory(redisConnectionFactory);
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new RedisObjectSerializer());
return template;
}
}
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
RedisObjectSerializer
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter();
private Converter<byte[], Object> deserializer = new DeserializingConverter(http://www.my516.com);
static final byte[] EMPTY_ARRAY = new byte[0];
@Override
public Object deserialize(byte[] bytes) {
if (isEmpty(bytes)) {
return null;
}
try {
return deserializer.convert(bytes);
} catch (Exception ex) {
throw new SerializationException("Cannot deserialize", ex);
}
}
@Override
public byte[] serialize(Object object) {
if (object == null) {
return EMPTY_ARRAY;
}
try {
return serializer.convert(object);
} catch (Exception ex) {
return EMPTY_ARRAY;
}
}
private boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
}
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
---------------------
最新文章
- Tomcat 学习心得
- Bug修正
- Java虚拟机5:Java垃圾回收(GC)机制详解
- NuGet学习笔记2——使用图形化界面打包自己的类库
- ASP.NET MVC电子书下载
- C#中方法的调用
- Android应用开发基础之四:网络编程(一)
- mysql insert一条记录后怎样返回创建记录的主键id,last_insert_id(),selectkey
- httpclient发送request请求时设置header和timeout
- ios应用,今年最蛋疼的6月,IPV6!!
- Java LinkedList特有方法程序小解 &;&; 使用LinkedList 模拟一个堆栈或者队列数据结构。
- Hibernate二级缓存原理
- js获得时间new Date()整理
- 程序员是这样区分Null和Undefined
- Android 免Root实现Apk静默安装,覆盖兼容市场主流的98%的机型
- 螺旋矩阵 java实现(待消化)
- java使用顺序存储实现队列
- importlib模块
- SPOJ 1812 LCS2
- Set和WeakSet数据结构
热门文章
- JQUERY的$(function(){})和window.onload=function(){}的区别【转】
- 7 November in 614
- CentOS7下python虚拟环境
- 测开之路三十四:html常用标签
- js中slice、splice、substr、split方法
- PHP将mysql数据表转换为excel文件
- ucenter 整合同步登录的内部实现原理
- PAT_A1071#Speech Patterns
- OSX 创建 randisk(或称 tmpfs)
- 私有IP地址