2007-01-29 16:13:43 +03:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This software consists of voluntary contributions made by many individuals
|
|
|
|
* and is licensed under the LGPL. For more information, see
|
|
|
|
* <http://www.phpdoctrine.com>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Doctrine_Cache
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
|
|
|
* @subpackage Doctrine_Cache
|
|
|
|
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
|
* @category Object Relational Mapping
|
|
|
|
* @link www.phpdoctrine.com
|
|
|
|
* @since 1.0
|
|
|
|
* @version $Revision$
|
|
|
|
*/
|
2007-02-01 01:51:26 +03:00
|
|
|
class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, IteratorAggregate
|
2007-01-29 16:13:43 +03:00
|
|
|
{
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* @var array $_options an array of general caching options
|
|
|
|
*/
|
2007-02-09 01:06:03 +03:00
|
|
|
protected $_options = array('size' => 1000,
|
|
|
|
'lifeTime' => 3600,
|
|
|
|
'addStatsPropability' => 0.25,
|
|
|
|
'savePropability' => 0.10,
|
|
|
|
'cleanPropability' => 0.01,
|
|
|
|
'statsFile' => '../data/stats.cache',
|
2007-02-01 01:51:26 +03:00
|
|
|
);
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* @var array $_queries query stack
|
|
|
|
*/
|
2007-01-29 16:13:43 +03:00
|
|
|
protected $_queries = array();
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* @var Doctrine_Cache_Interface $_driver the cache driver object
|
|
|
|
*/
|
2007-01-29 16:13:43 +03:00
|
|
|
protected $_driver;
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* @var array $data current cache data array
|
|
|
|
*/
|
2007-02-05 15:04:56 +03:00
|
|
|
protected $_data;
|
2007-02-07 19:04:50 +03:00
|
|
|
/**
|
|
|
|
* @var boolean $success the success of last operation
|
|
|
|
*/
|
|
|
|
protected $_success = false;
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* constructor
|
|
|
|
*
|
|
|
|
* @param Doctrine_Cache_Interface|string $driver cache driver name or a driver object
|
|
|
|
* @param array $options cache driver options
|
|
|
|
*/
|
|
|
|
public function __construct($driver, $options = array())
|
|
|
|
{
|
|
|
|
if (is_object($driver)) {
|
|
|
|
if ( ! ($driver instanceof Doctrine_Cache_Interface)) {
|
|
|
|
throw new Doctrine_Cache_Exception('Driver should implement Doctrine_Cache_Interface.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_driver = $driver;
|
|
|
|
$this->_driver->setOptions($options);
|
|
|
|
} else {
|
|
|
|
$class = 'Doctrine_Cache_' . ucwords(strtolower($driver));
|
2007-02-05 15:04:56 +03:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
if ( ! class_exists($class)) {
|
|
|
|
throw new Doctrine_Cache_Exception('Cache driver ' . $driver . ' could not be found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_driver = new $class($options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getDriver
|
|
|
|
* returns the current cache driver
|
|
|
|
*
|
|
|
|
* @return Doctrine_Cache_Driver
|
|
|
|
*/
|
|
|
|
public function getDriver()
|
2007-02-01 01:51:26 +03:00
|
|
|
{
|
2007-02-06 23:45:51 +03:00
|
|
|
return $this->_driver;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* setOption
|
|
|
|
*
|
|
|
|
* @param mixed $option the option name
|
|
|
|
* @param mixed $value option value
|
|
|
|
* @return boolean TRUE on success, FALSE on failure
|
|
|
|
*/
|
|
|
|
public function setOption($option, $value)
|
|
|
|
{
|
|
|
|
// sanity check (we need this since we are using isset() instead of array_key_exists())
|
|
|
|
if ($value === null) {
|
|
|
|
throw new Doctrine_Cache_Exception('Null values not accepted for options.');
|
|
|
|
}
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
if (isset($this->_options[$option])) {
|
|
|
|
$this->_options[$option] = $value;
|
|
|
|
return true;
|
2007-02-01 01:51:26 +03:00
|
|
|
}
|
2007-02-06 23:45:51 +03:00
|
|
|
return false;
|
2007-02-01 01:51:26 +03:00
|
|
|
}
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* getOption
|
|
|
|
*
|
|
|
|
* @param mixed $option the option name
|
|
|
|
* @return mixed option value
|
|
|
|
*/
|
|
|
|
public function getOption($option)
|
2007-02-05 15:04:56 +03:00
|
|
|
{
|
2007-02-06 23:45:51 +03:00
|
|
|
if ( ! isset($this->_options[$option])) {
|
|
|
|
throw new Doctrine_Cache_Exception('Unknown option ' . $option);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_options[$option];
|
2007-02-05 15:04:56 +03:00
|
|
|
}
|
2007-01-29 16:13:43 +03:00
|
|
|
/**
|
2007-02-06 23:45:51 +03:00
|
|
|
* add
|
|
|
|
* adds a query to internal query stack
|
2007-01-29 16:13:43 +03:00
|
|
|
*
|
2007-02-05 15:04:56 +03:00
|
|
|
* @param string|array $query sql query string
|
|
|
|
* @param string $namespace connection namespace
|
2007-01-29 16:13:43 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-01 01:51:26 +03:00
|
|
|
public function add($query, $namespace = null)
|
|
|
|
{
|
|
|
|
if (isset($namespace)) {
|
|
|
|
$this->_queries[$namespace][] = $query;
|
|
|
|
} else {
|
|
|
|
$this->_queries[] = $query;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getQueries
|
|
|
|
*
|
|
|
|
* @param string $namespace optional query namespace
|
|
|
|
* @return array an array of sql query strings
|
|
|
|
*/
|
|
|
|
public function getAll($namespace = null)
|
|
|
|
{
|
|
|
|
if (isset($namespace)) {
|
|
|
|
if( ! isset($this->_queries[$namespace])) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_queries[$namespace];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_queries;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* pop
|
|
|
|
*
|
|
|
|
* pops a query from the stack
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function pop()
|
2007-01-29 16:13:43 +03:00
|
|
|
{
|
2007-02-01 01:51:26 +03:00
|
|
|
return array_pop($this->_queries);
|
|
|
|
}
|
2007-02-01 02:00:43 +03:00
|
|
|
/**
|
|
|
|
* reset
|
|
|
|
*
|
|
|
|
* removes all queries from the query stack
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function reset()
|
|
|
|
{
|
|
|
|
$this->_queries = array();
|
|
|
|
}
|
2007-02-01 01:51:26 +03:00
|
|
|
/**
|
|
|
|
* count
|
|
|
|
*
|
2007-02-01 02:00:43 +03:00
|
|
|
* @return integer the number of queries in the stack
|
2007-02-01 01:51:26 +03:00
|
|
|
*/
|
|
|
|
public function count()
|
|
|
|
{
|
2007-02-01 02:00:43 +03:00
|
|
|
return count($this->_queries);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* getIterator
|
|
|
|
*
|
|
|
|
* @return ArrayIterator an iterator that iterates through the query stack
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
|
|
|
return new ArrayIterator($this->_queries);
|
2007-01-29 16:13:43 +03:00
|
|
|
}
|
2007-02-07 19:04:50 +03:00
|
|
|
/**
|
|
|
|
* @return boolean whether or not the last cache operation was successful
|
|
|
|
*/
|
|
|
|
public function isSuccessful()
|
|
|
|
{
|
|
|
|
return $this->_success;
|
|
|
|
}
|
2007-01-29 16:13:43 +03:00
|
|
|
/**
|
|
|
|
* save
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-02-09 01:06:03 +03:00
|
|
|
public function clean()
|
2007-01-29 16:13:43 +03:00
|
|
|
{
|
2007-02-09 01:06:03 +03:00
|
|
|
$rand = (mt_rand() / mt_getrandmax());
|
2007-01-29 16:13:43 +03:00
|
|
|
|
2007-02-09 01:06:03 +03:00
|
|
|
if ($rand <= $this->_options['cleanPropability']) {
|
|
|
|
$content = file_get_contents($this->_statsFile);
|
|
|
|
$queries = explode("\n", $content);
|
2007-01-29 16:13:43 +03:00
|
|
|
|
2007-02-09 01:06:03 +03:00
|
|
|
$stats = array();
|
|
|
|
|
|
|
|
foreach ($queries as $query) {
|
|
|
|
if (is_array($query)) {
|
|
|
|
$query = $query[0];
|
|
|
|
}
|
|
|
|
if (isset($stats[$query])) {
|
|
|
|
$stats[$query]++;
|
|
|
|
} else {
|
|
|
|
$stats[$query] = 1;
|
|
|
|
}
|
2007-02-05 15:04:56 +03:00
|
|
|
}
|
2007-02-09 01:06:03 +03:00
|
|
|
sort($stats);
|
|
|
|
|
|
|
|
$i = $this->_options['size'];
|
|
|
|
|
|
|
|
while ($i--) {
|
|
|
|
$element = next($stats);
|
|
|
|
$query = key($stats);
|
|
|
|
|
|
|
|
if (is_array($query)) {
|
|
|
|
$hash = md5(serialize($query));
|
|
|
|
} else {
|
|
|
|
$hash = md5($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_driver->delete($hash);
|
2007-01-29 16:13:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2007-02-09 01:06:03 +03:00
|
|
|
* appendStats
|
2007-01-29 16:13:43 +03:00
|
|
|
*
|
|
|
|
* adds all queries to stats file
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-02-09 01:06:03 +03:00
|
|
|
public function appendStats()
|
2007-01-29 16:13:43 +03:00
|
|
|
{
|
2007-02-06 23:45:51 +03:00
|
|
|
if ($this->_options['statsFile'] !== false) {
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
if ( ! file_exists($this->_options['statsFile'])) {
|
|
|
|
throw new Doctrine_Cache_Exception("Couldn't save cache statistics. Cache statistics file doesn't exists!");
|
|
|
|
}
|
2007-02-09 01:06:03 +03:00
|
|
|
|
|
|
|
$rand = (mt_rand() / mt_getrandmax());
|
2007-02-06 23:45:51 +03:00
|
|
|
|
2007-02-09 01:06:03 +03:00
|
|
|
if ($rand <= $this->_options['addStatsPropability']) {
|
|
|
|
file_put_contents($this->_options['statsFile'], implode("\n", $this->_queries));
|
|
|
|
}
|
2007-02-06 23:45:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* onPreQuery
|
|
|
|
* listens the onPreQuery event of Doctrine_Db
|
|
|
|
*
|
|
|
|
* adds the issued query to internal query stack
|
|
|
|
* and checks if cached element exists
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-02-01 01:51:26 +03:00
|
|
|
public function onPreQuery(Doctrine_Db_Event $event)
|
|
|
|
{
|
2007-02-05 15:04:56 +03:00
|
|
|
$query = $event->getQuery();
|
2007-02-09 00:29:06 +03:00
|
|
|
|
|
|
|
$data = false;
|
2007-02-05 15:04:56 +03:00
|
|
|
// only process SELECT statements
|
|
|
|
if (substr(trim(strtoupper($query)), 0, 6) == 'SELECT') {
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
$this->add($query, $event->getInvoker()->getName());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
$data = $this->_driver->fetch(md5($query));
|
|
|
|
|
2007-02-07 19:04:50 +03:00
|
|
|
$this->success = ($data) ? true : false;
|
|
|
|
|
2007-02-09 00:29:06 +03:00
|
|
|
if ( ! $data) {
|
2007-02-09 01:06:03 +03:00
|
|
|
$rand = (mt_rand() / mt_getrandmax());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
|
|
|
if ($rand < $this->_options['savePropability']) {
|
2007-02-09 01:06:03 +03:00
|
|
|
$stmt = $event->getInvoker()->getAdapter()->query($query);
|
2007-02-09 00:29:06 +03:00
|
|
|
|
|
|
|
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->success = true;
|
|
|
|
|
|
|
|
$this->_driver->save(md5($query), $data);
|
|
|
|
}
|
|
|
|
}
|
2007-02-05 15:04:56 +03:00
|
|
|
$this->_data = $data;
|
|
|
|
}
|
2007-02-09 00:29:06 +03:00
|
|
|
return (bool) $data;
|
2007-02-01 01:51:26 +03:00
|
|
|
}
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* onPreFetch
|
|
|
|
* listens the onPreFetch event of Doctrine_Db_Statement
|
|
|
|
*
|
|
|
|
* advances the internal pointer of cached data and returns
|
|
|
|
* the current element
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function onPreFetch(Doctrine_Db_Event $event)
|
2007-02-05 15:04:56 +03:00
|
|
|
{
|
2007-02-06 23:45:51 +03:00
|
|
|
$ret = current($this->_data);
|
|
|
|
next($this->_data);
|
|
|
|
return $ret;
|
2007-02-01 01:51:26 +03:00
|
|
|
}
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* onPreFetch
|
|
|
|
* listens the onPreFetchAll event of Doctrine_Db_Statement
|
|
|
|
*
|
|
|
|
* returns the current cache data array
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2007-02-05 15:04:56 +03:00
|
|
|
public function onPreFetchAll(Doctrine_Db_Event $event)
|
|
|
|
{
|
|
|
|
return $this->_data;
|
|
|
|
}
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* onPreExecute
|
|
|
|
* listens the onPreExecute event of Doctrine_Db_Statement
|
|
|
|
*
|
|
|
|
* adds the issued query to internal query stack
|
|
|
|
* and checks if cached element exists
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2007-02-01 01:51:26 +03:00
|
|
|
public function onPreExecute(Doctrine_Db_Event $event)
|
2007-02-06 23:45:51 +03:00
|
|
|
{
|
2007-02-05 15:04:56 +03:00
|
|
|
$query = $event->getQuery();
|
2007-02-09 00:29:06 +03:00
|
|
|
|
|
|
|
$data = false;
|
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
// only process SELECT statements
|
|
|
|
if (substr(trim(strtoupper($query)), 0, 6) == 'SELECT') {
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
$this->add($query, $event->getInvoker()->getDbh()->getName());
|
2007-02-07 19:04:50 +03:00
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
$data = $this->_driver->fetch(md5(serialize(array($query, $event->getParams()))));
|
|
|
|
|
2007-02-07 19:04:50 +03:00
|
|
|
$this->success = ($data) ? true : false;
|
|
|
|
|
2007-02-09 00:29:06 +03:00
|
|
|
if ( ! $data) {
|
2007-02-09 01:06:03 +03:00
|
|
|
$rand = (mt_rand() / mt_getrandmax());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-02-09 01:06:03 +03:00
|
|
|
if ($rand <= $this->_options['savePropability']) {
|
|
|
|
|
|
|
|
$stmt = $event->getInvoker()->getStatement();
|
|
|
|
|
|
|
|
$stmt->execute($event->getParams());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
|
|
|
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
|
|
|
|
|
|
|
|
$this->success = true;
|
|
|
|
|
|
|
|
$this->_driver->save(md5(serialize(array($query, $event->getParams()))), $data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
$this->_data = $data;
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-02-05 15:04:56 +03:00
|
|
|
}
|
2007-02-09 00:29:06 +03:00
|
|
|
return (bool) $data;
|
2007-02-01 01:51:26 +03:00
|
|
|
}
|
2007-01-29 16:13:43 +03:00
|
|
|
}
|