cache本地缓存

来源:互联网 发布:矿用防爆网络交换机 编辑:程序博客网 时间:2024/06/18 16:23

1.google的guava的cache

线程安全的,易用的本地内存缓存。

1.1 依赖

<!-- https://mvnrepository.com/artifact/com.google.guava/guava --><dependency>    <groupId>com.google.guava</groupId>    <artifactId>guava</artifactId>    <version>20.0-hal</version></dependency>

1.2 类与方法

com.google.common.cache.Cache<K, V>

接口声明。
void com.google.common.cache.Cache.put(K key, V value)
放入元素,若key存在,旧值被覆盖。
V com.google.common.cache.Cache.getIfPresent(Object key)

若存在返回V,不存在返回null。

构造cache的代码见下:

private Cache<Long,Byte> failureCache=CacheBuilder.newBuilder()            .maximumSize(10*10000)            .expireAfterWrite(3, TimeUnit.MINUTES)            .build();


0 0