com.litongjava.tio.utils.cache.caffeine.CaffeineCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tio-utils Show documentation
Show all versions of tio-utils Show documentation
t-io is a aio framework for java
package com.litongjava.tio.utils.cache.caffeine;
import java.io.Serializable;
import java.util.Collection;
import java.util.concurrent.ConcurrentMap;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.litongjava.tio.utils.cache.AbsCache;
import com.litongjava.tio.utils.hutool.StrUtil;
/**
* @author tanyaowu
*
*/
public class CaffeineCache extends AbsCache {
private LoadingCache loadingCache = null;
private LoadingCache temporaryLoadingCache = null;
public CaffeineCache(String cacheName, LoadingCache loadingCache,
LoadingCache temporaryLoadingCache) {
super(cacheName);
this.loadingCache = loadingCache;
this.temporaryLoadingCache = temporaryLoadingCache;
}
@Override
public void clear() {
loadingCache.invalidateAll();
temporaryLoadingCache.invalidateAll();
}
@Override
public Serializable _get(String key) {
if (StrUtil.isBlank(key)) {
return null;
}
Serializable ret = loadingCache.getIfPresent(key);
if (ret == null) {
ret = temporaryLoadingCache.getIfPresent(key);
}
return ret;
}
@Override
public Collection keys() {
ConcurrentMap map = loadingCache.asMap();
return map.keySet();
}
@Override
public Collection keysCollection() {
return loadingCache.asMap().keySet();
}
@Override
public void put(String key, Serializable value) {
if (StrUtil.isBlank(key)) {
return;
}
loadingCache.put(key, value);
}
@Override
public void putTemporary(String key, Serializable value) {
if (StrUtil.isBlank(key)) {
return;
}
temporaryLoadingCache.put(key, value);
}
@Override
public void remove(String key) {
if (StrUtil.isBlank(key)) {
return;
}
loadingCache.invalidate(key);
temporaryLoadingCache.invalidate(key);
}
/**
*
* @return
* @author: tanyaowu
*/
public ConcurrentMap asMap() {
return loadingCache.asMap();
}
/**
*
* @return
* @author: tanyaowu
*/
public long size() {
return loadingCache.estimatedSize();// .size();
}
@Override
public long ttl(String key) {
throw new RuntimeException("不支持ttl");
}
}