|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.github.ignition.support.cache.AbstractCache<KeyT,ValT>
public abstract class AbstractCache<KeyT,ValT>
A simple 2-level cache consisting of a small and fast in-memory cache (1st level cache) and an
(optional) slower but bigger disk cache (2nd level cache). For disk caching, either the
application's cache directory or the SD card can be used. Please note that in the case of the app
cache dir, Android may at any point decide to wipe that entire directory if it runs low on
internal storage. The SD card cache must be managed by the application, e.g. by calling
#wipe
whenever the app quits.
When pulling from the cache, it will first attempt to load the data from memory. If that fails, it will try to load it from disk (assuming disk caching is enabled). If that succeeds, the data will be put in the in-memory cache and returned (read-through). Otherwise it's a cache miss.
Pushes to the cache are always write-through (i.e. the data will be stored both on disk, if disk caching is enabled, and in memory).
Nested Class Summary |
---|
Nested classes/interfaces inherited from interface java.util.Map |
---|
Map.Entry<K,V> |
Field Summary | |
---|---|
static int |
DISK_CACHE_INTERNAL
|
static int |
DISK_CACHE_SDCARD
|
protected String |
diskCacheDirectory
|
Constructor Summary | |
---|---|
AbstractCache(String name,
int initialCapacity,
long expirationInMinutes,
int maxConcurrentThreads)
Creates a new cache instance. |
Method Summary | |
---|---|
void |
clear()
Clears the entire cache (memory and disk). |
void |
clear(boolean removeFromDisk)
Clears the memory cache, as well as the disk cache if it's enabled and removeFromDisk is true . |
boolean |
containsKey(Object key)
Checks if a value is present in the cache. |
boolean |
containsKeyInMemory(Object key)
Checks if a value is present in the in-memory cache. |
boolean |
containsKeyOnDisk(Object key)
Checks if a value is present in the disk cache. |
boolean |
containsValue(Object value)
Checks if the given value is currently held in memory. |
boolean |
enableDiskCache(android.content.Context context,
int storageDevice)
Enable caching to the phone's internal storage or SD card. |
Set<Map.Entry<KeyT,ValT>> |
entrySet()
|
ValT |
get(Object elementKey)
Reads a value from the cache by first probing the in-memory cache. |
List<File> |
getCachedFiles()
Retrieves the list of files that are currently cached to disk. |
String |
getDiskCacheDirectory()
Only meaningful if disk caching is enabled. |
abstract String |
getFileNameForKey(KeyT key)
Only meaningful if disk caching is enabled. |
boolean |
isDiskCacheEnabled()
|
boolean |
isEmpty()
|
Set<KeyT> |
keySet()
|
ValT |
put(KeyT key,
ValT value)
Writes an element to the cache. |
void |
putAll(Map<? extends KeyT,? extends ValT> t)
|
protected abstract ValT |
readValueFromDisk(File file)
Only meaningful if disk caching is enabled. |
ValT |
remove(Object key)
Removes an entry from both memory and disk. |
ValT |
removeKey(Object key)
Removes an entry from memory. |
void |
setDiskCacheEnabled(String rootDir)
|
int |
size()
|
Collection<ValT> |
values()
|
protected abstract void |
writeValueToDisk(File file,
ValT value)
Only meaningful if disk caching is enabled. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.util.Map |
---|
equals, hashCode |
Field Detail |
---|
public static final int DISK_CACHE_INTERNAL
public static final int DISK_CACHE_SDCARD
protected String diskCacheDirectory
Constructor Detail |
---|
public AbstractCache(String name, int initialCapacity, long expirationInMinutes, int maxConcurrentThreads)
name
- a human readable identifier for this cache. Note that this value will be used to
derive a directory name if the disk cache is enabled, so don't get too creative
here (camel case names work great)initialCapacity
- the initial element size of the cacheexpirationInMinutes
- time in minutes after which elements will be purged from the cachemaxConcurrentThreads
- how many threads you think may at once access the cache; this need not be an exact
number, but it helps in fragmenting the cache properlyMethod Detail |
---|
public boolean enableDiskCache(android.content.Context context, int storageDevice)
context
- the current contextstorageDevice
- where to store the cached files, either DISK_CACHE_INTERNAL
or
DISK_CACHE_SDCARD
)
public String getDiskCacheDirectory()
enableDiskCache(android.content.Context, int)
.
public abstract String getFileNameForKey(KeyT key)
enableDiskCache(android.content.Context, int)
. Turns a cache key
into the file name that will be used to persist the value to disk. Subclasses must implement
this.
key
- the cache key
protected abstract ValT readValueFromDisk(File file) throws IOException
enableDiskCache(android.content.Context, int)
. Restores a value
previously persisted to the disk cache.
file
- the file holding the cached value
IOException
protected abstract void writeValueToDisk(File file, ValT value) throws IOException
enableDiskCache(android.content.Context, int)
. Persists a value to
the disk cache.
ostream
- the file output stream (buffered).value
- the cache value to persist
IOException
public ValT get(Object elementKey)
get
in interface Map<KeyT,ValT>
elementKey
- the cache key
public ValT put(KeyT key, ValT value)
put
in interface Map<KeyT,ValT>
public void putAll(Map<? extends KeyT,? extends ValT> t)
putAll
in interface Map<KeyT,ValT>
public boolean containsKey(Object key)
containsKey
in interface Map<KeyT,ValT>
key
- the cache key
public boolean containsKeyInMemory(Object key)
key
- the cache key
public boolean containsKeyOnDisk(Object key)
key
- the cache key
public boolean containsValue(Object value)
containsValue
in interface Map<KeyT,ValT>
public ValT remove(Object key)
remove
in interface Map<KeyT,ValT>
public ValT removeKey(Object key)
key
- the cache key
public Set<KeyT> keySet()
keySet
in interface Map<KeyT,ValT>
public Set<Map.Entry<KeyT,ValT>> entrySet()
entrySet
in interface Map<KeyT,ValT>
public int size()
size
in interface Map<KeyT,ValT>
public boolean isEmpty()
isEmpty
in interface Map<KeyT,ValT>
public boolean isDiskCacheEnabled()
public List<File> getCachedFiles()
public void setDiskCacheEnabled(String rootDir)
rootDir
- a folder name to enable caching or null to disable it.public void clear()
clear
in interface Map<KeyT,ValT>
public void clear(boolean removeFromDisk)
removeFromDisk
is true
.
removeFromDisk
- whether or not to wipe the disk cache, toopublic Collection<ValT> values()
values
in interface Map<KeyT,ValT>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |