当前位置:   article > 正文

redis — 如何将redis内存使用量压缩一半(四)_msgpackredisserializer 依赖

msgpackredisserializer 依赖

 

1.背景说明

redis存储有固定内存,如果以某种方式达到其内存极限,我们的系统将开始出现故障,Redis内存使用量可能会成为瓶颈。

使用最新版本的Spring Boot,有两个主要依赖项- Spring Boot Web和Spring Data Reactive Redis,Spring Data Reactive Redis将用于连接和使用Redis的内部应用程序。从本质上讲,Redis依赖项默认使用Lettuce Redis客户端,并且受最新版本的Spring Boot支持。

2.MessagePack

降低内存使用量使用MessagePack,MessagePack工作原理可以参照官网说明MsgPack spec.md,其特征以官方msgpack官网用一句话总结:It’s like JSON.but fast and small

引入依赖,在pom.xml文件中添加以下依赖项:

  1. <!--redis内存压缩-->
  2. <dependency>
  3. <groupId>org.msgpack</groupId>
  4. <artifactId>msgpack-core</artifactId>
  5. <version>0.8.20</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.msgpack</groupId>
  9. <artifactId>jackson-dataformat-msgpack</artifactId>
  10. <version>0.8.20</version>
  11. </dependency>

创建了一个名为控制器MsgPackController:

  1. class MsgPackRedisSerializer<T> implements RedisSerializer<T> {
  2. public static final Charset DEFAULT_CHARSET;
  3. private final JavaType javaType;
  4. private ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory())
  5. .registerModules(new Jdk8Module(), new JavaTimeModule())
  6. .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
  7. .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
  8. .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  9. .setSerializationInclusion(JsonInclude.Include.NON_NULL);
  10. public MsgPackRedisSerializer(Class<T> type) {
  11. this.javaType = JavaTypeHandler.getJavaType(type);
  12. }
  13. public T deserialize(@Nullable byte[] bytes) throws SerializationException {
  14. if (bytes == null || bytes.length == 0) {
  15. return null;
  16. } else {
  17. try {
  18. return this.objectMapper.readValue(bytes, 0, bytes.length, this.javaType);
  19. } catch (Exception ex) {
  20. throw new SerializationException("Could not read MsgPack JSON: " + ex.getMessage(), ex);
  21. }
  22. }
  23. }
  24. public byte[] serialize(@Nullable Object value) throws SerializationException {
  25. if (value == null) {
  26. return new byte[0];
  27. } else {
  28. try {
  29. return this.objectMapper.writeValueAsBytes(value);
  30. } catch (Exception ex) {
  31. throw new SerializationException("Could not write MsgPack JSON: " + ex.getMessage(), ex);
  32. }
  33. }
  34. }
  35. static {
  36. DEFAULT_CHARSET = StandardCharsets.UTF_8;
  37. }
  38. }

实例MessagePackFactory被传递到中ObjectMapper。这将充当Redis和我们的Spring Boot应用程序之间数据的二进制格式和字符串格式之间的桥梁。

3.压缩

比较dataset.bytes当前内存与先前记录的内存,使用率将减少一半,我们可以进一步减少它。

  • Snappy压缩算法

它不旨在最大程度地压缩,也不旨在与任何其他压缩库兼容。相反,它的目标是非常高的速度和合理的压缩。

使用Snappy就像在中添加依赖项一样简单pom.xml,并且几行代码更改。只需Snappy.compress在序列化和Snappy.decompress反序列化时添加即可:

  1. <dependency>
  2. <groupId>org.xerial.snappy</groupId>
  3. <artifactId>snappy-java</artifactId>
  4. <version>1.1.7.3</version>
  5. </dependency>

配置redis序列化配置类:

  1. public class SnappyMsgPackRedisSerializer<T> implements RedisSerializer<T> {
  2. public static final Charset DEFAULT_CHARSET;
  3. private final JavaType javaType;
  4. private ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory())
  5. .registerModules(new Jdk8Module(), new JavaTimeModule())
  6. .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true)
  7. .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
  8. .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
  9. .setSerializationInclusion(JsonInclude.Include.NON_NULL);
  10. public SnappyMsgPackRedisSerializer(Class<T> type) {
  11. this.javaType = JavaTypeHandler.getJavaType(type);
  12. }
  13. @Override
  14. public T deserialize(@Nullable byte[] bytes) throws SerializationException {
  15. if (bytes == null || bytes.length == 0) {
  16. return null;
  17. } else {
  18. try {
  19. final byte[] uncompressBytes = Snappy.uncompress(bytes); //解压
  20. return this.objectMapper.readValue(uncompressBytes, 0, uncompressBytes.length, this.javaType);
  21. } catch (Exception ex) {
  22. throw new SerializationException("Could not read MsgPack JSON: " + ex.getMessage(), ex);
  23. }
  24. }
  25. }
  26. @Override
  27. public byte[] serialize(@Nullable Object value) throws SerializationException {
  28. if (value == null) {
  29. return new byte[0];
  30. } else {
  31. try {
  32. final byte[] bytes = this.objectMapper.writeValueAsBytes(value);
  33. return Snappy.compress(bytes); //压缩
  34. } catch (Exception ex) {
  35. throw new SerializationException("Could not write MsgPack JSON: " + ex.getMessage(), ex);
  36. }
  37. }
  38. }
  39. static {
  40. DEFAULT_CHARSET = StandardCharsets.UTF_8;
  41. }
  42. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/538574
推荐阅读
相关标签
  

闽ICP备14008679号