|
Class TCache
TCache class
TCache is the base class for cache classes with different cache storage
implementation.
TCache implements the interface ICache with the following
methods,
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.
Child classes must implement the following methods:
and optionally TCache::flush()
Since version 3.1.2, TCache implements the ArrayAccess interface such that
the cache acts as an array.
-
TComponent
-
TApplicationComponent
-
TModule
implements
IModule
-
TCache
implements
ICache,
ArrayAccess
Methods summary
public
|
#
init( TXmlElement $config )
Initializes the cache module. This method initializes the cache key prefix
and registers the cache module with the application if the cache is primary.
Initializes the cache module. This method initializes the cache key prefix
and registers the cache module with the application if the cache is primary.
Parameters
Overrides
|
public
boolean
|
#
getPrimaryCache( )
Returns
boolean whether this cache module is used as primary/system cache. A primary cache is
used by PRADO core framework to cache data such as parsed templates, themes,
etc.
|
public
|
#
setPrimaryCache( boolean $value )
Parameters
- $value
boolean whether this cache module is used as primary/system cache. Defaults to false.
See
|
public
string
|
#
getKeyPrefix( )
Returns
string a unique prefix for the keys of cached values. If it is not explicitly set, it
will take the value of TApplication::getUniqueID() .
|
public
|
#
setKeyPrefix( string $value )
Parameters
- $value
string a unique prefix for the keys of cached values
|
protected
sring
|
#
generateUniqueKey( string $key )
Parameters
- $key
string a key identifying a value to be cached
Returns
sring a key generated from the provided key which ensures the uniqueness across
applications
|
public
mixed
|
#
get( string $id )
Retrieves a value from cache with a specified key.
Retrieves a value from cache with a specified key.
Parameters
- $id
string a key identifying the cached value
Returns
mixed the value stored in cache, false if the value is not in the cache or expired.
Implementation of
|
public
boolean
|
#
set( string $id, mixed $value, integer $expire = 0, ICacheDependency $dependency = null )
Stores a value identified by a key into cache. If the cache already contains
such a key, the existing value and expiration time will be replaced with the new
ones. If the value is empty, the cache key will be deleted.
Stores a value identified by a key into cache. If the cache already contains
such a key, the existing value and expiration time will be replaced with the new
ones. If the value is empty, the cache key will be deleted.
Parameters
- $id
string the key identifying the value to be cached
- $value
mixed the value to be cached
- $expire
integer the number of seconds in which the cached value will expire. 0 means never
expire.
- $dependency
ICacheDependency
dependency of the cached item. If the dependency changes, the item is labeled
invalid.
Returns
boolean true if the value is successfully stored into cache, false otherwise
Implementation of
|
public
boolean
|
#
add( string $id, mixed $value, integer $expire = 0, ICacheDependency $dependency = null )
Stores a value identified by a key into cache if the cache does not contain
this key. Nothing will be done if the cache already contains the key or if value
is empty.
Stores a value identified by a key into cache if the cache does not contain
this key. Nothing will be done if the cache already contains the key or if value
is empty.
Parameters
- $id
string the key identifying the value to be cached
- $value
mixed the value to be cached
- $expire
integer the number of seconds in which the cached value will expire. 0 means never
expire.
- $dependency
ICacheDependency
dependency of the cached item. If the dependency changes, the item is labeled
invalid.
Returns
boolean true if the value is successfully stored into cache, false otherwise
Implementation of
|
public
boolean
|
#
delete( string $id )
Deletes a value with the specified key from cache
Deletes a value with the specified key from cache
Parameters
- $id
string the key of the value to be deleted
Returns
boolean if no error happens during deletion
Implementation of
|
public
|
#
flush( )
Deletes all values from cache. Be careful of performing this operation if the
cache is shared by multiple applications. Child classes may implement this
method to realize the flush operation.
Deletes all values from cache. Be careful of performing this operation if the
cache is shared by multiple applications. Child classes may implement this
method to realize the flush operation.
Throws
Implementation of
|
abstract protected
string
|
#
getValue( string $key )
Retrieves a value from cache with a specified key. This method should be
implemented by child classes to store the data in specific cache storage. The
uniqueness and dependency are handled in TCache::get() already. So only the
implementation of data retrieval is needed.
Retrieves a value from cache with a specified key. This method should be
implemented by child classes to store the data in specific cache storage. The
uniqueness and dependency are handled in TCache::get() already. So only the
implementation of data retrieval is needed.
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.
|
abstract protected
boolean
|
#
setValue( string $key, string $value, integer $expire )
Stores a value identified by a key in cache. This method should be
implemented by child classes to store the data in specific cache storage. The
uniqueness and dependency are handled in TCache::set() already. So only the
implementation of data storage is needed.
Stores a value identified by a key in cache. This method should be
implemented by child classes to store the data in specific cache storage. The
uniqueness and dependency are handled in TCache::set() already. So only the
implementation of data storage is needed.
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
|
abstract 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 method should be implemented by child classes to store the data
in specific cache storage. The uniqueness and dependency are handled in TCache::add() already. So only the implementation of data storage is needed.
Stores a value identified by a key into cache if the cache does not contain
this key. This method should be implemented by child classes to store the data
in specific cache storage. The uniqueness and dependency are handled in TCache::add() already. So only the implementation of data storage is needed.
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
|
abstract protected
boolean
|
#
deleteValue( string $key )
Deletes a value with the specified key from cache This method should be
implemented by child classes to delete the data from actual cache storage.
Deletes a value with the specified key from cache This method should be
implemented by child classes to delete the data from actual cache storage.
Parameters
- $key
string the key of the value to be deleted
Returns
boolean if no error happens during deletion
|
public
boolean
|
#
offsetExists( string $id )
Returns whether there is a cache entry with a specified key. This method is
required by the interface ArrayAccess.
Returns whether there is a cache entry with a specified key. This method is
required by the interface ArrayAccess.
Parameters
- $id
string a key identifying the cached value
Returns
boolean
Implementation of
ArrayAccess::offsetExists()
|
public
mixed
|
#
offsetGet( string $id )
Retrieves the value from cache with a specified key. This method is required
by the interface ArrayAccess.
Retrieves the value from cache with a specified key. This method is required
by the interface ArrayAccess.
Parameters
- $id
string a key identifying the cached value
Returns
mixed the value stored in cache, false if the value is not in the cache or expired.
Implementation of
ArrayAccess::offsetGet()
|
public
|
#
offsetSet( string $id, mixed $value )
Stores the value identified by a key into cache. If the cache already
contains such a key, the existing value will be replaced with the new ones. To
add expiration and dependencies, use the set() method. This method is required
by the interface ArrayAccess.
Stores the value identified by a key into cache. If the cache already
contains such a key, the existing value will be replaced with the new ones. To
add expiration and dependencies, use the set() method. This method is required
by the interface ArrayAccess.
Parameters
- $id
string the key identifying the value to be cached
- $value
mixed the value to be cached
Implementation of
ArrayAccess::offsetSet()
|
public
boolean
|
#
offsetUnset( string $id )
Deletes the value with the specified key from cache This method is required
by the interface ArrayAccess.
Deletes the value with the specified key from cache This method is required
by the interface ArrayAccess.
Parameters
- $id
string the key of the value to be deleted
Returns
boolean if no error happens during deletion
Implementation of
ArrayAccess::offsetUnset()
|
Methods inherited from TComponent
__call(),
__construct(),
__destruct(),
__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()
|
|