使用springboot cache + redis缓存时使用gzip压缩以提升性能
背景
在高并发的场景中,我们通常会使用缓存提升性能。在使用springboot cache时,我们通常会使用基于JSON的序列化与反序列化。
JSON具有可读性强,结构简单的特点,使用灵活。
但是JSON体积大,占用redis内存,同时增加网络开销,使用gzip压缩可以将体积缩减到原来的十分之一以下,取得媲美Protobuf的编码效率
使用springboot cache + redis并使用Gzip压缩
引入依赖
<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>
Gzip压缩工具类
public class GzipUtil {
public static byte[] compress(byte[] data) throws IOException {
if (data == null || data.length == 0) {
return new byte[0];
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzOut = new GZIPOutputStream(out);
gzOut.write(data);
gzOut.close();
return out.toByteArray();
}
public static byte[] decompress(byte[] gzip) throws IOException {
if (gzip == null || gzip.length == 0) {
return new byte[0];
}
ByteArrayInputStream in = new ByteArrayInputStream(gzip);
GZIPInputStream gzIn = new GZIPInputStream(in);
return gzIn.readAllBytes();
}
}
继承GenericJackson2JsonRedisSerializer 并实现Gzip压缩
public class JacksonGzipSerializer extends GenericJackson2JsonRedisSerializer {
@Override
public byte[] serialize(@Nullable Object source) throws SerializationException {
byte[] raw = super.serialize(source);
try {
return GzipUtil.compress(raw);
} catch (IOException ioe) {
throw new SerializationException("Exception", ioe);
}
}
@Override
public Object deserialize(@Nullable byte[] source) throws SerializationException {
try {
byte[] raw = GzipUtil.decompress(source);
return deserialize(raw, Object.class);
} catch (IOException ioe) {
throw new SerializationException("Exception", ioe);
}
}
}
配置Config,使用上述自定义的序列化及反序列化机制
@Configuration
public class CacheConfig implements CachingConfigurer {
@Resource
private RedisConnectionFactory redisConnectionFactory;
@Override
@Bean
public CacheManager cacheManager() {
return new RedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), redisCacheConfiguration());
}
public RedisCacheConfiguration redisCacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofMinutes(5))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.string()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new JacksonGzipSerializer()));
}
}
使用@EnableCaching开启缓存
@SpringBootApplication
@EnableCaching
public class RedisCacheApplication {
public static void main(String[] args) {
SpringApplication.run(RedisCacheApplication.class, args);
}
}
最后别忘了配置redis(具体格式和springboot版本有关)
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
效果
没用使用gzip的大小101KB

使用gzip压缩后的大小 14KB

使用springboot cache + redis缓存时使用gzip压缩以提升性能的更多相关文章
- springboot整合redis缓存
使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...
- springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。
springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...
- SpringBoot整合redis缓存(一)
准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...
- Java SpringBoot使用Redis缓存和Ehcache
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...
- springboot整合redis缓存一些知识点
前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...
- SpringBoot使用redis缓存List<Object>
一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...
- SpringBoot(七) - Redis 缓存
1.五大基本数据类型和操作 1.1 字符串-string 命令 说明 set key value 如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值 get key 如果ke ...
- SpringBoot 整合 Redis缓存
在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...
- springboot集成redis缓存
1.pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本) <dependency> <groupId>or ...
- spring Cache /Redis 缓存 + Spring 的集成示例
spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...
随机推荐
- 动态规划篇——线性DP
动态规划篇--线性DP 本次我们介绍动态规划篇的线性DP,我们会从下面几个角度来介绍: 数字三角形 最长上升子序列I 最长上升子序列II 最长公共子序列 最短编辑距离 数字三角形 我们首先介绍一下题目 ...
- vue3.0使用tui.image-editor图片编辑组件报错TypeError: Cannot convert undefined or null to object
在vue3.0的项目中使用tui.image-editor组件.一直都是报错.查看报错位置发现代码 addEventListener() { Object.keys(this.$listeners). ...
- 轻松玩转sed
sed处理文本方法 1.文本或管道输入 2.读入一行到模式控件 3.sed命令处理 4.输出到屏幕 所以 sed是一个流处理编辑器 sed一次处理一行内容 sed不改变文件内容(可以通过重定向改变文件 ...
- combotree 的简单使用2
上一次我在 combotree 的简单使用 中介绍了一种combotree的写法,不过有一个缺点,就是当输的结构非常大的时候,分级较多时,消耗内存的现象会比较严重,下面介绍的一种方法,使combotr ...
- ArcEngine 释放对象
释放对象 例如IFeatureCursor,IFeatureClass等 ESRI.ArcGIS.ADF.ComReleaser.ReleaseCOMObject(myFeatureCursor); ...
- 【Java面试指北】Exception Error Throwable 你分得清么?
读本篇文章之前,如果让你叙述一下 Exception Error Throwable 的区别,你能回答出来么? 你的反应是不是像下面一样呢? 你在写代码时会经常 try catch(Exception ...
- 【每日一题】【字符串与数字互转】【去除空格】【大数处理】2021年12月12日-8. 字符串转换整数 (atoi)
请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAtoi(string s) 的算法如下: ...
- codeforce E - Binary Inversions题解
题目: 给你一个01串,现在你可以(或者不用)选取其中一个元素进行一次反转操作0-1,1-0:从而使得串中的逆序对个数最多. 题目链接:codeforce origin problem 思路: 1. ...
- Lombok介绍和配置
什么是Lombok Lombok是一个Java库,能自动插入编辑器并构建工具,简化Java开发. 官网: https://www.projectlombok.org/ Lombok的作用 通过 添加注 ...
- sqlSession封装以及CRUD的实现
sqlSession封装以及CRUD的实现 封装MyBatisUtil dao 定义方法 映射文件写sql语句 daoimpl实现类 实现方法 test类测试方法 整体结构