• 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);

  • 相关阅读:
    RationalDMIS2022校验测头
    操作系统——经典同步问题
    设计模式学习笔记 - 规范与重构 - 7.实践:通过一段ID生成器代码,学习如何发现代码质量问题
    Chrome 侧边栏开发示例
    CenterNet算法代码剖析
    【一起读技术书】为什么开始,怎么开始,我们一起开始
    3. 栈基本概念、【顺序栈、共享栈 和 链栈】代码实现
    阿里云OSS文件上传
    智能运维|AIRIOT智慧光伏管理解决方案
    拓扑排序的扩展
  • 原文地址:https://blog.csdn.net/weixin_42714661/article/details/132826163