好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

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

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

熱門標簽:貴州電銷卡外呼系統 超呼電話機器人 魔獸2青云地圖標注 北京400電話辦理收費標準 鄭州人工智能電銷機器人系統 日本中國地圖標注 山東外呼銷售系統招商 十堰營銷電銷機器人哪家便宜 宿遷便宜外呼系統平臺

背景

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

有幾個配置緩存,上千個flink任務調用,每個任務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("初始數據: {}", 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就會使用壓縮列表來做列表鍵的底層實現。

下面看一下壓縮列表實現的列表鍵:

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

再看一下壓縮列表實現的哈希鍵:

壓縮列表是Redis為了節約內存而開發的,是一系列特殊編碼的連續內存塊組成的順序型數據結構。

一個壓縮列表可以包含任意多個節點,每個節點可以保存一個字節數組或者一個整數值。

看一下壓縮列表的示例:

看一下包含五個節點的壓縮列表:

節點的encoding屬性記錄了節點的content屬性所保存數據的類型以及長度。

節點的content屬性負責保存節點的值,節點值可以是一個字節數組或者整數,值的類型和長度由節點的encoding屬性決定。

連鎖更新:

每個節點的previous_entry_length屬性都記錄了前一個節點的長度,那么當前一個節點的長度從254以下變成254以上時,本節點的存儲前一個節點的長度的previous_entry_length就需要從1字節變為5字節。

那么后面的節點的previous_entry_length屬性也有可能更新。不過連鎖更新的幾率并不大。

總結:

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

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

標簽:北京 大慶 楊凌 果洛 臺州 吉安 朝陽 江蘇

巨人網絡通訊聲明:本文標題《壓縮Redis里的字符串大對象操作》,本文關鍵詞  壓縮,Redis,里,的,字符串,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《壓縮Redis里的字符串大對象操作》相關的同類信息!
  • 本頁收集關于壓縮Redis里的字符串大對象操作的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 快点cao我要被cao烂了| 最新欧美精品一区二区三区不卡| 看**视频一级毛片| 男男生子一受n攻文| 色婷婷综合网| 欧美肥臀bbwbbwbbw| 大乳boobS巨大乳J奶HD| 毛片地址| 五月狠狠精品人妻久久干| 邱淑贞无删减版三级视频| 亚洲综合久久一本伊伊区| 香港三级理论在线影院| 丰满富婆一级A片外国电影| 97人妻精品一区二区三区视频| 538国产在线搬运工视频| 激情床戏网站| 真空超短裙浪荡校花h| 爱爱成人免费视频mv| 男菊扒开h~女攻调教男受| 女性把尿口扒开让男生桶爽| 尤物色| 欧美屁屁影院| 国产麻豆91| 97色伦亚洲自偷| 丰满人妻一区二区三区蜜桃视频| www.妞干网.com| 两人做人爱免费视频看| 公天天吃我奶躁我高H漫画| 欧美日产国产精品视频免费| 国产精品嫩草影院视频| wc女次撤尿tv女次偷拍| 白丝??扒腿自慰爽出白色液体| 国产AV无码区亚洲AV欧美漫画| 亚洲色中色| 水润紧致销魂低吟| 看三级黄色片| 免费国产不卡午夜福在线观看 | 真人啪啪XXOO动态图片GIF| 天美传媒传媒免费观看| 小荡货你夹得我又紧又爽动态图| 久久久久久无码大片A片|