org.apache.jcs.auxiliary.disk.jdbc
Class JDBCDiskCache<K extends Serializable,V extends Serializable>

java.lang.Object
  extended by org.apache.jcs.auxiliary.AbstractAuxiliaryCache<K,V>
      extended by org.apache.jcs.auxiliary.AbstractAuxiliaryCacheEventLogging<K,V>
          extended by org.apache.jcs.auxiliary.disk.AbstractDiskCache<K,V>
              extended by org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCache<K,V>
All Implemented Interfaces:
AuxiliaryCache<K,V>, ICache<K,V>, ICacheType
Direct Known Subclasses:
MySQLDiskCache

public class JDBCDiskCache<K extends Serializable,V extends Serializable>
extends AbstractDiskCache<K,V>

This is the jdbc disk cache plugin.

It expects a table created by the following script. The table name is configurable.

                       drop TABLE JCS_STORE;
                       CREATE TABLE JCS_STORE
                       (
                       CACHE_KEY                  VARCHAR(250)          NOT NULL,
                       REGION                     VARCHAR(250)          NOT NULL,
                       ELEMENT                    BLOB,
                       CREATE_TIME                TIMESTAMP,
                       UPDATE_TIME_SECONDS        BIGINT,
                       MAX_LIFE_SECONDS           BIGINT,
                       SYSTEM_EXPIRE_TIME_SECONDS BIGINT,
                       IS_ETERNAL                 CHAR(1),
                       PRIMARY KEY (CACHE_KEY, REGION)
                       );
 

The cleanup thread will delete non eternal items where (now - create time) > max life seconds * 1000

To speed up the deletion the SYSTEM_EXPIRE_TIME_SECONDS is used instead. It is recommended that an index be created on this column is you will have over a million records.

Author:
Aaron Smuts

Nested Class Summary
 
Nested classes/interfaces inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
AbstractDiskCache.MyCacheListener
 
Nested classes/interfaces inherited from interface org.apache.jcs.engine.behavior.ICacheType
ICacheType.CacheType
 
Field Summary
 
Fields inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
alive, cacheEventQueue, cacheName, purgatory, purgHits, removeAllLock
 
Fields inherited from class org.apache.jcs.auxiliary.AbstractAuxiliaryCache
cacheEventLogger, keyMatcher
 
Constructor Summary
JDBCDiskCache(JDBCDiskCacheAttributes cattr, TableState tableState, ICompositeCacheManager compositeCacheManager)
          Constructs a JDBC Disk Cache for the provided cache attributes.
 
Method Summary
 String constructLikeParameterFromPattern(String pattern)
           
protected  int deleteExpired()
          Removed the expired.
protected  boolean doesElementExist(ICacheElement<K,V> ce)
          Does an element exist for this key?
 AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
          This returns the generic attributes for an auxiliary cache.
protected  String getDiskLocation()
          This is used by the event logging.
 IElementSerializer getElementSerializer()
          Allows it to be injected.
 Set<K> getGroupKeys(String groupName)
          The keys in a group.
 Set<String> getGroupNames()
          The group names in the cache.
protected  JDBCDiskCacheAttributes getJdbcDiskCacheAttributes()
           
 JDBCDiskCachePoolAccess getPoolAccess()
          Public so managers can access it.
 int getSize()
          Returns the current cache size.
 IStats getStatistics()
          Extends the parent stats.
protected  String getTableName()
          Returns the name of the table.
 TableState getTableState()
           
protected  JDBCDiskCachePoolAccess initializePoolAccess(JDBCDiskCacheAttributes cattr, ICompositeCacheManager compositeCacheManager)
          Registers the driver and creates a poolAccess class.
 void processDispose()
          Shuts down the pool
protected  ICacheElement<K,V> processGet(K key)
          Queries the database for the value.
protected  Map<K,ICacheElement<K,V>> processGetMatching(String pattern)
          This will run a like query.
protected  boolean processRemove(K key)
          Returns true if the removal was successful; or false if there is nothing to remove.
protected  void processRemoveAll()
          This should remove all elements.
protected  void processUpdate(ICacheElement<K,V> ce)
          Inserts or updates.
 void reset()
          Typically this is used to handle errors by last resort, force content update, or removeall
 void setElementSerializer(IElementSerializer elementSerializer)
          Allows you to inject a custom serializer.
protected  void setJdbcDiskCacheAttributes(JDBCDiskCacheAttributes jdbcDiskCacheAttributes)
           
protected  void setPoolAccess(JDBCDiskCachePoolAccess poolAccess)
           
 void setTableState(TableState tableState)
           
 String toString()
          For debugging.
 
Methods inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
dispose, doDispose, doGet, doGetMatching, doRemove, doRemoveAll, doUpdate, get, getCacheName, getCacheType, getEventLoggingExtraInfo, getMatching, getStats, getStatus, processGetMultiple, remove, removeAll, update
 
Methods inherited from class org.apache.jcs.auxiliary.AbstractAuxiliaryCacheEventLogging
disposeWithEventLogging, getMatchingWithEventLogging, getMultiple, getMultipleWithEventLogging, getWithEventLogging, removeAllWithEventLogging, removeWithEventLogging, updateWithEventLogging
 
Methods inherited from class org.apache.jcs.auxiliary.AbstractAuxiliaryCache
createICacheEvent, createICacheEvent, getCacheEventLogger, getKeyMatcher, logApplicationEvent, logError, logICacheEvent, setCacheEventLogger, setKeyMatcher
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.apache.jcs.auxiliary.AuxiliaryCache
setCacheEventLogger
 
Methods inherited from interface org.apache.jcs.engine.behavior.ICache
getMultiple, setKeyMatcher
 

Constructor Detail

JDBCDiskCache

public JDBCDiskCache(JDBCDiskCacheAttributes cattr,
                     TableState tableState,
                     ICompositeCacheManager compositeCacheManager)
Constructs a JDBC Disk Cache for the provided cache attributes. The table state object is used to mark deletions.

Parameters:
cattr -
tableState -
compositeCacheManager -
Method Detail

initializePoolAccess

protected JDBCDiskCachePoolAccess initializePoolAccess(JDBCDiskCacheAttributes cattr,
                                                       ICompositeCacheManager compositeCacheManager)
Registers the driver and creates a poolAccess class.

Parameters:
cattr -
compositeCacheManager -
Returns:
JDBCDiskCachePoolAccess for testing

processUpdate

protected void processUpdate(ICacheElement<K,V> ce)
Inserts or updates. By default it will try to insert. If the item exists we will get an error. It will then update. This behavior is configurable. The cache can be configured to check before inserting.

Specified by:
processUpdate in class AbstractAuxiliaryCacheEventLogging<K extends Serializable,V extends Serializable>
Parameters:
ce -

doesElementExist

protected boolean doesElementExist(ICacheElement<K,V> ce)
Does an element exist for this key?

Parameters:
ce -
Returns:
boolean

processGet

protected ICacheElement<K,V> processGet(K key)
Queries the database for the value. If it gets a result, the value is deserialized.

Specified by:
processGet in class AbstractAuxiliaryCacheEventLogging<K extends Serializable,V extends Serializable>
Parameters:
key -
Returns:
ICacheElement
See Also:
AbstractDiskCache.doGet(java.io.Serializable)

processGetMatching

protected Map<K,ICacheElement<K,V>> processGetMatching(String pattern)
This will run a like query. It will try to construct a usable query but different implementations will be needed to adjust the syntax.

Specified by:
processGetMatching in class AbstractAuxiliaryCacheEventLogging<K extends Serializable,V extends Serializable>
Parameters:
pattern -
Returns:
key,value map

constructLikeParameterFromPattern

public String constructLikeParameterFromPattern(String pattern)
Parameters:
pattern -
Returns:
String to use in the like query.

processRemove

protected boolean processRemove(K key)
Returns true if the removal was successful; or false if there is nothing to remove. Current implementation always results in a disk orphan.

Specified by:
processRemove in class AbstractAuxiliaryCacheEventLogging<K extends Serializable,V extends Serializable>
Parameters:
key -
Returns:
boolean

processRemoveAll

protected void processRemoveAll()
This should remove all elements. The auxiliary can be configured to forbid this behavior. If remove all is not allowed, the method balks.

Specified by:
processRemoveAll in class AbstractAuxiliaryCacheEventLogging<K extends Serializable,V extends Serializable>

deleteExpired

protected int deleteExpired()
Removed the expired. (now - create time) > max life seconds * 1000

Returns:
the number deleted

reset

public void reset()
Typically this is used to handle errors by last resort, force content update, or removeall


processDispose

public void processDispose()
Shuts down the pool

Specified by:
processDispose in class AbstractAuxiliaryCacheEventLogging<K extends Serializable,V extends Serializable>

getSize

public int getSize()
Returns the current cache size. Just does a count(*) for the region.

Specified by:
getSize in interface ICache<K extends Serializable,V extends Serializable>
Specified by:
getSize in class AbstractDiskCache<K extends Serializable,V extends Serializable>
Returns:
The size value
See Also:
ICache.getSize()

getGroupKeys

public Set<K> getGroupKeys(String groupName)
Description copied from class: AbstractDiskCache
The keys in a group.

Specified by:
getGroupKeys in interface AuxiliaryCache<K extends Serializable,V extends Serializable>
Specified by:
getGroupKeys in class AbstractDiskCache<K extends Serializable,V extends Serializable>
Parameters:
groupName -
Returns:
Set
See Also:
AuxiliaryCache.getGroupKeys(java.lang.String)

getGroupNames

public Set<String> getGroupNames()
Description copied from class: AbstractDiskCache
The group names in the cache.

Specified by:
getGroupNames in interface AuxiliaryCache<K extends Serializable,V extends Serializable>
Specified by:
getGroupNames in class AbstractDiskCache<K extends Serializable,V extends Serializable>
Returns:
Set
See Also:
AuxiliaryCache.getGroupNames()

setElementSerializer

public void setElementSerializer(IElementSerializer elementSerializer)
Description copied from class: AbstractAuxiliaryCache
Allows you to inject a custom serializer. A good example would be a compressing standard serializer.

Does not allow you to set it to null.

Specified by:
setElementSerializer in interface AuxiliaryCache<K extends Serializable,V extends Serializable>
Overrides:
setElementSerializer in class AbstractAuxiliaryCache<K extends Serializable,V extends Serializable>
Parameters:
elementSerializer - The elementSerializer to set.

getElementSerializer

public IElementSerializer getElementSerializer()
Description copied from class: AbstractAuxiliaryCache
Allows it to be injected.

Overrides:
getElementSerializer in class AbstractAuxiliaryCache<K extends Serializable,V extends Serializable>
Returns:
Returns the elementSerializer.

setJdbcDiskCacheAttributes

protected void setJdbcDiskCacheAttributes(JDBCDiskCacheAttributes jdbcDiskCacheAttributes)
Parameters:
jdbcDiskCacheAttributes - The jdbcDiskCacheAttributes to set.

getJdbcDiskCacheAttributes

protected JDBCDiskCacheAttributes getJdbcDiskCacheAttributes()
Returns:
Returns the jdbcDiskCacheAttributes.

getAuxiliaryCacheAttributes

public AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
Description copied from interface: AuxiliaryCache
This returns the generic attributes for an auxiliary cache. Most implementations will cast this to a more specific type.

Returns:
Returns the AuxiliaryCacheAttributes.

getStatistics

public IStats getStatistics()
Extends the parent stats.

Specified by:
getStatistics in interface AuxiliaryCache<K extends Serializable,V extends Serializable>
Overrides:
getStatistics in class AbstractDiskCache<K extends Serializable,V extends Serializable>
Returns:
IStats
See Also:
AuxiliaryCache.getStatistics()

getTableName

protected String getTableName()
Returns the name of the table.

Returns:
the table name or UNDEFINED

setTableState

public void setTableState(TableState tableState)
Parameters:
tableState - The tableState to set.

getTableState

public TableState getTableState()
Returns:
Returns the tableState.

getDiskLocation

protected String getDiskLocation()
This is used by the event logging.

Specified by:
getDiskLocation in class AbstractDiskCache<K extends Serializable,V extends Serializable>
Returns:
the location of the disk, either path or ip.

setPoolAccess

protected void setPoolAccess(JDBCDiskCachePoolAccess poolAccess)
Parameters:
poolAccess - the poolAccess to set

getPoolAccess

public JDBCDiskCachePoolAccess getPoolAccess()
Public so managers can access it.

Returns:
the poolAccess

toString

public String toString()
For debugging.

Overrides:
toString in class Object
Returns:
this.getStats();


Copyright © 2002-2013 The Apache Software Foundation. All Rights Reserved.