2026년 1월 14일 수요일

zstd redis 처리

 // 저장 시

byte[] data = originalContent.getBytes(StandardCharsets.UTF_8);

int originalLength = data.length;

byte[] compressed = Zstd.compress(data);


// [4바이트(길이) + 압축데이터] 형태로 병합

ByteBuffer buffer = ByteBuffer.allocate(4 + compressed.length);

buffer.putInt(originalLength);

buffer.put(compressed);

jedis.set(key.getBytes(), buffer.array());


// 조회 시

byte[] raw = jedis.get(key.getBytes());

ByteBuffer wrapped = ByteBuffer.wrap(raw);

int length = wrapped.getInt(); // 앞의 4바이트 읽기

byte[] compressedPart = new byte[raw.length - 4];

wrapped.get(compressedPart);   // 나머지 데이터 읽기


byte[] decompressed = Zstd.decompress(compressedPart, length);


댓글 없음: