在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
背景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
6MB
Redis 压缩列表
|
请发表评论