LoadingCache的使用

背景

LoadingCache是GuavaCache构建缓存实体的方法,是一个支持多线程并发读写、高性能、通用的in-heap(堆)本地缓存。
支持key不存在时按照给定的CacheLoader 的loader方法进行loading。如果有多个线程同时get一个不存在的key,那么会有一个线程负责load,其他线程阻塞wait等待。

CacheBuilder方法参数

  • maximumSize(): 最大缓存上限,快达到上限或达到上限,处理了时间最长没被访问过的对象或者根据配置的被释放的对象
  • expireAfterAccess():设置时间对象没有被读/写访问则对象从内存中删除,回收顺序和基于大小回收一样
  • expireAfterWrite(): 设置时间对象没有被写访问则对象从内存中删除
  • refreshAfterWrite():为缓存增加自动定时刷新功能。缓存项只有在被检索时才会真正刷新,即只有刷新间隔时间到了再去get(key)才会重新去执行Loading,否则就算刷新间隔时间到了也不会执行loading操作。

CacheLoader

实现自动加载缓存。可以在其中自定义load方法和reload方法,根据需求加载缓存和刷新缓存。

Cache常用方法

  • get(key): 有值则返回缓存值,没有则执行load方法加载缓存。
  • put(key, value): 显式地向缓存中插入值,会直接覆盖掉已有键之前映射的值。
  • invalidate(key): 显式地清除个别缓存项。
  • invalidateAll(keys): 批量清除缓存项。
  • invalidateAll(): 清除所有缓存项。
  • refresh(key): (异步)主动刷新对应缓存值 。在刷新操作进行时,缓存仍然可以向其他线程返回旧值。

实例

@Slf4j
@Component
public class EntryCache {

    @Autowired
    EntryMapper entryMapper;

    /**
     * guava cache 缓存实体
     */
    LoadingCache<String, Entry> cache = CacheBuilder.newBuilder()
            // 缓存刷新时间
            .refreshAfterWrite(10, TimeUnit.MINUTES)
            // 设置缓存个数
            .maximumSize(500)
            .build(new CacheLoader<String, Entry>() {
                @Override
                // 当本地缓存命没有中时,调用load方法获取结果并将结果缓存
                public Entry load(String appKey) {
                    return getEntryFromDB(appKey);
                }

                // 数据库进行查询
                private Entry getEntryFromDB(String name) {
                    log.info("load entry info from db!entry:{}", name);
                    return entryMapper.selectByName(name);
                }
            });

    /**
     * 对外暴露的方法
     * 从缓存中取entry,没取到就走数据库
     */
    public Entry getEntry(String name) throws ExecutionException {
        return cache.get(name);
    }

    @PostConstruct
    public void initCache() {
        log.info("init entry cache start!");
        //读取所有记录
        List<Entry> list = entryMapper.selectAll();

        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        for (Entry entry : list) {
            try {
                this.getEntry(entry.getName());
            } catch (Exception e) {
                log.error("init cache error!,e:{}", e.getMessage());
            }
        }
        log.info("init entry cache end!");
    }
}

参考

posted @ 2022-03-11 16:13  zjcfrancis  阅读(3455)  评论(0编辑  收藏  举报