当前位置: 首页 > news >正文

Guava Cache 实战:构建高并发场景下的字典数据缓存

一、场景背景

在系统开发中,字典数据(如状态类型、分类数据)具有以下特点:

  • 高频读取(每个请求都可能涉及)
  • 低频变化(管理员修改后才会变更)
  • 数据一致性要求适中(允许分钟级延迟)
  • 传统方案每次查询数据库的方式会造成性能瓶颈,本文展示如何基于 Guava Cache 构建缓存层。

二、技术选型分析

为什么选择 Guava Cache?

  • 轻量级:无需引入 Redis 等中间件
  • 自动加载:提供 LoadingCache 自动回源能力
  • 灵活策略:支持基于时间/权重的淘汰策略
  • 线程安全:内置并发控制机制

三、实战代码解析

1.相关代码文件

KeyValue

@Data
@NoArgsConstructor
@AllArgsConstructor
public class KeyValue<K, V> implements Serializable {

    private K key;
    private V value;

}

CacheUtils

public class CacheUtils {
    // 异步刷新缓存(适合全局数据)
    public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) {
        return CacheBuilder.newBuilder()
                .refreshAfterWrite(duration) // 写后刷新时间
                .build(CacheLoader.asyncReloading(loader, Executors.newCachedThreadPool()));
    }
    
    // 同步刷新缓存(适合用户关联数据)
    public static <K, V> LoadingCache<K, V> buildCache(Duration duration, CacheLoader<K, V> loader) {
        return CacheBuilder.newBuilder()
                .refreshAfterWrite(duration)
                .build(loader);
    }
}

DictFrameworkUtils

public class DictFrameworkUtils {

    private static DictDataApi dictDataApi;

    private static final DictDataRespDTO DICT_DATA_NULL = new DictDataRespDTO();

    /**
     * 针对 {@link #getDictDataLabel(String, String)} 的缓存
     */
    private static final LoadingCache<KeyValue<String, String>, DictDataRespDTO> GET_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<KeyValue<String, String>, DictDataRespDTO>() {

                @Override
                public DictDataRespDTO load(KeyValue<String, String> key) {
                    return ObjectUtil.defaultIfNull(dictDataApi.getDictData(key.getKey(), key.getValue()), DICT_DATA_NULL);
                }

            });

    /**
     * 针对 {@link #getDictDataLabelList(String)} 的缓存
     */
    private static final LoadingCache<String, List<String>> GET_DICT_DATA_LIST_CACHE = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<String, List<String>>() {

                @Override
                public List<String> load(String dictType) {
                    return dictDataApi.getDictDataLabelList(dictType);
                }

            });

    /**
     * 针对 {@link #parseDictDataValue(String, String)} 的缓存
     */
    private static final LoadingCache<KeyValue<String, String>, DictDataRespDTO> PARSE_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<KeyValue<String, String>, DictDataRespDTO>() {

                @Override
                public DictDataRespDTO load(KeyValue<String, String> key) {
                    return ObjectUtil.defaultIfNull(dictDataApi.parseDictData(key.getKey(), key.getValue()), DICT_DATA_NULL);
                }

            });

    public static void init(DictDataApi dictDataApi) {
        DictFrameworkUtils.dictDataApi = dictDataApi;
        log.info("[init][初始化 DictFrameworkUtils 成功]");
    }


    @SneakyThrows
    public static String getDictDataLabel(String dictType, String value) {
        return GET_DICT_DATA_CACHE.get(new KeyValue<>(dictType, value)).getLabel();
    }

    @SneakyThrows
    public static List<String> getDictDataLabelList(String dictType) {
        return GET_DICT_DATA_LIST_CACHE.get(dictType);
    }

    @SneakyThrows
    public static String parseDictDataValue(String dictType, String label) {
        return PARSE_DICT_DATA_CACHE.get(new KeyValue<>(dictType, label)).getValue();
    }

}

2. 核心工具类封装(CacheUtils)

public class CacheUtils {
    // 异步刷新缓存(适合全局数据)
    public static <K, V> LoadingCache<K, V> buildAsyncReloadingCache(Duration duration, CacheLoader<K, V> loader) {
        return CacheBuilder.newBuilder()
                .refreshAfterWrite(duration) // 写后刷新时间
                .build(CacheLoader.asyncReloading(loader, Executors.newCachedThreadPool()));
    }
}

关键配置说明:

  • refreshAfterWrite:写入后指定时间触发异步刷新
  • asyncReloading:使用独立线程池执行刷新任务
  • Executors.newCachedThreadPool:弹性线程池应对突发流量

3. 字典缓存实现(DictFrameworkUtils)

3.1 缓存初始化

   private static DictDataApi dictDataApi;

    private static final DictDataRespDTO DICT_DATA_NULL = new DictDataRespDTO();

  	// 使用复合 Key 缓存字典项(类型+值 → 标签)
    private static final LoadingCache<KeyValue<String, String>, DictDataRespDTO> GET_DICT_DATA_CACHE = CacheUtils.buildAsyncReloadingCache(
            Duration.ofMinutes(1L), // 过期时间 1 分钟
            new CacheLoader<KeyValue<String, String>, DictDataRespDTO>() {

                @Override
                public DictDataRespDTO load(KeyValue<String, String> key) {
                    return ObjectUtil.defaultIfNull(dictDataApi.getDictData(key.getKey(), key.getValue()), DICT_DATA_NULL);
                }

            });

3.2 缓存使用示例

@SneakyThrows // 通过 Lombok 隐藏异常
public static String getDictDataLabel(String dictType, String value) {
    return GET_DICT_DATA_CACHE.get(new KeyValue<>(dictType, value)).getLabel();
}

四、最佳实践总结

1. 缓存策略设计

策略说明本案例实现
缓存穿透非法 Key 导致频繁回源返回 DICT_DATA_NULL 空对象
缓存雪崩大量缓存同时失效随机过期时间(可扩展)
缓存击穿热点 Key 失效导致并发回源使用 LoadingCache 原子加载

2. 性能优化点

  • 异步刷新:通过 asyncReloading 实现后台线程刷新,避免阻塞请求线程
  • 分层缓存:同时缓存 字典项 → 标签 和 标签 → 字典项 两种关系
  • 弹性线程池:使用 CachedThreadPool 应对突发刷新请求

相关文章:

  • 文生视频提示词编写指南(进阶篇)
  • Python数据爬取
  • VMware Workstation安装C9800CL
  • UML对象图
  • 云存储服务器的作用都有哪些?
  • win10安装wget工具
  • Day1一切从安装有利工具开始
  • 反射率均值与RCS均值的计算方法差异
  • dockerTeskTop安装dify及使用deepseek
  • Git版本管理系列:(一)使用Git管理单分支
  • 【Shell】模拟爬虫下载天龙八部小说
  • 关于Spring MVC处理JSON数据集的详细说明,涵盖如何接收和发送JSON数据,包含代码示例和总结表格
  • Java中介者模式详解
  • introduceHLSL
  • 2.7/Q2,Charls最新文章解读
  • 数智读书笔记系列027:《医疗健康大数据治理》构建智慧医疗的核心基石
  • JS Bom对象
  • AI小白:AI算法中常用的数学函数
  • 【正点原子】STM32MP135去除SD卡引脚复用,出现 /dev/mmcblk1p5 not found!
  • (done) 并行计算 CS149 Lecture4 (并行编程基础)
  • 深圳企业网站建设专业/万网官网域名注册
  • 品牌网站怎么建设/宁波seo推广
  • 一个域名一个ip做多个网站/百度sem推广具体做什么
  • wordpress改变上传目录权限/保定seo网站推广
  • 网站优化的策略/网络营销有哪些方式
  • javaweb做商业网站/有趣的软文