springboot2.0整合redis作为缓存以json格式存储对象
步骤1
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.5.0</version>
</dependency>
步骤2
redis:
host: 127.0.0.1
jedis:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
lettuce:
pool:
max-active: 8
max-idle: 8
max-wait: -1ms
min-idle: 0
shutdown-timeout: 100ms
步骤3
package com.example.demo.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.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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import java.time.Duration; /**
* @author: Administrator
* @date: 2019/12/12 0012
*/
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); RedisSerializer<String> redisSerializer = new StringRedisSerializer(); Jackson2JsonRedisSerializer 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.setConnectionFactory(factory);
//key序列化方式
template.setKeySerializer(redisSerializer);
//value序列化
template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap序列化
template.setHashValueSerializer(jackson2JsonRedisSerializer); return template;
} @Bean
public CacheManager cacheManager(RedisConnectionFactory factory) {
RedisSerializer<String> redisSerializer = new StringRedisSerializer();
Jackson2JsonRedisSerializer 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); // 配置序列化(解决乱码的问题),过期时间30秒
RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(30))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer))
.disableCachingNullValues(); RedisCacheManager cacheManager = RedisCacheManager.builder(factory)
.cacheDefaults(config)
.build();
return cacheManager;
}
}
步骤4
package com.example.demo.service; import com.example.demo.bean.Employee;
import com.example.demo.mapper.EmployeeMapeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service; /**
* @author: Administrator
* @date: 2019/12/11 0011
*/
@Service
public class EmployeeService { @Autowired
EmployeeMapeer employeeMapeer; /**
* 将方法的运行结果进行缓存,以后再要相同的数据,我们就从缓存中获取,不用调用方法了
*
* @param id
* @return
*/
// @Cacheable(cacheNames = {"emp"}, key = "#root.methodName+'['+#id+']'")
@Cacheable(value = "emp")
public Employee findById(Integer id) {
// System.out.println("查询" + id + "员工信息");
return employeeMapeer.selectById(id);
} @CachePut(value = "emp")
public Employee update(Employee employee) {
System.out.println("updateEmp" + employee);
employeeMapeer.update(employee);
return employee;
}
}
springboot2.0整合redis作为缓存以json格式存储对象的更多相关文章
- SpringBoot2.0 整合 Redis集群 ,实现消息队列场景
本文源码:GitHub·点这里 || GitEE·点这里 一.Redis集群简介 1.RedisCluster概念 Redis的分布式解决方案,在3.0版本后推出的方案,有效地解决了Redis分布式的 ...
- springboot2.x整合redis实现缓存(附github链接)
本文代码已提交github: https://github.com/LCABC777/Springboot-redis(1)Springboot中使用redis操作的两种方式:lettuce和j ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- SpringBoot2.0整合Redis
Spring Boot2.0在2018年3月份正式发布,相比1.0还是有比较多的改动,例如SpringBoot 自2.0起支持jdk1.8及以上的版本.第三方类库升级.响应式 Spring 编程支持等 ...
- springboot2.0整合redis的发布和订阅
1.Maven引用 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...
- SpringBoot2.x整合Redis实战 4节课
1.分布式缓存Redis介绍 简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download 2.新手 ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解
笔记 3.SpringBoot2.x整合redis实战讲解 简介:使用springboot-starter整合reids实战 1.官网:https://docs.spring.io/spring-bo ...
- 第二篇:SpringBoot2.0整合ActiveMQ
本篇开始将具体介绍SpringBoot如何整合其它项目. 如何创建SpringBoot项目 访问https://start.spring.io/. 依次选择构建工具Maven Project.语言ja ...
- SpringBoot2.0 整合 QuartJob ,实现定时器实时管理
一.QuartJob简介 1.一句话描述 Quartz是一个完全由java编写的开源作业调度框架,形式简易,功能强大. 2.核心API (1).Scheduler 代表一个 Quartz 的独立运行容 ...
随机推荐
- swagger list Could not resolve reference because of: Could not resolve point
swagger list Could not resolve reference because of: Could not resolve point controller的参数要加 @Requ ...
- poi读取excel的列和删除列
(各自根据具体的poi版本进行相应的替换即可) package com.br.loan.strategy.common.utils; import lombok.extern.slf4j.Slf4j; ...
- MySQL路线
一 数据库简介与安装 二 库操作 三 表操作 四 数据操作 五 索引原理与慢查询优化 六 数据备份与慢查询优化 七 视图.触发器.事务.存储过程.函数
- 【转载】 C#中List集合使用OrderByDescending方法对集合进行倒序排序
在C#的List集合操作中,有时候需要针对List集合进行排序操作,如果是对List集合按照元素对象或者元素对象的某个属性进行倒序排序的话,可以使用OrderByDescending方法来实现,Ord ...
- JavaScript原型链以及Object,Function之间的关系
JavaScript里任何东西都是对象,任何一个对象内部都有另一个对象叫__proto__,即原型,它可以包含任何东西让对象继承.当然__proto__本身也是一个对象,它自己也有自己的__proto ...
- RTP包的结构
live555中数据的发送最后是要使用RTP协议发送的,下面介绍一下RTP包格式. RTP packet RTP是基于UDP协议的,RTP服务器会通过UDP协议,通常每次会发送一个RTP packet ...
- [LeetCode] 198. 打家劫舍 ☆(动态规划)
描述 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警. 给定一个 ...
- VS调试 DataTable (转载)
调试的时候遇到一个问题:不知道怎么在自动窗口或者添加监视那里查看DataSet或者DataTable的具体的值.度娘了一下很多都是添加DataTable.Rows[][]监视,但是一行一列地看还是有点 ...
- day 03 预科
目录 什么是变量: 变量的组成 变量的风格 1.驼峰体 2.下划线 变量名的组成规范 注释的作用 turtle库的简单使用 什么是变量: 1.是变化的量. 2.变:现实世界中的状态是会发生改变的. 3 ...
- python的常见内置模块之-----time
1.time模块 a.时间戳:print(time.time()) 从1970年到现在的时间,秒数 import time print(time.time()) >>>157448 ...