You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot下Caffeine Cache存储压缩实现方案咨询

为Caffeine Cache实现值压缩的方案建议

核心思路

通过在缓存写入前对值进行压缩、读取后解压的方式,减少缓存占用的内存空间,缓解3万元素带来的性能压力。关键是将压缩逻辑与Caffeine的缓存操作无缝集成,同时选择对性能影响最小的压缩算法。

具体实现步骤

1. 选择合适的压缩算法

优先选择高速轻量型压缩算法,避免因压缩解压带来过多CPU开销:

  • 推荐使用Snappy或LZ4,二者的压缩/解压速度远超Gzip,适合缓存这种对响应速度敏感的场景。
  • 注意:若缓存对象本身小于几百字节,压缩反而会增加额外开销,建议先评估对象大小,选择性启用压缩。

2. 实现压缩工具类

封装序列化、压缩、解压、反序列化的通用方法,确保缓存对象能被正确转换:

import org.xerial.snappy.Snappy;
import java.io.*;

public class CacheCompressionUtils {

    // 序列化对象并压缩
    public static byte[] compress(Object value) throws IOException {
        if (value == null) return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
            oos.writeObject(value);
        }
        return Snappy.compress(baos.toByteArray());
    }

    // 解压并反序列化对象
    public static Object decompress(byte[] compressedData) throws IOException, ClassNotFoundException {
        if (compressedData == null) return null;
        byte[] decompressed = Snappy.uncompress(compressedData);
        ByteArrayInputStream bais = new ByteArrayInputStream(decompressed);
        try (ObjectInputStream ois = new ObjectInputStream(bais)) {
            return ois.readObject();
        }
    }
}

3. 修改Caffeine缓存配置,集成压缩逻辑

自定义CaffeineCache的实现,覆盖put和get方法,自动处理压缩解压:

@Configuration
@EnableCaching
public class CachingConfig {

    @Bean
    public CacheManager cacheManager() {
        CaffeineCacheManager cacheManager = new CaffeineCacheManager() {
            @Override
            protected Cache<Object, Object> createCaffeineCache(String name) {
                // 保留原有Caffeine配置
                Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
                        .expireAfterWrite(10, TimeUnit.MINUTES)
                        .maximumSize(30000)
                        .recordStats();

                // 构建底层Caffeine缓存实例
                com.github.benmanes.caffeine.cache.Cache<Object, Object> caffeineCache = caffeine.build();

                // 自定义缓存操作,嵌入压缩逻辑
                return new CaffeineCache(name, caffeineCache) {
                    @Override
                    public ValueWrapper get(Object key) {
                        ValueWrapper rawWrapper = super.get(key);
                        if (rawWrapper == null) return null;
                        try {
                            Object decompressedValue = CacheCompressionUtils.decompress((byte[]) rawWrapper.get());
                            return () -> decompressedValue;
                        } catch (Exception e) {
                            throw new RuntimeException("缓存值解压失败", e);
                        }
                    }

                    @Override
                    public void put(Object key, Object value) {
                        try {
                            byte[] compressedValue = CacheCompressionUtils.compress(value);
                            super.put(key, compressedValue);
                        } catch (Exception e) {
                            throw new RuntimeException("缓存值压缩失败", e);
                        }
                    }

                    @Override
                    public <T> T get(Object key, Class<T> type) {
                        ValueWrapper wrapper = get(key);
                        return wrapper != null ? type.cast(wrapper.get()) : null;
                    }
                };
            }
        };
        cacheManager.setCacheNames(Collections.singletonList("my_cache"));
        return cacheManager;
    }
}

4. 依赖与额外注意事项

  • 添加Snappy依赖(Maven示例):
<dependency>
    <groupId>org.xerial.snappy</groupId>
    <artifactId>snappy-java</artifactId>
    <version>1.1.10.1</version>
</dependency>
  • 确保所有存入缓存的对象都实现了Serializable接口,否则无法序列化压缩。
  • 可添加大小判断逻辑,仅对超过阈值(如1KB)的对象进行压缩,避免小对象的无效压缩。
  • 利用Caffeine的recordStats()监控压缩前后的缓存命中率、内存占用及响应时间,验证优化效果。

内容的提问来源于stack exchange,提问作者manjosh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 15:02:01