欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

壓縮Redis里的字符串大對(duì)象操作

 更新時(shí)間:2021年06月23日 08:56:29   作者:持盾的紫眸  
這篇文章主要介紹了壓縮Redis里的字符串大對(duì)象操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景

Redis緩存的字符串過(guò)大時(shí)會(huì)有問(wèn)題。不超過(guò)10KB最好,最大不能超過(guò)1MB。

有幾個(gè)配置緩存,上千個(gè)flink任務(wù)調(diào)用,每個(gè)任務(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 {
        // 初始化詞典對(duì)象
        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("壓縮耗時(shí): {}", compressEndTime - compressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", compressed.length());
        // 解壓數(shù)據(jù)
        long decompressBeginTime = System.currentTimeMillis();
        // 第 3 個(gè)參數(shù)不能小于解壓后的字節(jié)數(shù)組的大小
        String decompressed = ConfigCacheUtil.uncompress(compressed);
        long decompressEndTime = System.currentTimeMillis();
        logger.info("解壓耗時(shí): {}", 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("壓縮耗時(shí): {}", compressEndTime - compressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", compressed.length);
        // 解壓數(shù)據(jù)
        long decompressBeginTime = System.currentTimeMillis();
        // 第 3 個(gè)參數(shù)不能小于解壓后的字節(jié)數(shù)組的大小
        byte[] decompressed = Zstd.decompress(compressed, 20 * 1024 * 1024 * 8);
        long decompressEndTime = System.currentTimeMillis();
        logger.info("解壓耗時(shí): {}", 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("壓縮耗時(shí): {}", compressEndTime - compressBeginTime);
        logger.info("數(shù)據(jù)大小: {}", compressed.length);
        // 解壓數(shù)據(jù)
        long decompressBeginTime = System.currentTimeMillis();
        // 第 3 個(gè)參數(shù)不能小于解壓后的字節(jié)數(shù)組的大小
        byte[] decompressed = Zstd.decompress(compressed, decompressDict, 20 * 1024 * 1024 * 8);
        long decompressEndTime = System.currentTimeMillis();
        logger.info("解壓耗時(shí): {}", 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 - 壓縮耗時(shí): 2
2020-09-08 22:42:48 INFO ConfigCacheUtil:164 - 數(shù)據(jù)大小: 1236
2020-09-08 22:42:48 INFO ConfigCacheUtil:171 - 解壓耗時(shí): 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 - 壓縮耗時(shí): 523
2020-09-08 22:42:48 INFO ConfigCacheUtil:183 - 數(shù)據(jù)大小: 972
2020-09-08 22:42:48 INFO ConfigCacheUtil:190 - 解壓耗時(shí): 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 - 壓縮耗時(shí): 1
2020-09-08 22:42:48 INFO ConfigCacheUtil:203 - 數(shù)據(jù)大小: 919
2020-09-08 22:42:48 INFO ConfigCacheUtil:210 - 解壓耗時(shí): 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 - 壓縮耗時(shí): 129
2020-09-08 22:44:06 INFO ConfigCacheUtil:165 - 數(shù)據(jù)大小: 330090
2020-09-08 22:44:06 INFO ConfigCacheUtil:172 - 解壓耗時(shí): 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 - 壓縮耗時(shí): 265
2020-09-08 22:44:06 INFO ConfigCacheUtil:184 - 數(shù)據(jù)大小: 201722
2020-09-08 22:44:06 INFO ConfigCacheUtil:191 - 解壓耗時(shí): 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 - 壓縮耗時(shí): 42
2020-09-08 22:44:06 INFO ConfigCacheUtil:204 - 數(shù)據(jù)大小: 115423
2020-09-08 22:44:07 INFO ConfigCacheUtil:211 - 解壓耗時(shí): 49
2020-09-08 22:44:07 INFO ConfigCacheUtil:212 - 數(shù)據(jù)大小: 5874139

Redis 壓縮列表

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

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

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

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

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

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

看一下壓縮列表的示例:

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

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

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

連鎖更新:

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

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

總結(jié):

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

相關(guān)文章

  • Redis如何統(tǒng)計(jì)用戶訪問(wèn)量

    Redis如何統(tǒng)計(jì)用戶訪問(wèn)量

    這篇文章主要介紹了Redis如何統(tǒng)計(jì)用戶訪問(wèn)量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 詳解redis desktop manager安裝及連接方式

    詳解redis desktop manager安裝及連接方式

    這篇文章主要介紹了redis desktop manager安裝及連接方式,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • Redis中List實(shí)現(xiàn)雙鏈表

    Redis中List實(shí)現(xiàn)雙鏈表

    本文主要介紹了Redis中List實(shí)現(xiàn)雙鏈表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 在Centos?8.0中安裝Redis服務(wù)器的教程詳解

    在Centos?8.0中安裝Redis服務(wù)器的教程詳解

    由于考慮到linux服務(wù)器的性能,所以經(jīng)常需要把一些中間件安裝在linux服務(wù)上,今天通過(guò)本文給大家介紹下在Centos?8.0中安裝Redis服務(wù)器的詳細(xì)過(guò)程,感興趣的朋友一起看看吧
    2022-03-03
  • Redis數(shù)組和鏈表深入詳解

    Redis數(shù)組和鏈表深入詳解

    這篇文章主要介紹了Redis數(shù)組和鏈表深入詳解,這是redis的基礎(chǔ)的知識(shí)點(diǎn),有感興趣的同學(xué)可以學(xué)習(xí)下
    2021-03-03
  • 詳解redis集群的三種方式

    詳解redis集群的三種方式

    Redis三種集群方式分別是主從復(fù)制,哨兵模式,Cluster集群,這篇文章主要介紹了redis集群的三種方式,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • 詳解Redis分布式鎖的原理與實(shí)現(xiàn)

    詳解Redis分布式鎖的原理與實(shí)現(xiàn)

    在單體應(yīng)用中,如果我們對(duì)共享數(shù)據(jù)不進(jìn)行加鎖操作,會(huì)出現(xiàn)數(shù)據(jù)一致性問(wèn)題,我們的解決辦法通常是加鎖。下面我們一起聊聊使用redis來(lái)實(shí)現(xiàn)分布式鎖
    2022-06-06
  • 使用Redis獲取數(shù)據(jù)轉(zhuǎn)json,解決動(dòng)態(tài)泛型傳參的問(wèn)題

    使用Redis獲取數(shù)據(jù)轉(zhuǎn)json,解決動(dòng)態(tài)泛型傳參的問(wèn)題

    這篇文章主要介紹了使用Redis獲取數(shù)據(jù)轉(zhuǎn)json,解決動(dòng)態(tài)泛型傳參的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • 安裝Redis就那么幾步,很簡(jiǎn)單

    安裝Redis就那么幾步,很簡(jiǎn)單

    Redis是一種非關(guān)系型數(shù)據(jù)庫(kù)(NoSQL),NoSQL是以key-value的形式存儲(chǔ),和傳統(tǒng)的關(guān)系型數(shù)據(jù)庫(kù)不一樣,不一定遵循傳統(tǒng)數(shù)據(jù)庫(kù)的一些基本要求,本文重點(diǎn)給大家介紹安裝Redis的步驟,需要的朋友參考下吧
    2018-11-11
  • redis 實(shí)現(xiàn)登陸次數(shù)限制的思路詳解

    redis 實(shí)現(xiàn)登陸次數(shù)限制的思路詳解

    這篇文章主要介紹了redis 實(shí)現(xiàn)登陸次數(shù)限制的思路詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評(píng)論