com.gentics.api.lib.datasource
Class AbstractCacheableDatasource

java.lang.Object
  extended by com.gentics.api.lib.datasource.AbstractDatasource
      extended by com.gentics.api.lib.datasource.AbstractCacheableDatasource
All Implemented Interfaces:
Datasource, Cloneable

public abstract class AbstractCacheableDatasource
extends AbstractDatasource

A abstract datasource which should make it easy to implement caching for subclasses.

Implementors can use setCacheEnabled(boolean) to enable/disable the cache (by default it is enabled). Before calling to the underlying storage engine, call getCachedResult(DatasourceResultCacheKeyBase) to retrieve a cached result, and afterwards putCachedResult(DatasourceResultCacheKeyBase, Collection) to put a newly retrieved result into the cache.

A cache key can either be retrieved using #getCacheKey(String, Object[], int, int, Sorting[], Map) or by implementing a custom {@link DatasourceResultCacheKeyBase} or extending {@link DatasourceResultCacheKey}

Author:
herbert

Nested Class Summary
static class AbstractCacheableDatasource.DatasourceCountCacheKey
          Datasource cache key for count statements.
static class AbstractCacheableDatasource.DatasourceResultCacheKey
          A simple cache key used for caching datasource results.
static interface AbstractCacheableDatasource.DatasourceResultCacheKeyBase
          Interface used for uniquely identifying a DatasourceResult.
 
Nested classes/interfaces inherited from interface com.gentics.api.lib.datasource.Datasource
Datasource.Sorting
 
Field Summary
protected  boolean cacheEnabled
          Defines if the cache is enabled for this instance.
protected static String COUNTCACHEREGION
          name of the cacheregion for count results
protected  com.gentics.lib.log.NodeLogger logger
           
protected static String RESULTSCACHEREGION
          Name of the cacheregion for queryresults
 
Fields inherited from interface com.gentics.api.lib.datasource.Datasource
SORTORDER_ASC, SORTORDER_DESC, SORTORDER_NONE
 
Constructor Summary
AbstractCacheableDatasource(String id)
          Default constructor which will by default enable caching.
AbstractCacheableDatasource(String id, boolean cacheEnabled)
          This constructor can be used if cache should not be used (by default the cache is enabled)
 
Method Summary
protected  boolean clearCache()
          Clears the cache for this datasource.
protected  Integer getCachedCount(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey)
          Returns the cached count for the given cache key or null if none was cached.
protected  Collection getCachedResult(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey)
          Returns the cached results of a query.
protected  AbstractCacheableDatasource.DatasourceResultCacheKeyBase getCacheKey(String query, Object[] objects, int start, int count, Datasource.Sorting[] sortedColumns, Map specificParameters)
          Creates and returns the cache key for the given parameters.
protected  AbstractCacheableDatasource.DatasourceResultCacheKeyBase getCacheKeyForCount(String query, Object[] objects, Map specificParameters)
          Creates and returns a new cache key for a count statement.
protected  PortalCache getCountResultsCache()
           
 String getDatasourceIdentifier()
          Returns a unique identifier for this datasource.
protected  PortalCache getQueryResultsCache()
          Returns the Query Results Cache.
protected  String getUniqueDatasourceIdentifier()
          Returns a unique datasource identifier.
 boolean isCacheEnabled()
          Returns if the cache is enabled.
protected  boolean putCachedCount(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey, Integer count)
          Put a result of a getCount() method into the cache
protected  boolean putCachedResult(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey, Collection result)
          Stores the given result collection into the cache for later retrieval.
protected  void setCacheEnabled(boolean cacheEnabled)
          Sets if the cache should be enabled or disabled.
 
Methods inherited from class com.gentics.api.lib.datasource.AbstractDatasource
clone, getCount, getCount, getId, getResult, getResult, getResult, getResult, getResult, hasChanged, isValidAttribute, setId
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.gentics.api.lib.datasource.Datasource
createDatasourceFilter, getCount, getCount2, getHandlePool, getResult, getResult, hasChanged, setAttributeNames, setRuleTree
 

Field Detail

cacheEnabled

protected boolean cacheEnabled
Defines if the cache is enabled for this instance. - Since this is an AbstractCacheableDatasource it is by default enabled.


RESULTSCACHEREGION

protected static final String RESULTSCACHEREGION
Name of the cacheregion for queryresults

See Also:
Constant Field Values

COUNTCACHEREGION

protected static final String COUNTCACHEREGION
name of the cacheregion for count results

See Also:
Constant Field Values

logger

protected com.gentics.lib.log.NodeLogger logger
Constructor Detail

AbstractCacheableDatasource

public AbstractCacheableDatasource(String id)
Default constructor which will by default enable caching.


AbstractCacheableDatasource

public AbstractCacheableDatasource(String id,
                                   boolean cacheEnabled)
This constructor can be used if cache should not be used (by default the cache is enabled)

Parameters:
cacheEnabled - false if caching should be disabled.
Method Detail

setCacheEnabled

protected void setCacheEnabled(boolean cacheEnabled)
Sets if the cache should be enabled or disabled.

Parameters:
cacheEnabled - true if cache should be enabled / false otherwise.

isCacheEnabled

public boolean isCacheEnabled()
Returns if the cache is enabled.

Returns:
true if cache is enabled.

getCacheKey

protected AbstractCacheableDatasource.DatasourceResultCacheKeyBase getCacheKey(String query,
                                                                               Object[] objects,
                                                                               int start,
                                                                               int count,
                                                                               Datasource.Sorting[] sortedColumns,
                                                                               Map specificParameters)
Creates and returns the cache key for the given parameters. Most parameters are equivalent to the parameters of Datasource.getResult(DatasourceFilter, String[], int, int, Sorting[], Map). The only exception beeing query and objects. Except 'query' all parameters are optional and may be null.

Parameters:
query - The Datasource / database specific query (e.g. for a datasource relying on a SQL database this would be the SQL query). Must not be null.
objects - The Datasource / database specific Objects which are referenced from the query (e.g. for a datasource relying on a SQL database this would be the variables binded in the SQL query)
start - The index of the first item which was returned
count - the nubmer of items in this result
sortedColumns - the sort order of the result
specificParameters - additional parameters given to getResult(..) method.
Throws:
NullPointerException - if query is null.

getCacheKeyForCount

protected AbstractCacheableDatasource.DatasourceResultCacheKeyBase getCacheKeyForCount(String query,
                                                                                       Object[] objects,
                                                                                       Map specificParameters)
Creates and returns a new cache key for a count statement.

Parameters:
query - datasource specific query
objects - datasource specific objects which are references from the query (e.g. bind variables)
specificParameters - additional parameters given to getCount(..) method.
Returns:
a new cache key.
Throws:
NullPointerException - if query is null.

getQueryResultsCache

protected PortalCache getQueryResultsCache()
Returns the Query Results Cache.

Returns:
query results cache.

getCountResultsCache

protected PortalCache getCountResultsCache()

getCachedResult

protected Collection getCachedResult(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey)
Returns the cached results of a query.

Parameters:
cacheKey -
Returns:
the cached object, or null if it was not found, or if an error happened.

getCachedCount

protected Integer getCachedCount(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey)
Returns the cached count for the given cache key or null if none was cached.


putCachedResult

protected boolean putCachedResult(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey,
                                  Collection result)
Stores the given result collection into the cache for later retrieval. The given result Collection needs to be Serializable. Each item in it has to be Serializable and must not contain any references to this Datasource.

Parameters:
cacheKey - the cache to to uniquely identify the given result
result - the result which was produced with the cacheKey.
Returns:
true if it was successfully put into the cache, false otherwise (e.g. if cache was disabled, or an error occurred).
Throws:
PortalCacheException - if an error happened during caching the result.

putCachedCount

protected boolean putCachedCount(AbstractCacheableDatasource.DatasourceResultCacheKeyBase cacheKey,
                                 Integer count)
Put a result of a getCount() method into the cache

See Also:
getCachedCount(com.gentics.api.lib.datasource.AbstractCacheableDatasource.DatasourceResultCacheKeyBase)

clearCache

protected boolean clearCache()
Clears the cache for this datasource.

Returns:
true if everything went successful and cache was cleared, false if cache is disabled or an error occurred.

getUniqueDatasourceIdentifier

protected String getUniqueDatasourceIdentifier()
Returns a unique datasource identifier. By default simply prefixes getDatasourceIdentifier() with the class name.

Returns:
globally unique identifier.

getDatasourceIdentifier

public String getDatasourceIdentifier()
Returns a unique identifier for this datasource. Needs to uniquely identify this datasource backend (e.g. database) in the scope of all datasources of the same type. (e.g. the string representation of the HandlePool) - should be a constant value which does not change in the lifetime of the datasource.



Copyright © 2013 Gentics Software GmbH. All Rights Reserved.