android基于LruCache做一个带过期时间的缓存
由于LruCache已经挺完善的了,所以要写的代码也不多,就是简单判断下item有没有过期就可以了,给LruCache包装了一层。
全部代码已经打成一个库,放在github上了,有兴趣的可以看看。
Code
主要代码就2个:
public class CacheItem<K, V> {
private K key;
private V value;
private long createTime;
private long updateTime;
private long deleteTime;
// 方法略
}
public class MemCache<K, V> {
private long defaultDuring = DateUtil.TIME_UNIT_SECOND * 10;
private final LruCache<K, CacheItem<K, V>> lruCache;
/**
* construct.
*
* @param maxSize maxSize
*/
public MemCache(int maxSize) {
lruCache = new LruCache<>(maxSize);
}
/**
* construct
*
* @param maxSize maxSize
* @param defaultDuring defaultDuring(milliseconds)
*/
public MemCache(int maxSize, long defaultDuring) {
this(maxSize);
this.defaultDuring = defaultDuring;
}
/**
* get value by key
*
* @param key key
* @return value, return null when not found or value expired
*/
public V get(@NonNull K key) {
CacheItem<K, V> cacheItem = lruCache.get(key);
if (cacheItem == null) {
return null;
}
if (DateUtil.isCacheItemAlive(cacheItem)) {
return cacheItem.getValue();
} else {
lruCache.remove(key);
return null;
}
}
/**
* put a value by key
*
* @param key key
* @param value value
* @param during during(milliseconds)
* @return previous value, return null if not found
*/
public V put(@NonNull K key, @NonNull V value, long during) {
if (during < 0) {
throw new IllegalArgumentException("during should >= 0");
}
Date date = new Date();
long time = date.getTime();
CacheItem<K, V> cacheItem = new CacheItem<>(key, value, time, time, time + during);
CacheItem<K, V> previous = lruCache.put(key, cacheItem);
if (previous != null) {
cacheItem.setCreateTime(previous.getCreateTime());
return cacheItem.getValue();
} else {
return null;
}
}
/**
* put a value by key(during = defaultDuring)
*
* @param key key
* @param value value
* @return previous value, return null if not found
*/
public V put(@NonNull K key, @NonNull V value) {
return put(key, value, defaultDuring);
}
/**
* remove value by key
* @param key key
* @return removed value, return null if not found
*/
public V remove(@NonNull K key) {
CacheItem<K, V> remove = lruCache.remove(key);
if (remove == null) {
return null;
}
return remove.getValue();
}
}
使用方法
很简单,就是get,put,remove
MemCache<String, String> cache = new MemCache<>(20);
cache.get(key);
cache.put(key, value);
cache.remove(key);