服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - 压缩Redis里的字符串大对象操作

压缩Redis里的字符串大对象操作

2021-08-11 19:07持盾的紫眸 Redis

这篇文章主要介绍了压缩Redis里的字符串大对象操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

背景

redis缓存的字符串过大时会有问题。不超过10kb最好,最大不能超过1mb。

有几个配置缓存,上千个flink任务调用,每个任务5分钟命中一次,大小在5kb到6mb不等,因此需要压缩。

第一种,使用gzip

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
 * 使用gzip压缩字符串
 */
public static string compress(string str) {
    if (str == null || str.length() == 0) {
        return str;
    }
    bytearrayoutputstream out = new bytearrayoutputstream();
    gzipoutputstream gzip = null;
    try {
        gzip = new gzipoutputstream(out);
        gzip.write(str.getbytes());
    } catch (ioexception e) {
        e.printstacktrace();
    } finally {
        if (gzip != null) {
            try {
                gzip.close();
            } catch (ioexception e) {
                e.printstacktrace();
            }
        }
    }
    return new sun.misc.base64encoder().encode(out.tobytearray());
}
 
/**
 * 使用gzip解压缩
 */
public static string uncompress(string compressedstr) {
    if (compressedstr == null || compressedstr.length() == 0) {
        return compressedstr;
    }
 
    bytearrayoutputstream out = new bytearrayoutputstream();
    bytearrayinputstream in = null;
    gzipinputstream ginzip = null;
    byte[] compressed = null;
    string decompressed = null;
    try {
        compressed = new sun.misc.base64decoder().decodebuffer(compressedstr);
        in = new bytearrayinputstream(compressed);
        ginzip = new gzipinputstream(in);
        byte[] buffer = new byte[1024];
        int offset = -1;
        while ((offset = ginzip.read(buffer)) != -1) {
            out.write(buffer, 0, offset);
        }
        decompressed = out.tostring();
    } catch (ioexception e) {
        e.printstacktrace();
    } finally {
        if (ginzip != null) {
            try {
                ginzip.close();
            } catch (ioexception e) {
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (ioexception e) {
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (ioexception e) {
            }
        }
    }
    return decompressed;
}

第二种,使用zstd

?
1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.github.luben/zstd-jni -->
<dependency>
    <groupid>com.github.luben</groupid>
    <artifactid>zstd-jni</artifactid>
    <version>1.4.5-6</version>
</dependency>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
public class configcacheutil {
    private static zstddictcompress compressdict;
    private static zstddictdecompress decompressdict;
    private static final integer level = 5;
    public static void train() throws ioexception {
        // 初始化词典对象
        string dictcontent = fileutils.readfiletostring(new file("/users/yangguang/vscode/text/cache.json"),
            standardcharsets.utf_8);
        byte[] dictbytes = dictcontent.getbytes(standardcharsets.utf_8);
        compressdict = new zstddictcompress(dictbytes, level);
        decompressdict = new zstddictdecompress(dictbytes);
    }
    public static void main(string[] args) throws ioexception {
        string read = fileutils.readfiletostring(new file("/users/yangguang/vscode/text/cache.json"));
        configcacheutil.testgzip(read);
        system.out.println("");
        configcacheutil.test(read.getbytes());
        system.out.println("");
        configcacheutil.testbytrain(read.getbytes());
    }
    public static void testgzip(string str) {
        logger.info("初始数据: {}", str.length());
        // 压缩数据
        long compressbegintime = system.currenttimemillis();
        string compressed = configcacheutil.compress(str);
        long compressendtime = system.currenttimemillis();
        logger.info("压缩耗时: {}", compressendtime - compressbegintime);
        logger.info("数据大小: {}", compressed.length());
        // 解压数据
        long decompressbegintime = system.currenttimemillis();
        // 第 3 个参数不能小于解压后的字节数组的大小
        string decompressed = configcacheutil.uncompress(compressed);
        long decompressendtime = system.currenttimemillis();
        logger.info("解压耗时: {}", decompressendtime - decompressbegintime);
        logger.info("数据大小: {}", decompressed.length());
    }
    
    public static void test(byte[] bytes) {
        logger.info("初始数据: {}", bytes.length);
        // 压缩数据
        long compressbegintime = system.currenttimemillis();
        byte[] compressed = zstd.compress(bytes);
        long compressendtime = system.currenttimemillis();
        logger.info("压缩耗时: {}", compressendtime - compressbegintime);
        logger.info("数据大小: {}", compressed.length);
        // 解压数据
        long decompressbegintime = system.currenttimemillis();
        // 第 3 个参数不能小于解压后的字节数组的大小
        byte[] decompressed = zstd.decompress(compressed, 20 * 1024 * 1024 * 8);
        long decompressendtime = system.currenttimemillis();
        logger.info("解压耗时: {}", decompressendtime - decompressbegintime);
        logger.info("数据大小: {}", decompressed.length);
    }
    public static void testbytrain(byte[] bytes) throws ioexception {
        configcacheutil.train();
        logger.info("初始数据: {}", bytes.length);
        // 压缩数据
        long compressbegintime = system.currenttimemillis();
        byte[] compressed = zstd.compress(bytes, compressdict);
        long compressendtime = system.currenttimemillis();
        logger.info("压缩耗时: {}", compressendtime - compressbegintime);
        logger.info("数据大小: {}", compressed.length);
        // 解压数据
        long decompressbegintime = system.currenttimemillis();
        // 第 3 个参数不能小于解压后的字节数组的大小
        byte[] decompressed = zstd.decompress(compressed, decompressdict, 20 * 1024 * 1024 * 8);
        long decompressendtime = system.currenttimemillis();
        logger.info("解压耗时: {}", decompressendtime - decompressbegintime);
        logger.info("数据大小: {}", decompressed.length);
        compressdict.tostring();
    }
}

输出

5kb

2020-09-08 22:42:48 info configcacheutil:157 - 初始数据: 5541
2020-09-08 22:42:48 info configcacheutil:163 - 压缩耗时: 2
2020-09-08 22:42:48 info configcacheutil:164 - 数据大小: 1236
2020-09-08 22:42:48 info configcacheutil:171 - 解压耗时: 2
2020-09-08 22:42:48 info configcacheutil:172 - 数据大小: 5541

2020-09-08 22:42:48 info configcacheutil:176 - 初始数据: 5541
2020-09-08 22:42:48 info configcacheutil:182 - 压缩耗时: 523
2020-09-08 22:42:48 info configcacheutil:183 - 数据大小: 972
2020-09-08 22:42:48 info configcacheutil:190 - 解压耗时: 85
2020-09-08 22:42:48 info configcacheutil:191 - 数据大小: 5541

2020-09-08 22:42:48 info configcacheutil:196 - 初始数据: 5541
2020-09-08 22:42:48 info configcacheutil:202 - 压缩耗时: 1
2020-09-08 22:42:48 info configcacheutil:203 - 数据大小: 919
2020-09-08 22:42:48 info configcacheutil:210 - 解压耗时: 22
2020-09-08 22:42:48 info configcacheutil:211 - 数据大小: 5541

6mb

2020-09-08 22:44:06 info configcacheutil:158 - 初始数据: 5719269
2020-09-08 22:44:06 info configcacheutil:164 - 压缩耗时: 129
2020-09-08 22:44:06 info configcacheutil:165 - 数据大小: 330090
2020-09-08 22:44:06 info configcacheutil:172 - 解压耗时: 69
2020-09-08 22:44:06 info configcacheutil:173 - 数据大小: 5719269

2020-09-08 22:44:06 info configcacheutil:177 - 初始数据: 5874139
2020-09-08 22:44:06 info configcacheutil:183 - 压缩耗时: 265
2020-09-08 22:44:06 info configcacheutil:184 - 数据大小: 201722
2020-09-08 22:44:06 info configcacheutil:191 - 解压耗时: 81
2020-09-08 22:44:06 info configcacheutil:192 - 数据大小: 5874139

2020-09-08 22:44:06 info configcacheutil:197 - 初始数据: 5874139
2020-09-08 22:44:06 info configcacheutil:203 - 压缩耗时: 42
2020-09-08 22:44:06 info configcacheutil:204 - 数据大小: 115423
2020-09-08 22:44:07 info configcacheutil:211 - 解压耗时: 49
2020-09-08 22:44:07 info configcacheutil:212 - 数据大小: 5874139

redis 压缩列表

压缩列表(ziplist)是列表键和哈希键的底层实现之一。当一个列表键只包含少量列表项,并且每个列表项要么就是小整数值,要么就是长度比较短的字符串,redis就会使用压缩列表来做列表键的底层实现。

下面看一下压缩列表实现的列表键:

压缩Redis里的字符串大对象操作

列表键里面包含的都是1、3、5、10086这样的小整数值,以及''hello''、''world''这样的短字符串。

再看一下压缩列表实现的哈希键:

压缩Redis里的字符串大对象操作

压缩列表是redis为了节约内存而开发的,是一系列特殊编码的连续内存块组成的顺序型数据结构。

一个压缩列表可以包含任意多个节点,每个节点可以保存一个字节数组或者一个整数值。

压缩Redis里的字符串大对象操作

看一下压缩列表的示例:

压缩Redis里的字符串大对象操作

看一下包含五个节点的压缩列表:

压缩Redis里的字符串大对象操作

节点的encoding属性记录了节点的content属性所保存数据的类型以及长度。

节点的content属性负责保存节点的值,节点值可以是一个字节数组或者整数,值的类型和长度由节点的encoding属性决定。

压缩Redis里的字符串大对象操作

连锁更新:

每个节点的previous_entry_length属性都记录了前一个节点的长度,那么当前一个节点的长度从254以下变成254以上时,本节点的存储前一个节点的长度的previous_entry_length就需要从1字节变为5字节。

那么后面的节点的previous_entry_length属性也有可能更新。不过连锁更新的几率并不大。

总结:

压缩Redis里的字符串大对象操作

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zimou5581/article/details/108478940

延伸 · 阅读

精彩推荐
  • Redis《面试八股文》之 Redis十六卷

    《面试八股文》之 Redis十六卷

    redis 作为我们最常用的内存数据库,很多地方你都能够发现它的身影,比如说登录信息的存储,分布式锁的使用,其经常被我们当做缓存去使用。...

    moon聊技术8182021-07-26
  • Redis如何使用Redis锁处理并发问题详解

    如何使用Redis锁处理并发问题详解

    这篇文章主要给大家介绍了关于如何使用Redis锁处理并发问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Redis具有一定的参考学习...

    haofly4522019-11-26
  • RedisRedis 6.X Cluster 集群搭建

    Redis 6.X Cluster 集群搭建

    码哥带大家完成在 CentOS 7 中安装 Redis 6.x 教程。在学习 Redis Cluster 集群之前,我们需要先搭建一套集群环境。机器有限,实现目标是一台机器上搭建 6 个节...

    码哥字节15752021-04-07
  • Redis详解三分钟快速搭建分布式高可用的Redis集群

    详解三分钟快速搭建分布式高可用的Redis集群

    这篇文章主要介绍了详解三分钟快速搭建分布式高可用的Redis集群,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    万猫学社4502021-07-25
  • Redisredis缓存存储Session原理机制

    redis缓存存储Session原理机制

    这篇文章主要为大家介绍了redis缓存存储Session原理机制详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    程序媛张小妍9252021-11-25
  • Redis关于Redis数据库入门详细介绍

    关于Redis数据库入门详细介绍

    大家好,本篇文章主要讲的是关于Redis数据库入门详细介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览...

    沃尔码6982022-01-24
  • RedisRedis Template实现分布式锁的实例代码

    Redis Template实现分布式锁的实例代码

    这篇文章主要介绍了Redis Template实现分布式锁,需要的朋友可以参考下 ...

    晴天小哥哥2592019-11-18
  • RedisRedis集群的5种使用方式,各自优缺点分析

    Redis集群的5种使用方式,各自优缺点分析

    Redis 多副本,采用主从(replication)部署结构,相较于单副本而言最大的特点就是主从实例间数据实时同步,并且提供数据持久化和备份策略。...

    优知学院4082021-08-10