PRADO Component Framework for PHP 5
  • Home
  • About
  • Testimonials
  • Demos
  • Download
  • Documentation
  • Forum
  • Development
  • Tutorials
  • Class Docs
  • API Manual
  • Wiki

Packages

  • None
  • System
    • Caching
    • Collections
    • Data
      • ActiveRecord
        • Relations
        • Scaffold
          • InputBuilder
      • Commom
        • Sqlite
      • Common
        • Mssql
        • Mysql
        • Oracle
        • Pgsql
        • Sqlite
      • DataGateway
      • SqlMap
        • Configuration
        • Statements
    • Exceptions
    • I18N
    • IO
    • Security
    • Util
    • Web
      • Javascripts
      • Services
      • UI
        • ActiveControls
        • WebControls
    • Xml
  • Wsat
    • pages
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo

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 cache
  • TCache::set() : store the value with a key into cache
  • TCache::add() : store the value only if cache does not have this key
  • TCache::delete() : delete the value with the specified key from cache
  • TDbCache::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
Extended by TApplicationComponent
Extended by TModule implements IModule
Extended by TCache implements ICache, ArrayAccess
Extended by TDbCache
Package: System\Caching
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
Methods summary
public
# __destruct( )

Destructor. Disconnect the db connection.

Destructor. Disconnect the db connection.

Overrides

TComponent::__destruct()
public
# init( TXmlElement $config )

Initializes this module. This method is required by the IModule interface. attach TDbCache::doInitializeCache() to TApplication.OnLoadStateComplete event attach TDbCache::doFlushCacheExpired() to TApplication.OnSaveState event

Initializes this module. This method is required by the IModule interface. attach TDbCache::doInitializeCache() to TApplication.OnLoadStateComplete event attach TDbCache::doFlushCacheExpired() to TApplication.OnSaveState event

Parameters

$config
TXmlElement
configuration for this module, can be null

Overrides

TCache::init()
public
# doFlushCacheExpired( )

Event listener for TApplication.OnSaveState

Event listener for TApplication.OnSaveState

Since

3.1.5

See

TDbCache::flushCacheExpired()
public
# doInitializeCache( )

Event listener for TApplication.OnLoadStateComplete

Event listener for TApplication.OnLoadStateComplete

Since

3.1.5

See

TDbCache::initializeCache()
protected
# initializeCache( boolean $force = false )

Initialize TDbCache

Initialize TDbCache

If setAutoCreateCacheTable AutoCreateCacheTableName is 'true' check existence of cache table and create table if does not exist.

Parameters

$force
boolean
Force override global state check

Throws

TConfigurationException
if any error happens during creating database or cache table.

Since

3.1.5
public
# flushCacheExpired( boolean $force = false )

Flush expired values from cache depending on setFlushInterval FlushInterval

Flush expired values from cache depending on setFlushInterval FlushInterval

Parameters

$force
boolean
override setFlushInterval FlushInterval and force deletion of expired items

Since

3.1.5
public integer
# getFlushInterval( )

Returns

integer
Interval in sec expired items will be removed from cache. Default to 60

Since

3.1.5
public
# setFlushInterval( integer $value )

Sets interval expired items will be removed from cache

Sets interval expired items will be removed from cache

To disable automatic deletion of expired items, e.g. for external flushing via cron you can set value to '0'

Parameters

$value
integer
Interval in sec

Since

3.1.5
protected TDbConnection
# createDbConnection( )

Creates the DB connection.

Creates the DB connection.

Returns

TDbConnection
the created DB connection

Throws

TConfigurationException
if module ID is invalid or empty
public TDbConnection
# getDbConnection( )

Returns

TDbConnection
the DB connection instance
public string
# getConnectionID( )

Returns

string
the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set.

Since

3.1.1
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.

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.

Parameters

$value
string
ID of the TDataSourceConfig module

Since

3.1.1
public string
# getConnectionString( )

Returns

string
The Data Source Name, or DSN, contains the information required to connect to the database.
public
# setConnectionString( string $value )

Parameters

$value
string
The Data Source Name, or DSN, contains the information required to connect to the database.

See

http://www.php.net/manual/en/function.pdo-construct.php
public string
# getUsername( )

Returns

string
the username for establishing DB connection. Defaults to empty string.
public
# setUsername( string $value )

Parameters

$value
string
the username for establishing DB connection
public string
# getPassword( )

Returns

string
the password for establishing DB connection. Defaults to empty string.
public
# setPassword( string $value )

Parameters

$value
string
the password for establishing DB connection
public string
# getCacheTableName( )

Returns

string
the name of the DB table to store cache content. Defaults to 'pradocache'.

See

TDbCache::setAutoCreateCacheTable()
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)

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)

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!

Parameters

$value
string
the name of the DB table to store cache content

See

TDbCache::setAutoCreateCacheTable()
public boolean
# getAutoCreateCacheTable( )

Returns

boolean
whether the cache DB table should be automatically created if not exists. Defaults to true.

See

TDbCache::setAutoCreateCacheTable()
public
# setAutoCreateCacheTable( boolean $value )

Parameters

$value
boolean
whether the cache DB table should be automatically created if not exists.

See

TDbCache::setCacheTableName()
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.

Retrieves a value from cache with a specified key. This is the implementation of the method declared in the parent class.

Parameters

$key
string
a unique key identifying the cached value

Returns

string
the value stored in cache, false if the value is not in the cache or expired.
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.

Stores a value identified by a key in cache. This is the implementation of the method declared in the parent class.

Parameters

$key
string
the key identifying the value to be cached
$value
string
the value to be cached
$expire
integer
the number of seconds in which the cached value will expire. 0 means never expire.

Returns

boolean
true if the value is successfully stored into cache, false otherwise
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.

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.

Parameters

$key
string
the key identifying the value to be cached
$value
string
the value to be cached
$expire
integer
the number of seconds in which the cached value will expire. 0 means never expire.

Returns

boolean
true if the value is successfully stored into cache, false otherwise
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.

Deletes a value with the specified key from cache This is the implementation of the method declared in the parent class.

Parameters

$key
string
the key of the value to be deleted

Returns

boolean
if no error happens during deletion
public
# flush( )

Deletes all values from cache. Be careful of performing this operation if the cache is shared by multiple applications.

Deletes all values from cache. Be careful of performing this operation if the cache is shared by multiple applications.

Throws

TNotSupportedException
if this method is not overridden by child classes

Overrides

TCache::flush()
Methods inherited from TCache
add(), delete(), generateUniqueKey(), get(), getKeyPrefix(), getPrimaryCache(), offsetExists(), offsetGet(), offsetSet(), offsetUnset(), set(), setKeyPrefix(), setPrimaryCache()
Methods inherited from TModule
getID(), setID()
Methods inherited from TApplicationComponent
getApplication(), getRequest(), getResponse(), getService(), getSession(), getUser(), publishAsset(), publishFilePath()
Methods inherited from TComponent
__call(), __construct(), __get(), __isset(), __set(), __sleep(), __unset(), __wakeup(), addParsedObject(), asa(), attachBehavior(), attachBehaviors(), attachClassBehavior(), attachEventHandler(), canGetProperty(), canSetProperty(), clearBehaviors(), createdOnTemplate(), detachBehavior(), detachBehaviors(), detachClassBehavior(), detachEventHandler(), disableBehavior(), disableBehaviors(), enableBehavior(), enableBehaviors(), evaluateExpression(), evaluateStatements(), fxAttachClassBehavior(), fxDetachClassBehavior(), getAutoGlobalListen(), getBehaviorsEnabled(), getClassHierarchy(), getEventHandlers(), getListeningToGlobalEvents(), getSubProperty(), hasEvent(), hasEventHandler(), hasProperty(), isa(), listen(), raiseEvent(), setSubProperty(), unlisten()
Constants inherited from TComponent
GLOBAL_RAISE_EVENT_LISTENER
Terms of Service | Contact Us
PRADO v3.2.4 API Manual API documentation generated by ApiGen 2.8.0
Copyright © 2006-2014 by the PRADO Group.
Powered by PRADO