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

---------------------

springboot-redis相关配置整理的更多相关文章

  1. Redis相关知识整理

    Redis相关知识整理 1. Redis和MySQL的区别?a).mysql是关系型数据库,而redis是NOSQL,非关系型数据库.mysql将数据持久化到硬盘,读取数据慢,而redis数据先存储在 ...

  2. redis相关配置

    redis相关配置1.yum 源码 rpm yum 快速,间接,高效,解决依赖关系,(自动安装到某个路径,不可控),通过yum安装的软件查询命令 rpm -ql nginx yum源的软件包可能版本非 ...

  3. SpringBoot数据源相关配置

    数据源配置 单数据源 配置步骤 引入依赖:H2数据库驱动.JDBC依赖.acturator(运维).web模块(用于测试).lambok(使用@Slf4j打印日志). 直接配置所需的Bean,注入容器 ...

  4. SpringBoot+Redis相关配置文件

    springboot整合redis配置类 package com.yalong.config; import com.fasterxml.jackson.annotation.JsonAutoDete ...

  5. SpringBoot Redis序列化配置

    Redis配置 #Redis spring.redis.host= spring.redis.port=6379 spring.redis.database=0 # Redis服务器连接密码(默认为空 ...

  6. Linux 安装redis 基本配置 发布订阅,安全配置,持久化 rdb ,aof

    redis redis相关配置1.yum  源码 rpm  yum 快速,间接,高效,解决依赖关系,(自动安装到某个路径,不可控),通过yum安装的软件查询命令 rpm -ql nginx  yum源 ...

  7. Maven + Springboot + redis 配置

    前言 刚进入到Java 开发的世界,对于小白Java的我来说,使用Maven + SpringBoot 的项目下启动redis: 第一步 本地安装Redis 服务 关于redis的教程链接 点击这里: ...

  8. springboot +redis配置

    springboot +redis配置 pom依赖 <dependency> <groupId>org.springframework.boot</groupId> ...

  9. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

随机推荐

  1. The mook jong

    The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...

  2. 2018-2019-2 《Java程序设计》第11周学习总结

    20175319 2018-2019-2 <Java程序设计>第11周学习总结 教材学习内容总结 本周学习<Java程序设计>第十三章java网络编程: - URL类 URL类 ...

  3. 使用mysql应该注意的细节

    一.表及字段的命名规范 1.可读性原则 使用大写和小写来格式化的库对象名字以获得良好的可读性. 例如:使用CustAdress而不是custaddress来提高可读性.(这里注意有些DBMS系统对表名 ...

  4. java自学基础、项目实战网站推荐

    推荐一个自学的好平台,有Java前端,后端,基础的内容都有讲解,还有框架的讲解和实战项目,特别适合自学 JAVA 自学网站 JAVA 练习题 Mybatis 教程 Spring MVC 教程 模仿天猫 ...

  5. layer子窗口赋值给父窗口

    子窗体赋值给父窗体: parent.$('#Receiver').val(typearr); //关闭子弹窗 var index = parent.layer.getFrameIndex(window ...

  6. make源文件时出现 /usr/bin/ld: cannot find -lstdc++ 错误

    解决CentOS 7 中,make源文件时出现 /usr/bin/ld: cannot find -lstdc++ 错误 在CentOS 7中,使用static方法编译,需要安装static vers ...

  7. Visual Studio Code如何编写运行C、C++

    Visual Studio Code如何编写运行C.C++ 作者:知乎用户链接:https://www.zhihu.com/question/30315894/answer/154979413来源:知 ...

  8. 项目部署到IIS后,明明存在某个文件,但是访问却返回404

    项目部署到IIS后,明明存在某个文件,但是访问却返回404,这是为什么呢,原因很可能是未添加MIME类型 比如我的文件名是“iconfont.woff” 打开IIS,点击对应的项目,右面展示的是下图 ...

  9. sql 摘抄

    练习题和参考解 (1)查询“001”课程比“002”课程成绩低的所有学生的学号.001学科成绩.002学科成绩                 1 SELECT 2 s1.StudentNo, 3 s ...

  10. OAuth_2

    角色: OAuth2.0为用户和应用定义了如下角色: 资源拥有者.资源服务器.客户端应用.授权服务器 资源拥有者:拥有共享数据的人或应用,比如Facebook的用户就是 资源拥有者,但资源拥有者也可以 ...