主頁 > 知識庫 > 壓縮Redis里的字符串大對象操作

壓縮Redis里的字符串大對象操作

熱門標(biāo)簽:AI電銷 鐵路電話系統(tǒng) 服務(wù)外包 呼叫中心市場需求 Linux服務(wù)器 百度競價排名 地方門戶網(wǎng)站 網(wǎng)站排名優(yōu)化

背景

Redis緩存的字符串過大時會有問題。不超過10KB最好,最大不能超過1MB。

有幾個配置緩存,上千個flink任務(wù)調(diào)用,每個任務(wù)5分鐘命中一次,大小在5KB到6MB不等,因此需要壓縮。

第一種,使用gzip

/**
 * 使用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

        !-- 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>
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("初始數(shù)據(jù): {}", str.length());
        // 壓縮數(shù)據(jù)
        long compressBeginTime = System.currentTimeMillis();
        String compressed = ConfigCacheUtil.compress(str);
        long compressEndTime = System.currentTimeMillis();
        logger.info("壓縮耗時: {}", compressEndTime - compressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", compressed.length());
        // 解壓數(shù)據(jù)
        long decompressBeginTime = System.currentTimeMillis();
        // 第 3 個參數(shù)不能小于解壓后的字節(jié)數(shù)組的大小
        String decompressed = ConfigCacheUtil.uncompress(compressed);
        long decompressEndTime = System.currentTimeMillis();
        logger.info("解壓耗時: {}", decompressEndTime - decompressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", decompressed.length());
    }
    
    public static void test(byte[] bytes) {
        logger.info("初始數(shù)據(jù): {}", bytes.length);
        // 壓縮數(shù)據(jù)
        long compressBeginTime = System.currentTimeMillis();
        byte[] compressed = Zstd.compress(bytes);
        long compressEndTime = System.currentTimeMillis();
        logger.info("壓縮耗時: {}", compressEndTime - compressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", compressed.length);
        // 解壓數(shù)據(jù)
        long decompressBeginTime = System.currentTimeMillis();
        // 第 3 個參數(shù)不能小于解壓后的字節(jié)數(shù)組的大小
        byte[] decompressed = Zstd.decompress(compressed, 20 * 1024 * 1024 * 8);
        long decompressEndTime = System.currentTimeMillis();
        logger.info("解壓耗時: {}", decompressEndTime - decompressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", decompressed.length);
    }
    public static void testByTrain(byte[] bytes) throws IOException {
        ConfigCacheUtil.train();
        logger.info("初始數(shù)據(jù): {}", bytes.length);
        // 壓縮數(shù)據(jù)
        long compressBeginTime = System.currentTimeMillis();
        byte[] compressed = Zstd.compress(bytes, compressDict);
        long compressEndTime = System.currentTimeMillis();
        logger.info("壓縮耗時: {}", compressEndTime - compressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", compressed.length);
        // 解壓數(shù)據(jù)
        long decompressBeginTime = System.currentTimeMillis();
        // 第 3 個參數(shù)不能小于解壓后的字節(jié)數(shù)組的大小
        byte[] decompressed = Zstd.decompress(compressed, decompressDict, 20 * 1024 * 1024 * 8);
        long decompressEndTime = System.currentTimeMillis();
        logger.info("解壓耗時: {}", decompressEndTime - decompressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", decompressed.length);
        compressDict.toString();
    }
}

輸出

5KB

2020-09-08 22:42:48 INFO ConfigCacheUtil:157 - 初始數(shù)據(jù): 5541
2020-09-08 22:42:48 INFO ConfigCacheUtil:163 - 壓縮耗時: 2
2020-09-08 22:42:48 INFO ConfigCacheUtil:164 - 數(shù)據(jù)大小: 1236
2020-09-08 22:42:48 INFO ConfigCacheUtil:171 - 解壓耗時: 2
2020-09-08 22:42:48 INFO ConfigCacheUtil:172 - 數(shù)據(jù)大小: 5541

2020-09-08 22:42:48 INFO ConfigCacheUtil:176 - 初始數(shù)據(jù): 5541
2020-09-08 22:42:48 INFO ConfigCacheUtil:182 - 壓縮耗時: 523
2020-09-08 22:42:48 INFO ConfigCacheUtil:183 - 數(shù)據(jù)大小: 972
2020-09-08 22:42:48 INFO ConfigCacheUtil:190 - 解壓耗時: 85
2020-09-08 22:42:48 INFO ConfigCacheUtil:191 - 數(shù)據(jù)大小: 5541

2020-09-08 22:42:48 INFO ConfigCacheUtil:196 - 初始數(shù)據(jù): 5541
2020-09-08 22:42:48 INFO ConfigCacheUtil:202 - 壓縮耗時: 1
2020-09-08 22:42:48 INFO ConfigCacheUtil:203 - 數(shù)據(jù)大小: 919
2020-09-08 22:42:48 INFO ConfigCacheUtil:210 - 解壓耗時: 22
2020-09-08 22:42:48 INFO ConfigCacheUtil:211 - 數(shù)據(jù)大小: 5541

6MB

2020-09-08 22:44:06 INFO ConfigCacheUtil:158 - 初始數(shù)據(jù): 5719269
2020-09-08 22:44:06 INFO ConfigCacheUtil:164 - 壓縮耗時: 129
2020-09-08 22:44:06 INFO ConfigCacheUtil:165 - 數(shù)據(jù)大小: 330090
2020-09-08 22:44:06 INFO ConfigCacheUtil:172 - 解壓耗時: 69
2020-09-08 22:44:06 INFO ConfigCacheUtil:173 - 數(shù)據(jù)大小: 5719269

2020-09-08 22:44:06 INFO ConfigCacheUtil:177 - 初始數(shù)據(jù): 5874139
2020-09-08 22:44:06 INFO ConfigCacheUtil:183 - 壓縮耗時: 265
2020-09-08 22:44:06 INFO ConfigCacheUtil:184 - 數(shù)據(jù)大小: 201722
2020-09-08 22:44:06 INFO ConfigCacheUtil:191 - 解壓耗時: 81
2020-09-08 22:44:06 INFO ConfigCacheUtil:192 - 數(shù)據(jù)大小: 5874139

2020-09-08 22:44:06 INFO ConfigCacheUtil:197 - 初始數(shù)據(jù): 5874139
2020-09-08 22:44:06 INFO ConfigCacheUtil:203 - 壓縮耗時: 42
2020-09-08 22:44:06 INFO ConfigCacheUtil:204 - 數(shù)據(jù)大小: 115423
2020-09-08 22:44:07 INFO ConfigCacheUtil:211 - 解壓耗時: 49
2020-09-08 22:44:07 INFO ConfigCacheUtil:212 - 數(shù)據(jù)大小: 5874139

Redis 壓縮列表

壓縮列表(ziplist)是列表鍵和哈希鍵的底層實現(xiàn)之一。當(dāng)一個列表鍵只包含少量列表項,并且每個列表項要么就是小整數(shù)值,要么就是長度比較短的字符串,Redis就會使用壓縮列表來做列表鍵的底層實現(xiàn)。

下面看一下壓縮列表實現(xiàn)的列表鍵:

列表鍵里面包含的都是1、3、5、10086這樣的小整數(shù)值,以及''hello''、''world''這樣的短字符串。

再看一下壓縮列表實現(xiàn)的哈希鍵:

壓縮列表是Redis為了節(jié)約內(nèi)存而開發(fā)的,是一系列特殊編碼的連續(xù)內(nèi)存塊組成的順序型數(shù)據(jù)結(jié)構(gòu)。

一個壓縮列表可以包含任意多個節(jié)點(diǎn),每個節(jié)點(diǎn)可以保存一個字節(jié)數(shù)組或者一個整數(shù)值。

看一下壓縮列表的示例:

看一下包含五個節(jié)點(diǎn)的壓縮列表:

節(jié)點(diǎn)的encoding屬性記錄了節(jié)點(diǎn)的content屬性所保存數(shù)據(jù)的類型以及長度。

節(jié)點(diǎn)的content屬性負(fù)責(zé)保存節(jié)點(diǎn)的值,節(jié)點(diǎn)值可以是一個字節(jié)數(shù)組或者整數(shù),值的類型和長度由節(jié)點(diǎn)的encoding屬性決定。

連鎖更新:

每個節(jié)點(diǎn)的previous_entry_length屬性都記錄了前一個節(jié)點(diǎn)的長度,那么當(dāng)前一個節(jié)點(diǎn)的長度從254以下變成254以上時,本節(jié)點(diǎn)的存儲前一個節(jié)點(diǎn)的長度的previous_entry_length就需要從1字節(jié)變?yōu)?字節(jié)。

那么后面的節(jié)點(diǎn)的previous_entry_length屬性也有可能更新。不過連鎖更新的幾率并不大。

總結(jié):

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 詳解redis數(shù)據(jù)結(jié)構(gòu)之壓縮列表
  • Redis字符串對象實用筆記
  • 壓縮列表犧牲速度來節(jié)省內(nèi)存,Redis是膨脹了嗎
  • Redis字符串原理的深入理解

標(biāo)簽:仙桃 湘潭 崇左 衡水 湖南 蘭州 銅川 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《壓縮Redis里的字符串大對象操作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266