com.neomades.content.cache.Cache |
![]() |
An interface for data that must be saved in cache. The cache can be seen as a
map where each entry is a CacheEntry
associated with a key which is a
String
. A CacheEntry
is only a byte array. Its data can
be loaded from a database, a file or the phone memory relying on that you
want to to.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
abstract void |
clear()
Clears the cache.
| ||||||||||
abstract CacheEntry |
get(String key)
Retrieves a
CacheEntry from the cache associated with the given key. | ||||||||||
abstract void |
initialize()
Performs any potentially long-running actions needed to initialize the cache.
| ||||||||||
abstract void |
invalidate(String key, boolean fullExpire)
Invalidates an entry in the cache.
| ||||||||||
abstract void |
put(String key, CacheEntry entry)
Adds or replaces an
CacheEntry in the cache. | ||||||||||
abstract void |
remove(String key)
Removes an entry from the cache.
|
Clears the cache.
Retrieves a CacheEntry
from the cache associated with the given key.
Data must be returned as a CacheEntry
object. If data are loaded from
a file or a database, a CacheEntry
object must be created here with
the data. To do that, you can use or
CacheEntry#setDeserializedData(Object)
.
The way the cache data are loaded or not when a request is performed depends
of the CacheEntry
type you use. The mechanism relies on the methods
isExpired()
and refreshNeeded()
. When
performing a request, if isExpired()
is true
,
the cache data will be ignored and the data will be downloaded again. If
refreshNeeded()
is true
, the current cache
data will be sent via an intermediate response and new data will be
downloaded. isExpired()
is evaluated before
refreshNeeded()
. The basic behavior of these methods is
explained in CacheEntry
javadoc. The API proposes a simplest
alternative implementation with the DefaultCacheEntry
class. If none
of the behaviors responds to your need, you can implement your own
CacheEntry
in the get(String)
method.
key | the cache key |
---|
CacheEntry
or null
if the CacheEntry
can not be found.
Performs any potentially long-running actions needed to initialize the cache. Will be called from a worker thread. It's here that database or files can be created for example.
Invalidates an entry in the cache.
Here is the default implementation that should be used:
CacheEntry entry = get(key); if (entry != null) { entry.setSoftTtl(0); if (fullExpire) { entry.setTtl(0); } put(key, entry); }
This will force the cache content to be downloaded again the next time the
request to download it will be run. Note that if fullExpire
is
false
the old data in the cache will be sent in an intermediate
response in order to have something to present to the user while the new data
are downloaded.
key | the cache key |
---|---|
fullExpire | true to ignore the current cache data
|
Adds or replaces an CacheEntry
in the cache. It's here that data in
the CacheEntry
must be saved in the persistent system (database,
file, phone memory...).
key | the cache key |
---|---|
entry | Data to store in the cache |
Removes an entry from the cache. Do not forget to delete the
CacheEntry
data from the persistent associated system (database,
file...)
key | the cache key |
---|