• Caffeine缓存的使用


    1.springboot集成Caffeine

    1. com.github.ben-manes.caffeine
    2. caffeine
    3. 2.9.3

    2.Cache 模式

    1)普通使用

    1. // 普通缓存
    2. cache = Caffeine.newBuilder()
    3. .expireAfterWrite(10, TimeUnit.MINUTES)
    4. .maximumSize(1000) // 设置缓存的最大容量
    5. .build();
    6. // 查找一个缓存元素, 没有查找到的时候返回null
    7. MyObject graph = cache.getIfPresent(key);
    8. // 查找缓存,如果缓存不存在则生成缓存元素, 如果无法生成则返回null
    9. graph = cache.get(key, k -> userService.getUserCache((Integer) key));
    10. // 添加或者更新一个缓存元素
    11. cache.put(key, graph);
    12. // 移除一个缓存元素
    13. cache.invalidate(key);

    2)同步缓存

    1. // 同步自动加载缓存
    2. autoCache = Caffeine.newBuilder()
    3. .expireAfterWrite(10, TimeUnit.MINUTES) // 设置写入后的过期时间
    4. .maximumSize(1000) // 设置缓存的最大容量
    5. .build(key -> userService.getUserCache((Integer) key));
    6. //缓存未命中会自动调用当前方法,将数据库获取到的数据缓存到Caffeine
    7. graph = cache.get(key);
    8. // 添加或者更新一个缓存元素
    9. cache.put(key, graph);
    10. // 移除一个缓存元素
    11. cache.invalidate(key);

    3)异步缓存

    1. // 异步加载缓存
    2. asyncCache = Caffeine.newBuilder()
    3. .expireAfterWrite(10, TimeUnit.MINUTES) // 设置写入后的过期时间
    4. .maximumSize(1000) // 设置缓存的最大容量
    5. .buildAsync().synchronous();
    6. // 查找缓存,如果缓存不存在则生成缓存元素, 如果无法生成则返回null
    7. graph = cache.get(key, k -> userService.getUserCache((Integer) key));
    8. // 添加或者更新一个缓存元素
    9. cache.put(key, graph);
    10. // 移除一个缓存元素
    11. cache.invalidate(key);

    4.异步自动缓存

    1. // 异步自动加载缓存
    2. autoAsyncCache = Caffeine.newBuilder()
    3. .expireAfterWrite(10, TimeUnit.MINUTES) // 设置写入后的过期时间
    4. .maximumSize(1000) // 设置缓存的最大容量
    5. .buildAsync(key ->
    6. // 异步加载缓存逻辑
    7. userService.getUserCache((Integer)key)
    8. );
    9. graph = cache.get(key);
    10. // 添加或者更新一个缓存元素
    11. cache.put(key, graph);
    12. // 移除一个缓存元素
    13. cache.invalidate(key);

    5.自定义每个缓存过期时间

    1. //首先自定义过期时间
    2. expireAfterCache = Caffeine.newBuilder().expireAfter(new Expiry() {
    3. @Override
    4. public long expireAfterCreate(@NonNull Object key, @NonNull Object value, long currentTime) {
    5. return currentTime;
    6. }
    7. @Override
    8. public long expireAfterUpdate(@NonNull Object key, @NonNull Object value, long currentTime, @NonNegative long currentDuration) {
    9. return currentDuration;
    10. }
    11. @Override
    12. public long expireAfterRead(@NonNull Object key, @NonNull Object value, long currentTime, @NonNegative long currentDuration) {
    13. return currentDuration;
    14. }
    15. }).maximumSize(1000).build();
    16. // 添加或者更新一个缓存元素
    17. expireAfterCache.policy().expireVariably().ifPresent(p->{
    18. //为每个key设置不同的过期时间,可自行封装下
    19. p.put("cs","123",5,TimeUnit.SECONDS);
    20. });
    21. // 查找一个缓存元素, 没有查找到的时候返回null
    22. MyObject graph = expireAfterCache.getIfPresent(key);
    23. // 移除一个缓存元素
    24. expireAfterCache.invalidate(key);

  • 相关阅读:
    Kubernetes(k8s)访问控制:身份认证
    Calico 网络故障排查 calico/node is not ready: BIRD is not ready(指定Calico匹配网桥规则)
    【Java集合类面试十四】、HashMap是如何解决哈希冲突的?
    C++对多继承的理解
    698. 划分为k个相等的子集 ●●
    高效的嵌入式系统架构设计
    QT中QJson详细解析+代码演示
    机电设备制造企业,如何借助ERP系统做好客供料管理?
    【Buildroot】工具包使用
    JavaScript混淆工具大比拼:JScrambler和JShaman哪个更胜一筹?
  • 原文地址:https://blog.csdn.net/weixin_42714661/article/details/132826163