Class JDBCDiskCache<K,V>
java.lang.Object
org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache<K,V>
org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheEventLogging<K,V>
org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache<K,V>
org.apache.commons.jcs3.auxiliary.disk.jdbc.JDBCDiskCache<K,V>
- All Implemented Interfaces:
AuxiliaryCache<K,
,V> ICache<K,
,V> ICacheType
- Direct Known Subclasses:
MySQLDiskCache
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.
-
Nested Class Summary
Nested classes/interfaces inherited from class org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache
AbstractDiskCache.MyCacheListener
Nested classes/interfaces inherited from interface org.apache.commons.jcs3.engine.behavior.ICacheType
ICacheType.CacheType
-
Field Summary
Fields inherited from interface org.apache.commons.jcs3.engine.behavior.ICache
NAME_COMPONENT_DELIMITER
-
Constructor Summary
ConstructorsConstructorDescriptionJDBCDiskCache
(JDBCDiskCacheAttributes cattr, DataSourceFactory dsFactory, TableState tableState) Constructs a JDBC Disk Cache for the provided cache attributes. -
Method Summary
Modifier and TypeMethodDescriptionconstructLikeParameterFromPattern
(String pattern) protected int
Removed the expired.protected boolean
doesElementExist
(ICacheElement<K, V> ce, Connection con) Does an element exist for this key?This returns the generic attributes for an auxiliary cache.Public so managers can access it.protected String
This is used by the event logging.protected JDBCDiskCacheAttributes
Return the keys in this cache.int
getSize()
Returns the current cache size.Extends the parent stats.protected String
Returns the name of the table.void
Shuts down the poolprotected 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
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 removeallprotected void
setJdbcDiskCacheAttributes
(JDBCDiskCacheAttributes jdbcDiskCacheAttributes) void
setTableState
(TableState tableState) toString()
For debugging.Methods inherited from class org.apache.commons.jcs3.auxiliary.disk.AbstractDiskCache
dispose, doDispose, doGet, doGetMatching, doRemove, doRemoveAll, doUpdate, get, getCacheName, getCacheType, getEventLoggingExtraInfo, getMatching, getStats, getStatus, isAlive, remove, removeAll, setAlive, update
Methods inherited from class org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCacheEventLogging
disposeWithEventLogging, getMatchingWithEventLogging, getMultiple, getMultipleWithEventLogging, getWithEventLogging, removeAllWithEventLogging, removeWithEventLogging, updateWithEventLogging
Methods inherited from class org.apache.commons.jcs3.auxiliary.AbstractAuxiliaryCache
createICacheEvent, createICacheEvent, getCacheEventLogger, getElementSerializer, getKeyMatcher, logApplicationEvent, logError, logICacheEvent, processGetMultiple, setCacheEventLogger, setElementSerializer, setKeyMatcher
-
Constructor Details
-
JDBCDiskCache
public JDBCDiskCache(JDBCDiskCacheAttributes cattr, DataSourceFactory dsFactory, TableState tableState) Constructs a JDBC Disk Cache for the provided cache attributes. The table state object is used to mark deletions.- Parameters:
cattr
- the configuration object for this cachedsFactory
- the DataSourceFactory for this cachetableState
- an object to track table operations
-
-
Method Details
-
processUpdate
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 classAbstractAuxiliaryCacheEventLogging<K,
V> - Parameters:
ce
-
-
doesElementExist
Does an element exist for this key?- Parameters:
ce
- the cache elementcon
- a database connection- Returns:
- boolean
-
processGet
Queries the database for the value. If it gets a result, the value is deserialized.- Specified by:
processGet
in classAbstractAuxiliaryCacheEventLogging<K,
V> - Parameters:
key
-- Returns:
- ICacheElement
- See Also:
-
processGetMatching
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 classAbstractAuxiliaryCacheEventLogging<K,
V> - Parameters:
pattern
-- Returns:
- key,value map
-
constructLikeParameterFromPattern
- Parameters:
pattern
-- Returns:
- String to use in the like query.
-
processRemove
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 classAbstractAuxiliaryCacheEventLogging<K,
V> - Parameters:
key
-- Returns:
- boolean
-
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 classAbstractAuxiliaryCacheEventLogging<K,
V>
-
deleteExpired
Removed the expired. (now - create time) > max life seconds * 1000- Returns:
- the number deleted
-
reset
Typically this is used to handle errors by last resort, force content update, or removeall -
processDispose
Shuts down the pool- Specified by:
processDispose
in classAbstractAuxiliaryCacheEventLogging<K,
V>
-
getSize
Returns the current cache size. Just does a count(*) for the region. -
getKeySet
Return the keys in this cache.- Specified by:
getKeySet
in interfaceAuxiliaryCache<K,
V> - Specified by:
getKeySet
in classAbstractDiskCache<K,
V> - Returns:
- a set of the key type TODO This should probably be done in chunks with a range passed in. This will be a problem if someone puts a 1,000,000 or so items in a region.
- Throws:
IOException
- if access to the auxiliary cache fails- See Also:
-
setJdbcDiskCacheAttributes
- Parameters:
jdbcDiskCacheAttributes
- The jdbcDiskCacheAttributes to set.
-
getJdbcDiskCacheAttributes
- Returns:
- Returns the jdbcDiskCacheAttributes.
-
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
Extends the parent stats.- Specified by:
getStatistics
in interfaceAuxiliaryCache<K,
V> - Overrides:
getStatistics
in classAbstractDiskCache<K,
V> - Returns:
- IStats
- See Also:
-
getTableName
Returns the name of the table.- Returns:
- the table name or UNDEFINED
-
setTableState
- Parameters:
tableState
- The tableState to set.
-
getTableState
- Returns:
- Returns the tableState.
-
getDiskLocation
This is used by the event logging.- Specified by:
getDiskLocation
in classAbstractDiskCache<K,
V> - Returns:
- the location of the disk, either path or ip.
-
getDataSource
Public so managers can access it.- Returns:
- the dsFactory
- Throws:
SQLException
- if getting a data source fails
-
toString
For debugging.
-