com.qingzhuge.framework.cache.caffeine.CaffeineCache Maven / Gradle / Ivy
package com.qingzhuge.framework.cache.caffeine;
import com.qingzhuge.framework.cache.Level1Cache;
import com.github.benmanes.caffeine.cache.Cache;
import java.util.Arrays;
import java.util.Collection;
/**
* @author :zero.xiao
* datetime :2018/1/14 16:01
* 一级缓存实现
**/
public class CaffeineCache implements Level1Cache {
private Cache cache;
private long size ;
private long expire ;
/**开启缓存*/
private boolean open;
CaffeineCache(Cache cache, long size, long expire,boolean open) {
this.cache = cache;
this.size = size;
this.expire = expire;
this.open = open;
}
@Override
public long ttl() {
return expire;
}
@Override
public long size() { return size; }
@Override
public Object get(String key) {
if (!open){
return null;
}
return cache.getIfPresent(key);
}
@Override
public void put(String key, Object value) {
if (open) {
cache.put(key, value);
}
}
@Override
public Collection keys() {
if (!open){
return null;
}
return cache.asMap().keySet();
}
@Override
public void evict(String...keys) {
if (!open){
return;
}
cache.invalidateAll(Arrays.asList(keys));
}
@Override
public void clear() {
cache.invalidateAll();
}
}