public class SimpleCache<K,V> extends Object implements Cache<K,V>, Serializable
setCacheFullRemoveType(CacheFullRemoveType) set remove type when cache is fullsetValidTime(long) set valid time of elements in cache, in millsput(Object, CacheObject) or put(Object, Object) put elementget(Object) get elementloadCache(String) restore cache from filesaveCache(String, SimpleCache) save cache to fileSimpleCache()SimpleCache(int)loadCache(String) restore cache from filegetHitRate() get hit rate of the cachegetHitCount() get hit count of the cachegetMissCount() get miss count of the cachegetMaxSize() get the maximum capacity of the cachegetSize() get the number of elements in the cache validMap
| 限定符和类型 | 字段和说明 |
|---|---|
protected Map<K,CacheObject<V>> |
cache
map to storage element
|
static int |
DEFAULT_MAX_SIZE
default maximum capacity of the cache
|
protected AtomicLong |
hitCount
hit count of cache
|
protected AtomicLong |
missCount
miss count of cache
|
| 构造器和说明 |
|---|
SimpleCache()
Maximum size of the cache is
DEFAULT_MAX_SIZE
Elements of the cache will not invalid, can set by setValidTime(long)
Remove type is RemoveTypeEnterTimeFirst when cache is full
|
SimpleCache(int maxSize)
Elements of the cache will not invalid, can set by
setValidTime(long)
Remove type is RemoveTypeEnterTimeFirst when cache is full
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
clear()
Removes all elements from this Map, leaving it empty.
|
boolean |
containsKey(K key)
whether this cache contains the specified key.
|
Set<Map.Entry<K,CacheObject<V>>> |
entrySet()
key value set
|
protected CacheObject<V> |
fullRemoveOne()
remove a element when cache is full. according to
getCacheFullRemoveType()
if getCacheFullRemoveType() is instance of RemoveTypeNotRemove return null, else
remove a element according to getCacheFullRemoveType()
|
CacheObject<V> |
get(K key)
get element
|
CacheFullRemoveType<V> |
getCacheFullRemoveType()
get remove type when cache is full
|
long |
getHitCount()
get hit count
|
double |
getHitRate()
get hit rate
|
int |
getMaxSize()
get the maximum capacity of the cache
|
long |
getMissCount()
get miss count
|
int |
getSize()
get the number of elements in the cache valid
|
long |
getValidTime()
get valid time of elements in cache, in mills.
|
protected boolean |
isExpired(CacheObject<V> obj)
returns whether the element of the specified key has invalided
if
getValidTime() less than 0, return false, else
if element is null, return true, else
if CacheObject.isExpired() is true and CacheObject.isForever() is false, return true, else
if CacheObject.getEnterTime() add getValidTime() less than current time, return true
return false
|
protected boolean |
isExpired(K key)
whether the element of the specified key has invalided
|
Set<K> |
keySet()
key set
|
static <K,V> SimpleCache<K,V> |
loadCache(String filePath)
restore cache from file
|
CacheObject<V> |
put(K key,
CacheObject<V> value)
put element, key and value both not allowed to be null
|
CacheObject<V> |
put(K key,
V value)
put element, key not allowed to be null
|
void |
putAll(Cache<K,V> cache2)
pull all elements of cache2 to this
|
CacheObject<V> |
remove(K key)
remove the specified key from cache, key not allowed to be null
|
protected int |
removeExpired()
remove invalid elements
|
static <K,V> void |
saveCache(String filePath,
SimpleCache<K,V> cache)
save cache to file, the data of
CacheObject should can be serializabled |
void |
setCacheFullRemoveType(CacheFullRemoveType<V> cacheFullRemoveType)
set remove type when cache is full
|
protected void |
setUsedInfo(CacheObject<V> obj)
set used info
|
void |
setValidTime(long validTime)
set valid time of elements in cache, in mills
|
Collection<CacheObject<V>> |
values()
value set
|
public static final int DEFAULT_MAX_SIZE
protected Map<K,CacheObject<V>> cache
protected AtomicLong hitCount
protected AtomicLong missCount
public SimpleCache()
DEFAULT_MAX_SIZEsetValidTime(long)RemoveTypeEnterTimeFirst when cache is fullpublic SimpleCache(int maxSize)
setValidTime(long)RemoveTypeEnterTimeFirst when cache is fullmaxSize - maximum size of the cachepublic int getMaxSize()
public long getValidTime()
public void setValidTime(long validTime)
validTime - valid time of elements in cache, in mills. If less than 0, it will be set to -1 and means not
invalid. Rule of invalid see isExpired(CacheObject)public CacheFullRemoveType<V> getCacheFullRemoveType()
public void setCacheFullRemoveType(CacheFullRemoveType<V> cacheFullRemoveType)
cacheFullRemoveType - the cacheFullRemoveType to setpublic int getSize()
public CacheObject<V> get(K key)
protected void setUsedInfo(CacheObject<V> obj)
obj - public CacheObject<V> put(K key, V value)
put 在接口中 Cache<K,V>key - keyvalue - data of CacheObjectput(Object, CacheObject)public CacheObject<V> put(K key, CacheObject<V> value)
public boolean containsKey(K key)
containsKey 在接口中 Cache<K,V>key - protected boolean isExpired(K key)
key - isExpired(CacheObject)public CacheObject<V> remove(K key)
protected CacheObject<V> fullRemoveOne()
getCacheFullRemoveType()
getCacheFullRemoveType() is instance of RemoveTypeNotRemove return null, elsegetCacheFullRemoveType()key - protected int removeExpired()
public void clear()
clear 在接口中 Cache<K,V>Map.clear()protected boolean isExpired(CacheObject<V> obj)
getValidTime() less than 0, return false, elseCacheObject.isExpired() is true and CacheObject.isForever() is false, return true, elseCacheObject.getEnterTime() add getValidTime() less than current time, return trueobj - public long getHitCount()
public long getMissCount()
public double getHitRate()
getHitRate 在接口中 Cache<K,V>public Set<K> keySet()
CachekeySet 在接口中 Cache<K,V>Map.keySet()public Set<Map.Entry<K,CacheObject<V>>> entrySet()
CacheentrySet 在接口中 Cache<K,V>Map.entrySet()public Collection<CacheObject<V>> values()
Cachevalues 在接口中 Cache<K,V>Map.values()public static <K,V> SimpleCache<K,V> loadCache(String filePath)
filePath - public static <K,V> void saveCache(String filePath, SimpleCache<K,V> cache)
CacheObject should can be serializabledK - V - filePath - cache - Copyright © 2012-2014. All Rights Reserved.