背景

在高并发的场景中,我们通常会使用缓存提升性能。在使用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压缩以提升性能的更多相关文章

  1. springboot整合redis缓存

    使用springBoot添加redis缓存需要在POM文件里引入 org.springframework.bootspring-boot-starter-cacheorg.springframewor ...

  2. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  3. SpringBoot整合redis缓存(一)

    准备工作 1.Linux系统 2.安装redis(也可以安装docker,然后再docker中装redis,本文章就直接用Linux安装redis做演示) redis下载地址: 修改redis,开启远 ...

  4. Java SpringBoot使用Redis缓存和Ehcache

    <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http:// ...

  5. springboot整合redis缓存一些知识点

    前言 最近在做智能家居平台,考虑到家居的控制需要快速的响应于是打算使用redis缓存.一方面减少数据库压力另一方面又能提高响应速度.项目中使用的技术栈基本上都是大家熟悉的springboot全家桶,在 ...

  6. SpringBoot使用redis缓存List<Object>

    一.概述 最近在做性能优化,之前有一个业务是这样实现的: 1.温度报警后第三方通讯管理机直接把报警信息保存到数据库 2.我们在数据库中添加触发器,(BEFORE INSERT)根据这条报警信息处理业务 ...

  7. SpringBoot(七) - Redis 缓存

    1.五大基本数据类型和操作 1.1 字符串-string 命令 说明 set key value 如果key还没有,那就可以添加,如果key已经存在了,那会覆盖原有key的值 get key 如果ke ...

  8. SpringBoot 整合 Redis缓存

    在我们的日常项目开发过程中缓存是无处不在的,因为它可以极大的提高系统的访问速度,关于缓存的框架也种类繁多,今天主要介绍的是使用现在非常流行的NoSQL数据库(Redis)来实现我们的缓存需求. Spr ...

  9. springboot集成redis缓存

    1.pom.xml增加redis缓存起步依赖(spring-boot-starter-parent包含许多starter版本) <dependency> <groupId>or ...

  10. spring Cache /Redis 缓存 + Spring 的集成示例

    spring Cache https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ spring+redis 缓存 ht ...

随机推荐

  1. 进军东南亚市场,腾讯云数据库 TDSQL 助力印尼 BNC 银行数字化转型

    腾讯云数据库在助力金融核心系统分布式替换上,已经辐射到了东南亚市场. 东南亚最大的银行之一印尼BNC银行(Bank Neo Commerce)已正式完成新核心分布式迁移,使用腾讯云数据库TDSQL后, ...

  2. day20 关联查询与多表联查 & 子查询与union联合查询 & 数据库定义语言DDL

    day20 关联查询 #左连接:表名 left join 表名 以左表为主表,只显示与左表能匹配的行 SELECT s.*,q.* FROM student AS s LEFT JOIN queue_ ...

  3. 【SQL】窗口函数:求数据组内累计值和累计百分比

    〇.概述 1.所需资料 窗口函数实现组内百分比.累计值.累计百分比:https://blog.csdn.net/weixin_39751959/article/details/88828922 2.背 ...

  4. gulp4.0构建任务

    执行default任务时,依次执行以下任务 gulp.task('default', ['htmlmin', 'cssmin', 'jsmin', 'copy']); 报错:Task function ...

  5. 02-线性结构4 Pop Sequence (25分)

    02-线性结构4 Pop Sequence (25分) Given a stack which can keep M numbers at most. Push N numbers in the or ...

  6. SpringBoot内置tomcat启动过程及原理

    作者:李岩科 1 背景 SpringBoot 是一个框架,一种全新的编程规范,他的产生简化了框架的使用,同时也提供了很多便捷的功能,比如内置 tomcat 就是其中一项,他让我们省去了搭建 tomca ...

  7. VS2019发布至远程IIS部署流程

    服务器部署 传统的开发将项目发布至本地桌面之后,复制至站点目录或通过FTP上传站点目录,有点小麻烦,通过开发工具VS2019本身集成的功能,可以一步到发布到远程IIS站点. 条件: VS系列发工具,例 ...

  8. 【JVM】经典垃圾回收器

    本文已收录至Github,推荐阅读 Java随想录 微信公众号:Java随想录 CSDN: 码农BookSea 转载请在文首注明出处,如发现恶意抄袭/搬运,会动用法律武器维护自己的权益.让我们一起维护 ...

  9. JavaScript:函数:函数的返回值

    调用函数后,总是有返回值的: 使用return关键字进行返回,返回的结果,我们需要用变量来存储; 如果没有使用return语句,或者只有return这一个裸关键字,那么返回的结果是undefined: ...

  10. python31 网络并发编程方法

    同步与异步 用来表达任务的提交方式 同步 提交完任务之后原地等待任务的返回结果 期间不做任何事 异步 提交完任务之后不原地等待任务的返回结果 直接去做其他事 有结果自动通知 阻塞与非阻塞 用来表达任务 ...