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

  • 相关阅读:
    Java——接口
    CSRF +self xss的运用【DVWA测试】
    基于TNEWS‘ 今日头条中文新闻(短文本)分类
    【计算机组成原理】怎样判断补码乘法是否溢出
    大学生数学竞赛题,微积分方面的
    【Rust日报】用Rust从头实现一个C编译器
    主流网络协议
    Java网络编程
    【MySQL】内置函数
    php7以上如何禁用掉eval
  • 原文地址:https://blog.csdn.net/weixin_42714661/article/details/132826163