Package org.greenrobot.essentials
Class ObjectCache<KEY,VALUE>
- java.lang.Object
-
- org.greenrobot.essentials.ObjectCache<KEY,VALUE>
-
public class ObjectCache<KEY,VALUE> extends java.lang.Object
An in-memory object cache supporting soft/weak/strong references, maximum size (clearing the entries that were put first), and time-based expiration.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
ObjectCache.CacheEntry<V>
static class
ObjectCache.ReferenceType
-
Field Summary
Fields Modifier and Type Field Description private int
countEvicted
private int
countExpired
private int
countHit
private int
countMiss
private int
countPut
private int
countPutCountSinceEviction
private int
countRefCleared
private long
expirationMillis
private boolean
isExpiring
private boolean
isStrongReference
private int
maxSize
private long
nextCleanUpTimestamp
private ObjectCache.ReferenceType
referenceType
private java.util.Map<KEY,ObjectCache.CacheEntry<VALUE>>
values
-
Constructor Summary
Constructors Constructor Description ObjectCache(ObjectCache.ReferenceType referenceType, int maxSize, long expirationMillis)
Create a cache according to the given configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) void
checkCleanUpObsoleteEntries()
int
cleanUpObsoleteEntries()
Iterates over all entries to check for obsolete ones (time expired or reference cleared).void
clear()
Clears all cached entries.boolean
containsKey(KEY key)
boolean
containsKeyWithValue(KEY key)
void
evictToTargetSize(int targetSize)
VALUE
get(KEY key)
Get the cached entry or null if no valid cached entry is found.int
getCountEvicted()
int
getCountExpired()
int
getCountHit()
int
getCountMiss()
int
getCountPut()
int
getCountRefCleared()
int
getMaxSize()
java.lang.String
getStatsStringRemoved()
Often used in addition totoString()
to print out states: details why entries were removed.private VALUE
getValue(KEY keyForRemoval, ObjectCache.CacheEntry<VALUE> entry)
private VALUE
getValueForRemoved(ObjectCache.CacheEntry<VALUE> entry)
java.util.Set<KEY>
keySet()
VALUE
put(KEY key, VALUE object)
Stores an new entry in the cache.void
putAll(java.util.Map<KEY,VALUE> mapDataToPut)
Stores all entries contained in the given map in the cache.VALUE
remove(KEY key)
Removes an entry from the cache.int
size()
java.lang.String
toString()
-
-
-
Field Detail
-
values
private final java.util.Map<KEY,ObjectCache.CacheEntry<VALUE>> values
-
referenceType
private final ObjectCache.ReferenceType referenceType
-
isStrongReference
private final boolean isStrongReference
-
maxSize
private final int maxSize
-
expirationMillis
private final long expirationMillis
-
isExpiring
private final boolean isExpiring
-
nextCleanUpTimestamp
private volatile long nextCleanUpTimestamp
-
countPutCountSinceEviction
private volatile int countPutCountSinceEviction
-
countPut
private volatile int countPut
-
countHit
private volatile int countHit
-
countMiss
private volatile int countMiss
-
countExpired
private volatile int countExpired
-
countRefCleared
private volatile int countRefCleared
-
countEvicted
private volatile int countEvicted
-
-
Constructor Detail
-
ObjectCache
public ObjectCache(ObjectCache.ReferenceType referenceType, int maxSize, long expirationMillis)
Create a cache according to the given configuration.- Parameters:
referenceType
- SOFT is usually a good choice allowing the VM to clear caches when running low on memory. STRONG may also be preferred, e.g. when the required space is granted.maxSize
- The maximum number of entries stored by this cacheexpirationMillis
-
-
-
Method Detail
-
getValueForRemoved
private VALUE getValueForRemoved(ObjectCache.CacheEntry<VALUE> entry)
-
getValue
private VALUE getValue(KEY keyForRemoval, ObjectCache.CacheEntry<VALUE> entry)
-
putAll
public void putAll(java.util.Map<KEY,VALUE> mapDataToPut)
Stores all entries contained in the given map in the cache.
-
clear
public void clear()
Clears all cached entries.
-
evictToTargetSize
public void evictToTargetSize(int targetSize)
-
checkCleanUpObsoleteEntries
void checkCleanUpObsoleteEntries()
-
cleanUpObsoleteEntries
public int cleanUpObsoleteEntries()
Iterates over all entries to check for obsolete ones (time expired or reference cleared). Note: Usually you don't need to call this method explicitly, because it is called internally in certain conditions when space has to be reclaimed.
-
containsKey
public boolean containsKey(KEY key)
-
containsKeyWithValue
public boolean containsKeyWithValue(KEY key)
-
keySet
public java.util.Set<KEY> keySet()
-
getMaxSize
public int getMaxSize()
-
size
public int size()
-
getCountPut
public int getCountPut()
-
getCountHit
public int getCountHit()
-
getCountMiss
public int getCountMiss()
-
getCountExpired
public int getCountExpired()
-
getCountRefCleared
public int getCountRefCleared()
-
getCountEvicted
public int getCountEvicted()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
getStatsStringRemoved
public java.lang.String getStatsStringRemoved()
Often used in addition totoString()
to print out states: details why entries were removed.
-
-