Browse Source

Finished all cache components.

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
78623f3ebb
  1. 179
      framework/caching/DbCache.php
  2. 185
      framework/caching/FileCache.php
  3. 2
      framework/caching/MemCache.php
  4. 12
      framework/db/dao/Command.php

179
framework/caching/DbCache.php

@ -16,16 +16,28 @@ use yii\db\dao\Query;
/** /**
* DbCache implements a cache application component by storing cached data in a database. * DbCache implements a cache application component by storing cached data in a database.
* *
* DbCache stores cache data in a DB table named {@link cacheTableName}. * DbCache stores cache data in a DB table whose name is specified via [[cacheTableName]].
* If the table does not exist, it will be automatically created. * For MySQL database, the table should be created beforehand as follows :
* By setting {@link autoCreateCacheTable} to false, you can also manually create the DB table.
* *
* DbCache relies on {@link http://www.php.net/manual/en/ref.pdo.php PDO} to access database. * ~~~
* By default, it will use a SQLite3 database under the application runtime directory. * CREATE TABLE tbl_cache (
* You can also specify {@link connectionID} so that it makes use of * id char(128) NOT NULL,
* a DB application component to access database. * expire int(11) DEFAULT NULL,
* data LONGBLOB,
* PRIMARY KEY (id),
* KEY expire (expire)
* );
* ~~~
* *
* See {@link CCache} manual for common cache operations that are supported by DbCache. * You should replace `LONGBLOB` as follows if you are using a different DBMS:
*
* - PostgreSQL: `BYTEA`
* - SQLite, SQL server, Oracle: `BLOB`
*
* DbCache connects to the database via the DB connection specified in [[connectionID]]
* which must refer to a valid DB application component.
*
* Please refer to [[Cache]] for common cache operations that are supported by DbCache.
* *
* @property Connection $dbConnection The DB connection instance. * @property Connection $dbConnection The DB connection instance.
* *
@ -35,30 +47,16 @@ use yii\db\dao\Query;
class DbCache extends Cache class DbCache extends Cache
{ {
/** /**
* @var string the ID of the {@link Connection} application component. If not set, * @var string the ID of the [[Connection|DB connection]] application component.
* a SQLite3 database will be automatically created and used. The SQLite database file * Defaults to 'db'.
* is <code>protected/runtime/cache-YiiVersion.db</code>.
*/ */
public $connectionID; public $connectionID = 'db';
/** /**
* @var string name of the DB table to store cache content. Defaults to 'YiiCache'. * @var string name of the DB table to store cache content. Defaults to 'tbl_cache'.
* Note, if {@link autoCreateCacheTable} is false and you want to create the DB table * The table must be created before using this cache component.
* manually by yourself, you need to make sure the DB table is of the following structure:
* <pre>
* (id CHAR(128) PRIMARY KEY, expire INTEGER, value BLOB)
* </pre>
* 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.)
* @see autoCreateCacheTable
*/ */
public $cacheTableName = 'tbl_cache'; public $cacheTableName = 'tbl_cache';
/** /**
* @var boolean whether the cache DB table should be created automatically if it does not exist. Defaults to true.
* If you already have the table created, it is recommended you set this property to be false to improve performance.
* @see cacheTableName
*/
public $autoCreateCacheTable = true;
/**
* @var integer the probability (parts per million) that garbage collection (GC) should be performed * @var integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance. * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all. * This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
@ -70,57 +68,6 @@ class DbCache extends Cache
private $_db; private $_db;
/** /**
* Initializes this application component.
*
* This method is required by the {@link IApplicationComponent} interface.
* It ensures the existence of the cache DB table.
* It also removes expired data items from the cache.
*/
public function init()
{
parent::init();
$db = $this->getDbConnection();
$db->setActive(true);
if ($this->autoCreateCacheTable) {
$sql = "DELETE FROM {$this->cacheTableName} WHERE expire>0 AND expire<" . time();
try {
$db->createCommand($sql)->execute();
} catch (Exception $e) {
$this->createCacheTable($db, $this->cacheTableName);
}
}
}
/**
* Creates the cache DB table.
* @param Connection $db the database connection
* @param string $tableName the name of the table to be created
*/
protected function createCacheTable($db, $tableName)
{
$driver = $db->getDriverName();
if ($driver === 'mysql') {
$blob = 'LONGBLOB';
} else {
if ($driver === 'pgsql') {
$blob = 'BYTEA';
} else {
$blob = 'BLOB';
}
}
$sql = <<<EOD
CREATE TABLE $tableName
(
id CHAR(128) PRIMARY KEY,
expire INTEGER,
value $blob
)
EOD;
$db->createCommand($sql)->execute();
}
/**
* Returns the DB connection instance used for caching purpose. * Returns the DB connection instance used for caching purpose.
* @return Connection the DB connection instance * @return Connection the DB connection instance
* @throws Exception if [[connectionID]] does not point to a valid application component. * @throws Exception if [[connectionID]] does not point to a valid application component.
@ -128,7 +75,8 @@ EOD;
public function getDbConnection() public function getDbConnection()
{ {
if ($this->_db === null) { if ($this->_db === null) {
if ($this->connectionID !== null && ($db = \Yii::$application->getComponent($this->connectionID)) instanceof Connection) { $db = \Yii::$application->getComponent($this->connectionID);
if ($db instanceof Connection) {
$this->_db = $db; $this->_db = $db;
} else { } else {
throw new Exception("DbCache.connectionID must refer to the ID of a DB connection application component."); throw new Exception("DbCache.connectionID must refer to the ID of a DB connection application component.");
@ -155,10 +103,9 @@ EOD;
protected function getValue($key) protected function getValue($key)
{ {
$query = new Query; $query = new Query;
$query->select(array('value')) $query->select(array('data'))
->from($this->cacheTableName) ->from($this->cacheTableName)
->where(array('id' => $key)) ->where('id = :id AND (expire = 0 OR expire > :time)', array(':id' => $key, ':time' => time()));
->andWhere('expire = 0 OR expire > ' . time());
$db = $this->getDbConnection(); $db = $this->getDbConnection();
if ($db->queryCachingDuration >= 0) { if ($db->queryCachingDuration >= 0) {
$duration = $db->queryCachingDuration; $duration = $db->queryCachingDuration;
@ -181,19 +128,20 @@ EOD;
if (empty($keys)) { if (empty($keys)) {
return array(); return array();
} }
$query = new Query;
$ids = implode("','", $keys); $query->select(array('id', 'data'))
$time = time(); ->from($this->cacheTableName)
$sql = "SELECT id, value FROM {$this->cacheTableName} WHERE id IN ('$ids') AND (expire=0 OR expire>$time)"; ->where(array('id' => $keys))
->andWhere("expire = 0 OR expire > " . time() . ")");
$db = $this->getDbConnection(); $db = $this->getDbConnection();
if ($db->queryCachingDuration > 0) { if ($db->queryCachingDuration >= 0) {
$duration = $db->queryCachingDuration; $duration = $db->queryCachingDuration;
$db->queryCachingDuration = 0; $db->queryCachingDuration = -1;
$rows = $db->createCommand($sql)->queryAll(); $rows = $query->createCommand($db)->queryAll();
$db->queryCachingDuration = $duration; $db->queryCachingDuration = $duration;
} else { } else {
$rows = $db->createCommand($sql)->queryAll(); $rows = $query->createCommand($db)->queryAll();
} }
$results = array(); $results = array();
@ -201,7 +149,7 @@ EOD;
$results[$key] = false; $results[$key] = false;
} }
foreach ($rows as $row) { foreach ($rows as $row) {
$results[$row['id']] = $row['value']; $results[$row['id']] = $row['data'];
} }
return $results; return $results;
} }
@ -217,9 +165,21 @@ EOD;
*/ */
protected function setValue($key, $value, $expire) protected function setValue($key, $value, $expire)
{ {
$this->deleteValue($key); $query = new Query;
$command = $query->update($this->cacheTableName, array(
'expire' => $expire > 0 ? $expire + time() : 0,
'data' => array($value, \PDO::PARAM_LOB),
), array(
'id' => $key,
))->createCommand($this->getDbConnection());
if ($command->execute()) {
$this->gc();
return true;
} else {
return $this->addValue($key, $value, $expire); return $this->addValue($key, $value, $expire);
} }
}
/** /**
* Stores a value identified by a key into cache if the cache does not contain this key. * Stores a value identified by a key into cache if the cache does not contain this key.
@ -232,19 +192,21 @@ EOD;
*/ */
protected function addValue($key, $value, $expire) protected function addValue($key, $value, $expire)
{ {
if (mt_rand(0, 1000000) < $this->_gcProbability) {
$this->gc(); $this->gc();
}
if ($expire > 0) { if ($expire > 0) {
$expire += time(); $expire += time();
} else { } else {
$expire = 0; $expire = 0;
} }
$sql = "INSERT INTO {$this->cacheTableName} (id,expire,value) VALUES ('$key',$expire,:value)";
$query = new Query;
$command = $query->insert($this->cacheTableName, array(
'id' => $key,
'expire' => $expire,
'data' => array($value, \PDO::PARAM_LOB),
))->createCommand($this->getDbConnection());
try { try {
$command = $this->getDbConnection()->createCommand($sql);
$command->bindValue(':value', $value, PDO::PARAM_LOB);
$command->execute(); $command->execute();
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {
@ -260,28 +222,39 @@ EOD;
*/ */
protected function deleteValue($key) protected function deleteValue($key)
{ {
$sql = "DELETE FROM {$this->cacheTableName} WHERE id='$key'"; $query = new Query;
$this->getDbConnection()->createCommand($sql)->execute(); $query->delete($this->cacheTableName, array('id' => $key))
->createCommand($this->getDbConnection())
->execute();
return true; return true;
} }
/** /**
* Removes the expired data values. * Removes the expired data values.
* @param boolean $force whether to enforce the garbage collection regardless of [[gcProbability]].
* Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
*/ */
protected function gc() public function gc($force = false)
{ {
$this->getDbConnection()->createCommand("DELETE FROM {$this->cacheTableName} WHERE expire>0 AND expire<" . time())->execute(); if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$query = new Query;
$query->delete($this->cacheTableName, 'expire > 0 AND expire < ' . time())
->createCommand($this->getDbConnection())
->execute();
}
} }
/** /**
* Deletes all values from cache. * Deletes all values from cache.
* This is the implementation of the method declared in the parent class. * This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful. * @return boolean whether the flush operation was successful.
* @since 1.1.5
*/ */
protected function flushValues() protected function flushValues()
{ {
$this->getDbConnection()->createCommand("DELETE FROM {$this->cacheTableName}")->execute(); $query = new Query;
$query->delete($this->cacheTableName)
->createCommand($this->getDbConnection())
->execute();
return true; return true;
} }
} }

185
framework/caching/FileCache.php

@ -1,6 +1,6 @@
<?php <?php
/** /**
* CFileCache class file * FileCache class file
* *
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
@ -9,92 +9,57 @@
namespace yii\caching; namespace yii\caching;
use yii\base\Exception;
/** /**
* CFileCache provides a file-based caching mechanism. * FileCache implements a cache component using files.
*
* For each data value being cached, CFileCache will use store it in a separate file
* under {@link cachePath} which defaults to 'protected/runtime/cache'.
* CFileCache will perform garbage collection automatically to remove expired cache files.
* *
* See {@link CCache} manual for common cache operations that are supported by CFileCache. * For each data value being cached, FileCache will store it in a separate file.
* The cache files are placed under [[cachePath]]. FileCache will perform garbage collection
* automatically to remove expired cache files.
* *
* @property integer $gCProbability The probability (parts per million) that garbage collection (GC) should be performed * Please refer to [[Cache]] for common cache operations that are supported by FileCache.
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class CFileCache extends CCache class FileCache extends Cache
{ {
/** /**
* @var string the directory to store cache files. Defaults to null, meaning * @var string the directory to store cache files. You may use path alias here.
* using 'protected/runtime/cache' as the directory.
*/ */
public $cachePath; public $cachePath = '@application/runtime/cache';
/** /**
* @var string cache file suffix. Defaults to '.bin'. * @var string cache file suffix. Defaults to '.bin'.
*/ */
public $cacheFileSuffix = '.bin'; public $cacheFileSuffix = '.bin';
/** /**
* @var integer the level of sub-directories to store cache files. Defaults to 0, * @var integer the level of sub-directories to store cache files. Defaults to 1.
* meaning no sub-directories. If the system has huge number of cache files (e.g. 10K+), * If the system has huge number of cache files (e.g. one million), you may use a bigger value
* you may want to set this value to be 1 or 2 so that the file system is not over burdened. * (usually no bigger than 3). Using sub-directories is mainly to ensure the file system
* The value of this property should not exceed 16 (less than 3 is recommended). * is not over burdened with a single directory having too many files.
*/ */
public $directoryLevel=0; public $directoryLevel = 1;
/**
private $_gcProbability=100; * @var integer the probability (parts per million) that garbage collection (GC) should be performed
private $_gced=false; * when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
**/
public $gcProbability = 100;
/** /**
* Initializes this application component. * Initializes this component by ensuring the existence of the cache path.
* This method is required by the {@link IApplicationComponent} interface.
* It checks the availability of memcache.
* @throws CException if APC cache extension is not loaded or is disabled.
*/ */
public function init() public function init()
{ {
parent::init(); parent::init();
if($this->cachePath===null) $this->cachePath = \Yii::getAlias($this->cachePath);
$this->cachePath=\Yii::$application->getRuntimePath().DIRECTORY_SEPARATOR.'cache'; if ($this->cachePath === false) {
if(!is_dir($this->cachePath)) throw new Exception('FileCache.cachePath must be a valid path alias.');
mkdir($this->cachePath,0777,true);
} }
if (!is_dir($this->cachePath)) {
/** mkdir($this->cachePath, 0777, true);
* @return integer the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
*/
public function getGCProbability()
{
return $this->_gcProbability;
}
/**
* @param integer $value the probability (parts per million) that garbage collection (GC) should be performed
* when storing a piece of data in the cache. Defaults to 100, meaning 0.01% chance.
* This number should be between 0 and 1000000. A value 0 meaning no GC will be performed at all.
*/
public function setGCProbability($value)
{
$value=(int)$value;
if($value<0)
$value=0;
if($value>1000000)
$value=1000000;
$this->_gcProbability=$value;
} }
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
* @since 1.1.5
*/
protected function flushValues()
{
$this->gc(false);
return true;
} }
/** /**
@ -106,12 +71,12 @@ class CFileCache extends CCache
protected function getValue($key) protected function getValue($key)
{ {
$cacheFile = $this->getCacheFile($key); $cacheFile = $this->getCacheFile($key);
if(($time=@filemtime($cacheFile))>time()) if (($time = @filemtime($cacheFile)) > time()) {
return @file_get_contents($cacheFile); return @file_get_contents($cacheFile);
else if($time>0) } else {
@unlink($cacheFile);
return false; return false;
} }
}
/** /**
* Stores a value identified by a key in cache. * Stores a value identified by a key in cache.
@ -124,27 +89,22 @@ class CFileCache extends CCache
*/ */
protected function setValue($key, $value, $expire) protected function setValue($key, $value, $expire)
{ {
if(!$this->_gced && mt_rand(0,1000000)<$this->_gcProbability) if ($expire <= 0) {
{
$this->gc();
$this->_gced=true;
}
if($expire<=0)
$expire = 31536000; // 1 year $expire = 31536000; // 1 year
}
$expire += time(); $expire += time();
$cacheFile = $this->getCacheFile($key); $cacheFile = $this->getCacheFile($key);
if($this->directoryLevel>0) if ($this->directoryLevel > 0) {
@mkdir(dirname($cacheFile), 0777, true); @mkdir(dirname($cacheFile), 0777, true);
if(@file_put_contents($cacheFile,$value,LOCK_EX)!==false) }
{ if (@file_put_contents($cacheFile, $value, LOCK_EX) !== false) {
@chmod($cacheFile, 0777); @chmod($cacheFile, 0777);
return @touch($cacheFile, $expire); return @touch($cacheFile, $expire);
} } else {
else
return false; return false;
} }
}
/** /**
* Stores a value identified by a key into cache if the cache does not contain this key. * Stores a value identified by a key into cache if the cache does not contain this key.
@ -158,8 +118,9 @@ class CFileCache extends CCache
protected function addValue($key, $value, $expire) protected function addValue($key, $value, $expire)
{ {
$cacheFile = $this->getCacheFile($key); $cacheFile = $this->getCacheFile($key);
if(@filemtime($cacheFile)>time()) if (@filemtime($cacheFile) > time()) {
return false; return false;
}
return $this->setValue($key, $value, $expire); return $this->setValue($key, $value, $expire);
} }
@ -182,41 +143,69 @@ class CFileCache extends CCache
*/ */
protected function getCacheFile($key) protected function getCacheFile($key)
{ {
if($this->directoryLevel>0) if ($this->directoryLevel > 0) {
{
$base = $this->cachePath; $base = $this->cachePath;
for($i=0;$i<$this->directoryLevel;++$i) for ($i = 0; $i < $this->directoryLevel; ++$i) {
{ if (($prefix = substr($key, $i + $i, 2)) !== false) {
if(($prefix=substr($key,$i+$i,2))!==false)
$base .= DIRECTORY_SEPARATOR . $prefix; $base .= DIRECTORY_SEPARATOR . $prefix;
} }
return $base.DIRECTORY_SEPARATOR.$key.$this->cacheFileSuffix;
} }
else return $base . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
} else {
return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix; return $this->cachePath . DIRECTORY_SEPARATOR . $key . $this->cacheFileSuffix;
} }
}
/**
* Deletes all values from cache.
* This is the implementation of the method declared in the parent class.
* @return boolean whether the flush operation was successful.
*/
protected function flushValues()
{
$this->gc(true, false);
return true;
}
/** /**
* Removes expired cache files. * Removes expired cache files.
* @param boolean $expiredOnly whether to removed expired cache files only. If true, all cache files under {@link cachePath} will be removed. * @param boolean $force whether to enforce the garbage collection regardless of [[gcProbability]].
* @param string $path the path to clean with. If null, it will be {@link cachePath}. * Defaults to false, meaning the actual deletion happens with the probability as specified by [[gcProbability]].
* @param boolean $expiredOnly whether to removed expired cache files only.
* If true, all cache files under [[cachePath]] will be removed.
*/ */
public function gc($expiredOnly=true,$path=null) public function gc($force = false, $expiredOnly = true)
{ {
if($path===null) if ($force || mt_rand(0, 1000000) < $this->gcProbability) {
$path=$this->cachePath; $this->gcRecursive($this->cachePath, $expiredOnly);
if(($handle=opendir($path))===false) }
return; }
while(($file=readdir($handle))!==false)
/**
* Recursively removing expired cache files under a directory.
* This method is mainly used by [[gc()]].
* @param string $path the directory under which expired cache files are removed.
* @param boolean $expiredOnly whether to only remove expired cache files. If false, all files
* under `$path` will be removed.
*/
protected function gcRecursive($path, $expiredOnly)
{ {
if($file[0]==='.') if (($handle = opendir($path)) !== false) {
while (($file = readdir($handle)) !== false) {
if ($file[0] === '.') {
continue; continue;
}
$fullPath = $path . DIRECTORY_SEPARATOR . $file; $fullPath = $path . DIRECTORY_SEPARATOR . $file;
if(is_dir($fullPath)) if (is_dir($fullPath)) {
$this->gc($expiredOnly,$fullPath); $this->gcRecursive($fullPath, $expiredOnly);
else if($expiredOnly && @filemtime($fullPath)<time() || !$expiredOnly) if (!$expiredOnly) {
@rmdir($fullPath);
}
} elseif (!$expiredOnly || $expiredOnly && @filemtime($fullPath) < time()) {
@unlink($fullPath); @unlink($fullPath);
} }
}
closedir($handle); closedir($handle);
} }
} }
}

2
framework/caching/MemCache.php

@ -54,7 +54,7 @@ use yii\base\Exception;
* In the above, two memcache servers are used: server1 and server2. You can configure more properties of * In the above, two memcache servers are used: server1 and server2. You can configure more properties of
* each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options. * each server, such as `persistent`, `weight`, `timeout`. Please see [[MemCacheServer]] for available options.
* *
* @property mixed $memCache The memcache instance (or memcached if [[useMemcached]] is true) used by this component. * @property \Memcache|\Memcached $memCache The memcache instance (or memcached if [[useMemcached]] is true) used by this component.
* @property MemCacheServer[] $servers List of memcache server configurations. * @property MemCacheServer[] $servers List of memcache server configurations.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>

12
framework/db/dao/Command.php

@ -186,7 +186,9 @@ class Command extends \yii\base\Component
* Note that the SQL data type of each value is determined by its PHP type. * Note that the SQL data type of each value is determined by its PHP type.
* @param array $values the values to be bound. This must be given in terms of an associative * @param array $values the values to be bound. This must be given in terms of an associative
* array with array keys being the parameter names, and array values the corresponding parameter values, * array with array keys being the parameter names, and array values the corresponding parameter values,
* e.g. `array(':name'=>'John', ':age'=>25)`. * e.g. `array(':name'=>'John', ':age'=>25)`. By default, the PDO type of each value is determined
* by its PHP type. You may explicitly specify the PDO type by using an array: `array(value, type)`,
* e.g. `array(':name'=>'John', ':profile'=>array($profile, \PDO::PARAM_LOB))`.
* @return Command the current command being executed * @return Command the current command being executed
*/ */
public function bindValues($values) public function bindValues($values)
@ -194,7 +196,13 @@ class Command extends \yii\base\Component
if (!empty($values)) { if (!empty($values)) {
$this->prepare(); $this->prepare();
foreach ($values as $name => $value) { foreach ($values as $name => $value) {
$this->pdoStatement->bindValue($name, $value, $this->connection->getPdoType(gettype($value))); if (is_array($value)) {
$type = $value[1];
$value = $value[0];
} else {
$type = $this->connection->getPdoType(gettype($value));
}
$this->pdoStatement->bindValue($name, $value, $type);
$this->_params[$name] = $value; $this->_params[$name] = $value;
} }
} }

Loading…
Cancel
Save