org.apache.jcs.auxiliary.disk.jdbc
Class JDBCDiskCache

java.lang.Object
  extended by org.apache.jcs.auxiliary.disk.AbstractDiskCache
      extended by org.apache.jcs.auxiliary.disk.jdbc.JDBCDiskCache
All Implemented Interfaces:
java.io.Serializable, AuxiliaryCache, ICache, ICacheType
Direct Known Subclasses:
MySQLDiskCache

public class JDBCDiskCache
extends AbstractDiskCache

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                DATE,
                       CREATE_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
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
alive, cacheEventQueue, cacheName, purgatory, purgHits
 
Fields inherited from interface org.apache.jcs.engine.behavior.ICacheType
CACHE_HUB, DISK_CACHE, LATERAL_CACHE, REMOTE_CACHE
 
Constructor Summary
JDBCDiskCache(JDBCDiskCacheAttributes cattr, TableState tableState)
          Constructs a JDBC Disk Cache for the provided cache attributes.
 
Method Summary
protected  int deleteExpired()
          Removed the expired.
 void doDispose()
          Shuts down the pool
protected  boolean doesElementExist(ICacheElement ce)
          Does an element exist for this key?
 ICacheElement doGet(java.io.Serializable key)
          Queries the database for the value.
 boolean doRemove(java.io.Serializable key)
          Returns true if the removal was succesful; or false if there is nothing to remove.
 void doRemoveAll()
          This should remove all elements.
 void doUpdate(ICacheElement ce)
          Add a cache element to the persistent store.
 AuxiliaryCacheAttributes getAuxiliaryCacheAttributes()
          This returns the generic attributes for an auxiliary cache.
 IElementSerializer getElementSerializer()
           
 java.util.Set getGroupKeys(java.lang.String groupName)
          The keys in a group.
protected  JDBCDiskCacheAttributes getJdbcDiskCacheAttributes()
           
 int getSize()
          Returns the current cache size.
 IStats getStatistics()
          Extends the parent stats.
protected  java.lang.String getTableName()
          Returns the name of the table.
 TableState getTableState()
           
protected  void initializePoolAccess(JDBCDiskCacheAttributes cattr)
          Registers the driver and creates a poolAccess class.
 void reset()
          Typically this is used to handle errors by last resort, force content update, or removeall
protected  byte[] serialize(java.io.Serializable obj)
          Returns the serialized form of the given object in a byte array.
 void setElementSerializer(IElementSerializer elementSerializer)
           
protected  void setJdbcDiskCacheAttributes(JDBCDiskCacheAttributes jdbcDiskCacheAttributes)
           
 void setTableState(TableState tableState)
           
 java.lang.String toString()
          For debugging.
 
Methods inherited from class org.apache.jcs.auxiliary.disk.AbstractDiskCache
dispose, get, getCacheName, getCacheType, getStats, getStatus, remove, removeAll, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JDBCDiskCache

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

Parameters:
cattr -
tableState -
Method Detail

initializePoolAccess

protected void initializePoolAccess(JDBCDiskCacheAttributes cattr)
Registers the driver and creates a poolAccess class.

Parameters:
cattr -

doUpdate

public void doUpdate(ICacheElement ce)
Description copied from class: AbstractDiskCache
Add a cache element to the persistent store.

Specified by:
doUpdate in class AbstractDiskCache

doesElementExist

protected boolean doesElementExist(ICacheElement ce)
Does an element exist for this key?

Parameters:
ce -
Returns:
boolean

doGet

public ICacheElement doGet(java.io.Serializable key)
Queries the database for the value. If it gets a result, the value is deserialized.

Specified by:
doGet in class AbstractDiskCache
Parameters:
key - Key to locate value for.
Returns:
An object matching key, or null.
See Also:
AbstractDiskCache.doGet(java.io.Serializable)

doRemove

public boolean doRemove(java.io.Serializable key)
Returns true if the removal was succesful; or false if there is nothing to remove. Current implementation always result in a disk orphan.

Specified by:
doRemove in class AbstractDiskCache
Parameters:
key -
Returns:
boolean

doRemoveAll

public void doRemoveAll()
This should remove all elements.

Specified by:
doRemoveAll in class AbstractDiskCache

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


doDispose

public void doDispose()
Shuts down the pool

Specified by:
doDispose in class AbstractDiskCache

getSize

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

Specified by:
getSize in interface AuxiliaryCache
Specified by:
getSize in interface ICache
Specified by:
getSize in class AbstractDiskCache
Returns:
The size value
See Also:
ICache.getSize()

serialize

protected byte[] serialize(java.io.Serializable obj)
                    throws java.io.IOException
Returns the serialized form of the given object in a byte array.

Parameters:
obj -
Returns:
byte[]
Throws:
java.io.IOException

getGroupKeys

public java.util.Set getGroupKeys(java.lang.String groupName)
Description copied from class: AbstractDiskCache
The keys in a group.

(non-Javadoc)

Specified by:
getGroupKeys in interface AuxiliaryCache
Specified by:
getGroupKeys in class AbstractDiskCache
Parameters:
groupName -
Returns:
See Also:
AuxiliaryCache.getGroupKeys(java.lang.String)

setElementSerializer

public void setElementSerializer(IElementSerializer elementSerializer)
Parameters:
elementSerializer - The elementSerializer to set.

getElementSerializer

public IElementSerializer getElementSerializer()
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
Overrides:
getStatistics in class AbstractDiskCache
Returns:
See Also:
AuxiliaryCache.getStatistics()

getTableName

protected java.lang.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.

toString

public java.lang.String toString()
For debugging.

Overrides:
toString in class java.lang.Object


Copyright © 2002-2007 Apache Software Foundation. All Rights Reserved.