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

  • 相关阅读:
    poshmark前景,怎么快速提升销量!
    北漂七年拿过阿里、腾讯、华为offer的资深架构师,分享经验总结
    1103 Integer Factorization
    数据库字段,逗号拼接存储,如何将其拆分查询
    目标检测新SOTA:YOLOv9 问世,新架构让传统卷积重焕生机
    【HTML小游戏】推箱子网页版(附完整源码)
    Netty NIO ByteBuffer 简单实验
    疫情之下,我帮你总结了全网最全的Java面试高频考点
    【JavaScript 逆向】拼多多 anti_content 参数逆向解析
    生成.pkl文件,读取.pkl文件的内容
  • 原文地址:https://blog.csdn.net/weixin_42714661/article/details/132826163