定义缓存接口:
public interface Cache<K, V> {
/**
* Cache a pair of key value. If the key value already exists, the value will be overwritten.
* @param key cache key
* @param val cache value
*/
void put(K key, V val);
/**
* Take the corresponding value from the cache according to the cache key.
* @param key cache key
* @return cache value
*/
V get(K key);
/**
* Get the value in the cache according to the primary key, and put it into the cache after processing by the function.
* @param key cache key
* @param call a function, the return value of the function will be updated to the cache
* @return cache value
* @throws Exception callable function interface throw exception
*/
V get(K key, Callable<? extends V> call) throws Exception;
/**
* Take the corresponding value from the cache according to the cache key, and remove this record from the cache.
* @param key cache key
* @return cache value
*/
V remove(K key);
/**
* Clear the entire cache.
*/
void clear();
/**
* Returns the number of key-value pairs in the cache.
* @return number of key-value pairs
*/
int getSize();
}
基于Java-Map的本地缓存实现:
public class SimpleCache<K, V> implements Cache<K, V> {
private Map<K, V> cache;
public SimpleCache(int size) {
cache = new HashMap<>(size);
}
@Override
public void put(K key, V val) {
cache.put(key, val);
}
@Override
public V get(K key) {
return cache.get(key);
}
@Override
public V get(K key, Callable<? extends V> call) throws Exception {
if (cache.containsKey(key)) {
return cache.get(key);
} else {
V v2 = call.call();
cache.put(key, v2);
return v2;
}
}
@Override
public V remove(K key) {
return cache.remove(key);
}
@Override
public void clear() {
cache.clear();
}
@Override
public int getSize() {
return cache.size();
}
}
基于LinkedHashMap的LruCache实现:
public class LruCache<K, V> implements Cache<K, V> {
private final Cache<K, V> delegate;
private Map<K, V> keyMap;
private K eldestKey;
public LruCache(Cache<K, V> delegate, int size) {
this.delegate = delegate;
setSize(size);
}
@Override
public int getSize() {
return delegate.getSize();
}
public void setSize(final int size) {
keyMap = new LinkedHashMap<K, V>(size, .75F, true) {
private static final long serialVersionUID = 4267176411845948333L;
@Override
protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
boolean tooBig = size() > size;
if (tooBig) {
eldestKey = eldest.getKey();
}
return tooBig;
}
};
}
@Override
public void put(K key, V val) {
delegate.put(key, val);
cycleKeyList(key);
}
@Override
public V get(K key) {
keyMap.get(key);
return delegate.get(key);
}
@Override
public V get(K key, Callable<? extends V> call) throws Exception {
return this.delegate.get(key, call);
}
@Override
public V remove(K key) {
return delegate.remove(key);
}
@Override
public void clear() {
delegate.clear();
keyMap.clear();
}
private void cycleKeyList(K key) {
keyMap.put(key, null);
if (eldestKey != null) {
delegate.remove(eldestKey);
eldestKey = null;
}
}
}
自动过期缓存实现:
public class AutoExpireCache<K, V> implements Cache<K, V> {
private long expireNanos;
private Cache<K, V> delegate;
private HashMap<K, CacheItemProperties> keyProp = new HashMap<>();
public AutoExpireCache(Cache<K, V> delegate, long expireNanos) {
this.expireNanos = expireNanos;
this.delegate = delegate;
}
@Override
public void put(K key, V val) {
keyProp.put(key, cacheItemProperties());
this.delegate.put(key, val);
}
@Override
public V get(K key) {
if (keyProp.get(key) != null && isExpire(keyProp.get(key))) {
this.keyProp.remove(key);
this.delegate.remove(key);
return null;
}
return this.delegate.get(key);
}
@Override
public V get(K key, Callable<? extends V> call) throws Exception {
V cachedValue = this.get(key);
if (null == cachedValue) {
V v2 = call.call();
this.put(key, v2);
return v2;
}
return cachedValue;
}
@Override
public V remove(K key) {
keyProp.remove(key);
return this.delegate.remove(key);
}
@Override
public void clear() {
keyProp.clear();
this.delegate.clear();
}
@Override
public int getSize() {
return this.delegate.getSize();
}
private boolean isExpire(CacheItemProperties itemProperties) {
if (itemProperties == null) {
return true;
}
return expireNanos != -1 && (System.nanoTime() - itemProperties.getExpireNanos() > expireNanos);
}
private CacheItemProperties cacheItemProperties() {
CacheItemProperties cacheItemProperties = new CacheItemProperties();
cacheItemProperties.setExpireNanos(System.nanoTime());
return cacheItemProperties;
}
}
版权声明:本文为shadow_zed原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。