Class TDbCache
TDbCache class
TDbCache implements a cache application module by storing cached data in a database.
TDbCache relies on PDO to retrieve data from databases. In order to use TDbCache, you need to enable the PDO extension as well as the corresponding PDO DB driver. For example, to use SQLite database to store cached data, you need both php_pdo and php_pdo_sqlite extensions.
By default, TDbCache creates and uses an SQLite database under the application runtime directory. You may change this default setting by specifying the following properties:
- setConnectionID ConnectionID or
- setConnectionString ConnectionString, setUsername Username and setPassword Pasword.
The cached data is stored in a table in the specified database. By default, the name of the table is called 'pradocache'. If the table does not exist in the database, it will be automatically created with the following structure:
CREATE TABLE pradocache (itemkey CHAR(128), value BLOB, expire INT)
CREATE INDEX IX_itemkey ON pradocache (itemkey)
CREATE INDEX IX_expire ON pradocache (expire)
Note, some DBMS might not support BLOB type. In this case, replace 'BLOB' with a suitable binary data type (e.g. LONGBLOB in MySQL, BYTEA in PostgreSQL.)
Important: Make sure that the indices are non-unique!
If you want to change the cache table name, or if you want to create the table by yourself, you may set setCacheTableName CacheTableName and setAutoCreateCacheTable AutoCreateCacheTableName properties.
setFlushInterval FlushInterval control how often expired items will be removed from cache. If you prefer to remove expired items manualy e.g. via cronjob you can disable automatic deletion by setting FlushInterval to '0'.
The following basic cache operations are implemented:
TCache::get()
: retrieve the value with a key (if any) from cacheTCache::set()
: store the value with a key into cacheTCache::add()
: store the value only if cache does not have this keyTCache::delete()
: delete the value with the specified key from cacheTDbCache::flush()
: delete all values from cache
Each value is associated with an expiration time. The TCache::get()
operation
ensures that any expired value will not be returned. The expiration time by the
number of seconds. A expiration time 0 represents never expire.
By definition, cache does not ensure the existence of a value even if it never expires. Cache is not meant to be an persistent storage.
Do not use the same database file for multiple applications using TDbCache. Also note, cache is shared by all user sessions of an application.
Some usage examples of TDbCache are as follows,
$cache=new TDbCache; // TDbCache may also be loaded as a Prado application module $cache->init(null); $cache->add('object',$object); $object2=$cache->get('object');
If loaded, TDbCache will register itself with TApplication
as the
cache module. It can be accessed via TApplication::getCache()
.
TDbCache may be configured in application configuration file as follows
<module id="cache" class="System.Caching.TDbCache" />
- TComponent
-
TApplicationComponent
-
TModule implements IModule
-
TCache implements ICache, ArrayAccess
-
TDbCache
Copyright: Copyright © 2005-2014 PradoSoft
License: http://www.pradosoft.com/license/
Author: Qiang Xue <qiang.xue@gmail.com>
Since: 3.1.0
Located at Caching/TDbCache.php
public
|
|
public
|
#
init(
Initializes this module. This method is required by the IModule interface.
attach |
public
|
|
public
|
|
protected
|
|
public
|
#
flushCacheExpired( boolean $force = false )
Flush expired values from cache depending on setFlushInterval FlushInterval |
public
integer
|
|
public
|
|
protected
|
|
public
|
|
public
string
|
|
public
|
#
setConnectionID( string $value )
Sets the ID of a TDataSourceConfig module. The datasource module will be used to establish the DB connection for this cache module. The database connection can also be specified via setConnectionString ConnectionString. When both ConnectionID and ConnectionString are specified, the former takes precedence. |
public
string
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
|
|
public
string
|
|
public
|
#
setCacheTableName( string $value )
Sets the name of the DB table to store cache content. Note, if setAutoCreateCacheTable AutoCreateCacheTable is false and you want to create the DB table manually by yourself, you need to make sure the DB table is of the following structure:
CREATE TABLE pradocache (itemkey CHAR(128), value BLOB, expire INT)
CREATE INDEX IX_itemkey ON pradocache (itemkey)
CREATE INDEX IX_expire ON pradocache (expire)
|
public
boolean
|
|
public
|
|
protected
string
|
#
getValue( string $key )
Retrieves a value from cache with a specified key. This is the implementation of the method declared in the parent class. |
protected
boolean
|
#
setValue( string $key, string $value, integer $expire )
Stores a value identified by a key in cache. This is the implementation of the method declared in the parent class. |
protected
boolean
|
#
addValue( string $key, string $value, integer $expire )
Stores a value identified by a key into cache if the cache does not contain this key. This is the implementation of the method declared in the parent class. |
protected
boolean
|
#
deleteValue( string $key )
Deletes a value with the specified key from cache This is the implementation of the method declared in the parent class. |
public
|
#
flush( )
Deletes all values from cache. Be careful of performing this operation if the cache is shared by multiple applications. |
add(),
delete(),
generateUniqueKey(),
get(),
getKeyPrefix(),
getPrimaryCache(),
offsetExists(),
offsetGet(),
offsetSet(),
offsetUnset(),
set(),
setKeyPrefix(),
setPrimaryCache()
|
getID(),
setID()
|
getApplication(),
getRequest(),
getResponse(),
getService(),
getSession(),
getUser(),
publishAsset(),
publishFilePath()
|
GLOBAL_RAISE_EVENT_LISTENER
|