|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.tentackle.appworx.AppDbObjectCache<T>
T - the AppDbObject classpublic class AppDbObjectCache<T extends AppDbObject>
Cache for AppDbObjects.
The cache works both client- and server side.
There can be one cache per class.
The cache may hold any number of unique indexes (see AppDbObjectCacheIndex)
that can be added or removed at runtime. At ID-index is mandatory
and automatically added. Objects may be preloaded on first access (all
objects in the current context) or loaded on demand. The cache is also
aware of object-lists. For example, a selectAllCached will return a
cached list of all objects. And many more features...
| Field Summary | |
|---|---|
static List<AppDbObjectCache<? extends AppDbObject>> |
cacheList
list of all caches |
static int |
FORGET
forget all entries if maxSize reached (default) |
static int |
LFU
least frequently used |
static int |
LRU
least recently used |
| Constructor Summary | |
|---|---|
AppDbObjectCache(Class<T> objectClazz)
Creates a cache without preloading. |
|
AppDbObjectCache(Class<T> objectClazz,
boolean preload)
Creates an instance of an AppDbObjectCache. |
|
| Method Summary | ||
|---|---|---|
boolean |
add(T obj)
add an object to the cache. |
|
void |
addIndex(AppDbObjectCacheIndex<T,?> index)
Add an index to cache. |
|
void |
addIndexIfNotAssigned(AppDbObjectCacheIndex<T,?> index)
Adds an index if not already added. |
|
static
|
createCache(Class<T> clazz,
boolean preload)
Creates an instance of an AppDbObjectCache and registers in a global cache-list (see removeObjectsForDbInAllCaches(org.tentackle.db.Db)). |
|
void |
expire(Db db,
long maxSerial)
Expire dirty objects in cache. |
|
void |
expire(long maxSerial)
Same as expire but delayed until the next select. |
|
void |
expireByExpirationInfo(long[] expireSet,
long curSerial)
Expires object in cache with an ID of a given set. |
|
int |
getKeepQuota()
Gets the current keep quota. |
|
int |
getMaxSize()
Get the maximum cache size. |
|
List<T> |
getObjects()
Gets all objects in cache. Because the objects may live in different db-contexts (and different db-connections!) this method should be used with great care in apps as it returns the objects "as is", i.e. |
|
int |
getSize()
Get the number of objects stored in cache. |
|
int |
getStrategy()
|
|
long |
getTableSerial()
|
|
void |
invalidate()
force the cache to be cleared next access |
|
static boolean |
isAllEnabled()
Check whether all caches are enabled at all. |
|
boolean |
isEnabled()
Check whether cache is enabled at all. |
|
boolean |
isPreloading()
|
|
boolean |
remove(T obj)
remove an object from cache. |
|
void |
removeIndex(AppDbObjectCacheIndex<T,?> index)
removes an index. |
|
int |
removeNonCacheables()
Removes all objects with isObjectCacheable() == false. |
|
void |
removeObjectsForDb(Db db)
removes all objects and lists for a given Db. |
|
static void |
removeObjectsForDbInAllCaches(Db db)
Removes all objects in ALL caches that refer to a given db. |
|
|
select(AppDbObjectCacheIndex<T,C> index,
ContextDb db,
C key)
Retrieve object via cache. |
|
|
select(AppDbObjectCacheIndex<T,C> index,
ContextDb db,
C key,
boolean loadIfMissing)
Retrieves an object via cache. |
|
|
select(AppDbObjectCacheIndex<T,C> index,
ContextDb db,
C fromKey,
C toKey)
Retrieve a range of objects from cache. |
|
T |
select(ContextDb db,
long id)
common select by ID. |
|
T |
select(ContextDb db,
long id,
boolean loadIfMissing)
common select by ID. |
|
List<T> |
selectAllInContext(ContextDb db)
Retrieve all objects for a context via cache. |
|
static void |
setAllEnabled(boolean enabled)
Set all caches enabled or disabled. |
|
void |
setEnabled(boolean enabled)
Set cache enabled or disabled. |
|
void |
setKeepQuota(int keepQuota)
Set the keep quota in percent for the caching strategy. |
|
void |
setMaxSize(int maxSize)
Set the maximum cache size. |
|
void |
setStrategy(int strategy)
Set the caching strategy. |
|
void |
shrinkCache()
apply caching strategy |
|
String |
toString()
Returns a string representation of the object. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
public static final int FORGET
public static final int LRU
public static final int LFU
public static List<AppDbObjectCache<? extends AppDbObject>> cacheList
| Constructor Detail |
|---|
public AppDbObjectCache(Class<T> objectClazz,
boolean preload)
objectClazz - is the AppDbObject-class managed by the cache.preload - is true if preload all objects in contextDb of cache.public AppDbObjectCache(Class<T> objectClazz)
objectClazz - is the AppDbObject-class managed by the cache.| Method Detail |
|---|
public static <T extends AppDbObject> AppDbObjectCache<T> createCache(Class<T> clazz,
boolean preload)
removeObjectsForDbInAllCaches(org.tentackle.db.Db)).
This is the preferred method to create a cache!
T - the data object classclazz - is the AppDbObject-class managed by the cache.preload - is true if preload all objects in contextDb of cache.
public static void removeObjectsForDbInAllCaches(Db db)
db - is the db-connection (probably closed)public String toString()
java.lang.ObjecttoString method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())
toString in class Objectpublic void addIndex(AppDbObjectCacheIndex<T,?> index)
index - is the index to addpublic void addIndexIfNotAssigned(AppDbObjectCacheIndex<T,?> index)
index - the index to addpublic void removeIndex(AppDbObjectCacheIndex<T,?> index)
index - to removepublic void invalidate()
public void expireByExpirationInfo(long[] expireSet,
long curSerial)
expireSet - is an array of long-pairs (id/tableserial), sorted by tableSerial, representing the the objects to set expiredcurSerial - is the current table serial (from Modification-table), 0 = don't check
public void expire(Db db,
long maxSerial)
db - is the db to use, null = delay expiration check until next selectmaxSerial - is the max. tableSerial to scan for updates, 0 = clazz provides no tableSerialpublic void expire(long maxSerial)
maxSerial - is the max. tableSerial to scan for updates, 0 = clazz provides no tableSerialpublic long getTableSerial()
public void shrinkCache()
public int getSize()
public List<T> getObjects()
public int removeNonCacheables()
public <C extends Comparable<? super C>> T select(AppDbObjectCacheIndex<T,C> index,
ContextDb db,
C key,
boolean loadIfMissing)
C - the Comparable classindex - the cache index to usedb - the contextDbkey - is the Comparable used as a keyloadIfMissing - is true if the object should be loaded from storage if not in cache
public <C extends Comparable<C>> T select(AppDbObjectCacheIndex<T,C> index,
ContextDb db,
C key)
C - the Comparable classindex - the cache index to usedb - the contextDbkey - is the Comparable used as a key
public T select(ContextDb db,
long id,
boolean loadIfMissing)
db - the contextDbid - is the object-IDloadIfMissing - is true if the object should be loaded from storage if not in cache
public T select(ContextDb db,
long id)
db - the contextDbid - is the object-ID
public boolean add(T obj)
obj - is the AppDbObject to add
public boolean remove(T obj)
obj - is the AppDbObject to remove
public void removeObjectsForDb(Db db)
db - is the db-connection (probably closed)
public <C extends Comparable<? super C>> List<T> select(AppDbObjectCacheIndex<T,C> index,
ContextDb db,
C fromKey,
C toKey)
C - the Comparable classindex - the cache index to usedb - the contextDbfromKey - starting key, inclusivetoKey - ending key, exclusive
public List<T> selectAllInContext(ContextDb db)
db - the database context
public static boolean isAllEnabled()
public static void setAllEnabled(boolean enabled)
enabled - New value of property enabled.public boolean isEnabled()
public void setEnabled(boolean enabled)
enabled - New value of property enabled.public boolean isPreloading()
public int getMaxSize()
public void setMaxSize(int maxSize)
maxSize - New value of property maxSize.public void setStrategy(int strategy)
strategy - is one of FORGET, LRU or LFUpublic int getStrategy()
public void setKeepQuota(int keepQuota)
keepQuota - in percentpublic int getKeepQuota()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||