Doctrine 1.2.4
Doctrine_Cache_Db Class Reference

Inherits Doctrine_Cache_Driver.

Public Member Functions

 __construct ($options=array())
 
 contains ($id)
 
 createTable ()
 
 delete ($id)
 
 deleteAll ()
 
 deleteByPrefix ($prefix)
 
 deleteByRegex ($regex)
 
 deleteBySuffix ($suffix)
 
 fetch ($id, $testCacheValidity=true)
 
 getConnection ()
 
 getOption ($option)
 
 save ($id, $data, $lifeTime=false)
 
 setOption ($option, $value)
 

Protected Member Functions

 _doContains ($id)
 
 _doDelete ($id)
 
 _doFetch ($id, $testCacheValidity=true)
 
 _doSave ($id, $data, $lifeTime=false, $saveKey=true)
 
 _doSave ($id, $data, $lifeTime=false)
 
 _getCacheKeys ()
 
 _getKey ($id)
 
 _hex2bin ($hex)
 

Detailed Description

Definition at line 34 of file Db.php.

Constructor & Destructor Documentation

Doctrine_Cache_Db::__construct (   $options = array())

Configure Database cache driver. Specify instance of Doctrine_Connection and tableName to store cache in

Parameters
array$_optionsan array of options

Definition at line 42 of file Db.php.

{
if ( ! isset($options['connection']) ||
! ($options['connection'] instanceof Doctrine_Connection)) {
throw new Doctrine_Cache_Exception('Connection option not set.');
}
if ( ! isset($options['tableName']) ||
! is_string($options['tableName'])) {
throw new Doctrine_Cache_Exception('Table name option not set.');
}
$this->_options = $options;
}

Member Function Documentation

Doctrine_Cache_Db::_doContains (   $id)
protected

Test if a cache record exists for the passed id

Parameters
string$idcache id
Returns
mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Definition at line 101 of file Db.php.

{
$sql = 'SELECT id, expire FROM ' . $this->_options['tableName']
. ' WHERE id = ?';
$result = $this->getConnection()->fetchOne($sql, array($id));
if (isset($result[0] )) {
return time();
}
return false;
}
Doctrine_Cache_Db::_doDelete (   $id)
protected

Remove a cache record directly. This method is implemented by the cache drivers and used in Doctrine_Cache_Driver::delete()

Parameters
string$idcache id
Returns
boolean true if no problem

Definition at line 162 of file Db.php.

{
$sql = 'DELETE FROM ' . $this->_options['tableName'] . ' WHERE id = ?';
return $this->getConnection()->exec($sql, array($id));
}
Doctrine_Cache_Db::_doFetch (   $id,
  $testCacheValidity = true 
)
protected

Fetch a cache record from this cache driver instance

Parameters
string$idcache id
boolean$testCacheValidityif set to false, the cache validity won't be tested
Returns
mixed Returns either the cached data or false

Definition at line 77 of file Db.php.

{
$sql = 'SELECT data, expire FROM ' . $this->_options['tableName']
. ' WHERE id = ?';
if ($testCacheValidity) {
$sql .= " AND (expire is null OR expire > '" . date('Y-m-d H:i:s') . "')";
}
$result = $this->getConnection()->execute($sql, array($id))->fetchAll(Doctrine_Core::FETCH_NUM);
if ( ! isset($result[0])) {
return false;
}
return unserialize($this->_hex2bin($result[0][0]));
}
Doctrine_Cache_Db::_doSave (   $id,
  $data,
  $lifeTime = false,
  $saveKey = true 
)
protected

Save a cache record directly. This method is implemented by the cache drivers and used in Doctrine_Cache_Driver::save()

Parameters
string$idcache id
string$datadata to cache
int$lifeTimeif != false, set a specific lifetime for this cache record (null => infinite lifeTime)
Returns
boolean true if no problem

Definition at line 123 of file Db.php.

{
if ($this->contains($id)) {
//record is in database, do update
$sql = 'UPDATE ' . $this->_options['tableName']
. ' SET data = ?, expire=? '
. ' WHERE id = ?';
if ($lifeTime) {
$expire = date('Y-m-d H:i:s', time() + $lifeTime);
} else {
$expire = NULL;
}
$params = array(bin2hex(serialize($data)), $expire, $id);
} else {
//record is not in database, do insert
$sql = 'INSERT INTO ' . $this->_options['tableName']
. ' (id, data, expire) VALUES (?, ?, ?)';
if ($lifeTime) {
$expire = date('Y-m-d H:i:s', time() + $lifeTime);
} else {
$expire = NULL;
}
$params = array($id, bin2hex(serialize($data)), $expire);
}
return $this->getConnection()->exec($sql, $params);
}
Doctrine_Cache_Driver::_doSave (   $id,
  $data,
  $lifeTime = false 
)
abstractprotectedinherited

Save a cache record directly. This method is implemented by the cache drivers and used in Doctrine_Cache_Driver::save()

Parameters
string$idcache id
string$datadata to cache
int$lifeTimeif != false, set a specific lifetime for this cache record (null => infinite lifeTime)
Returns
boolean true if no problem
Doctrine_Cache_Db::_getCacheKeys ( )
protected

Fetch an array of all keys stored in cache

Returns
array Returns the array of cache keys

Definition at line 222 of file Db.php.

{
$sql = 'SELECT id FROM ' . $this->_options['tableName'];
$keys = array();
$results = $this->getConnection()->execute($sql)->fetchAll(Doctrine_Core::FETCH_NUM);
for ($i = 0, $count = count($results); $i < $count; $i++) {
$keys[] = $results[$i][0];
}
return $keys;
}
Doctrine_Cache_Driver::_getKey (   $id)
protectedinherited

Get the hash key passing its suffix

Parameters
string$idThe hash key suffix
Returns
string Hash key to be used by drivers

Definition at line 226 of file Driver.php.

{
$prefix = isset($this->_options['prefix']) ? $this->_options['prefix'] : '';
if ( ! $prefix || strpos($id, $prefix) === 0) {
return $id;
} else {
return $prefix . $id;
}
}
Doctrine_Cache_Db::_hex2bin (   $hex)
protected

Convert hex data to binary data. If passed data is not hex then it is returned as is.

Parameters
string$hex
Returns
string $binary

Definition at line 204 of file Db.php.

{
if ( ! is_string($hex)) {
return null;
}
if ( ! ctype_xdigit($hex)) {
return $hex;
}
return pack("H*", $hex);
}
Doctrine_Cache_Driver::contains (   $id)
inherited

Test if a cache record exists for the passed id

Parameters
string$idcache id
Returns
mixed false (a cache is not available) or "last modified" timestamp (int) of the available cache record

Definition at line 101 of file Driver.php.

{
$key = $this->_getKey($id);
return $this->_doContains($key);
}
Doctrine_Cache_Db::createTable ( )

Create the cache table

Returns
void

Definition at line 173 of file Db.php.

{
$name = $this->_options['tableName'];
$fields = array(
'id' => array(
'type' => 'string',
'length' => 255
),
'data' => array(
'type' => 'blob'
),
'expire' => array(
'type' => 'timestamp'
)
);
$options = array(
'primary' => array('id')
);
$this->getConnection()->export->createTable($name, $fields, $options);
}
Doctrine_Cache_Driver::delete (   $id)
inherited

Remove a cache record

Note: This method accepts wildcards with the * character

Parameters
string$idcache id
Returns
boolean true if no problem

Definition at line 129 of file Driver.php.

{
$key = $this->_getKey($id);
if (strpos($key, '*') !== false) {
return $this->deleteByRegex('/' . str_replace('*', '.*', $key) . '/');
}
return $this->_doDelete($key);
}
Doctrine_Cache_Driver::deleteAll ( )
inherited

Delete all cache entries from the cache driver

Returns
integer $count The number of deleted cache entries

Definition at line 208 of file Driver.php.

{
$count = 0;
if (is_array($keys = $this->_getCacheKeys())) {
foreach ($keys as $key) {
$count++;
$this->delete($key);
}
}
return $count;
}
Doctrine_Cache_Driver::deleteByPrefix (   $prefix)
inherited

Delete cache entries where the key has the passed prefix

Parameters
string$prefix
Returns
integer $count The number of deleted cache entries

Definition at line 167 of file Driver.php.

{
$count = 0;
$keys = $this->_getCacheKeys();
if (is_array($keys)) {
foreach ($keys as $key) {
if (strpos($key, $prefix) === 0) {
$count++;
$this->delete($key);
}
}
}
return $count;
}
Doctrine_Cache_Driver::deleteByRegex (   $regex)
inherited

Delete cache entries where the key matches a PHP regular expressions

Parameters
string$regex
Returns
integer $count The number of deleted cache entries

Definition at line 146 of file Driver.php.

{
$count = 0;
$keys = $this->_getCacheKeys();
if (is_array($keys)) {
foreach ($keys as $key) {
if (preg_match($regex, $key)) {
$count++;
$this->delete($key);
}
}
}
return $count;
}
Doctrine_Cache_Driver::deleteBySuffix (   $suffix)
inherited

Delete cache entries where the key has the passed suffix

Parameters
string$suffix
Returns
integer $count The number of deleted cache entries

Definition at line 188 of file Driver.php.

{
$count = 0;
$keys = $this->_getCacheKeys();
if (is_array($keys)) {
foreach ($keys as $key) {
if (substr($key, -1 * strlen($suffix)) == $suffix) {
$count++;
$this->delete($key);
}
}
}
return $count;
}
Doctrine_Cache_Driver::fetch (   $id,
  $testCacheValidity = true 
)
inherited

Fetch a cache record from this cache driver instance

Parameters
string$idcache id
boolean$testCacheValidityif set to false, the cache validity won't be tested
Returns
mixed Returns either the cached data or false

Definition at line 89 of file Driver.php.

{
$key = $this->_getKey($id);
return $this->_doFetch($key, $testCacheValidity);
}
Doctrine_Cache_Db::getConnection ( )

Get the connection object associated with this cache driver

Returns
Doctrine_Connection $connection

Definition at line 65 of file Db.php.

{
return $this->_options['connection'];
}
Doctrine_Cache_Driver::getOption (   $option)
inherited

Get value of option

Parameters
mixed$optionthe option name
Returns
mixed option value

Definition at line 73 of file Driver.php.

{
if ( ! isset($this->_options[$option])) {
return null;
}
return $this->_options[$option];
}
Doctrine_Cache_Driver::save (   $id,
  $data,
  $lifeTime = false 
)
inherited

Save some string datas into a cache record

Parameters
string$idcache id
string$datadata to cache
int$lifeTimeif != false, set a specific lifetime for this cache record (null => infinite lifeTime)
Returns
boolean true if no problem

Definition at line 115 of file Driver.php.

{
$key = $this->_getKey($id);
return $this->_doSave($key, $data, $lifeTime);
}
Doctrine_Cache_Driver::setOption (   $option,
  $value 
)
inherited

Set option name and value

Parameters
mixed$optionthe option name
mixed$valueoption value
Returns
boolean TRUE on success, FALSE on failure

Definition at line 58 of file Driver.php.

{
if (isset($this->_options[$option])) {
$this->_options[$option] = $value;
return true;
}
return false;
}

The documentation for this class was generated from the following file: