使用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 ...
随机推荐
- ubuntu 20.04 / 22.04 运行32位程序
sudo dpkg --add-architecture i386 sudo apt install libc6:i386 libstdc++6:i386 sudo apt-get update su ...
- 【离线数仓】Day01-用户行为数据采集:数仓概念、需求及架构、数据生成及采集、linux命令及其他组件常见知识
一.数据仓库概念 二.项目需求及架构设计 1.需求分析 2.项目框架 3.框架版本选型 服务器选型:云主机 服务器规划 三.数据生成模块 1.数据基本格式 公共字段:所有手机都包含 业务字段:埋点上报 ...
- include指令和include动作的区别
include指令和<jsp:include>动作标识的区别 1.include指令通过file属性指定被包含的文件,并且file属性不支持任何表达式: <jsp:include&g ...
- feDisplacementMap滤镜实现水波纹效果,计算动态值。
参考资料 https://www.zhangxinxu.com/wordpress/2017/12/understand-svg-fedisplacementmap-filter/ 该文章已经讲的特别 ...
- ubuntu1804搭建FTP服务器的方法
搭建FTP服务器 FTP的工作原理: FTP:File Transfer Protocol ,文件传输协议.属于NAS存储的一种协议,基于CS结构. ftp采用的是双端口模式,分为命令端口和数据端口, ...
- ob_DES_艺恩
网站 aHR0cHM6Ly93d3cuZW5kYXRhLmNvbS5jbi9Cb3hPZmZpY2UvQk8vWWVhci9pbmRleC5odG1s 抓包返回密文 点到initator,PostA ...
- JavaScript:操作符:算术运算符(加减乘除模幂)及其隐式转换数据类型
加法+ 减法- 乘法* 除法/ 模运算% 幂运算**,即a ** b求的是a的b次方 执行上述运算时,当两个操作数有非数字时,JS会隐式转换为数字,再进行运算: 一些特殊的非数字,会进行如下转换: t ...
- APIO2022 游记
Day 0 有人刚登记完房间就把房卡落在房间里了我不说是谁(真不是我,不信去问jth) 下午把gen把模拟赛的题补了一下,T3是个不太可做的虚树上淀粉质dp,先咕着. Day 1 上午来的比较晚,没有 ...
- emqtt-bench
安装 安装环境:Centos7 安装包:emqtt-bench-0.4.6-alpha.2-centos7-amd64.tar.gz 建议使用已编译好的发行包进行安装,源码编译已踩坑. # 创建存放目 ...
- SSM框架——MyBatis
Mybatis 1.Mybatis的使用 1.1给项目导入相关依赖 我这里有几个下载好的依赖包提供给大家 点我下载--junit4.13.2 点我下载--maven3.8.1 点我下载--mybati ...