1
0
mirror of synced 2024-12-14 15:16:04 +03:00
doctrine2/lib/Doctrine/Cache.php

425 lines
11 KiB
PHP
Raw Normal View History

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.org>.
2007-01-29 16:13:43 +03:00
*/
Doctrine::autoload('Doctrine_EventListener');
2007-01-29 16:13:43 +03:00
/**
* Doctrine_Cache
*
* @package Doctrine
* @subpackage Cache
2007-01-29 16:13:43 +03:00
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
class Doctrine_Cache extends Doctrine_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-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
*/
protected $_data = array();
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);
2007-02-06 23:45:51 +03:00
} else {
$class = 'Doctrine_Cache_' . ucwords(strtolower($driver));
2007-02-06 23:45:51 +03:00
if ( ! class_exists($class)) {
throw new Doctrine_Cache_Exception('Cache driver ' . $driver . ' could not be found.');
}
2007-02-06 23:45:51 +03:00
$this->_driver = new $class($options);
}
}
2007-02-06 23:45:51 +03:00
/**
* 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;
}
2007-02-06 23:45:51 +03:00
/**
* 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) {
2007-02-06 23:45:51 +03:00
throw new Doctrine_Cache_Exception('Null values not accepted for options.');
}
2007-02-01 01:51:26 +03:00
if (isset($this->_options[$option])) {
2007-02-06 23:45:51 +03:00
$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)) {
2007-02-01 01:51:26 +03:00
$this->_queries[$namespace][] = $query;
} else {
$this->_queries[] = $query;
}
}
2007-02-01 01:51:26 +03:00
/**
* 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])) {
2007-02-01 01:51:26 +03:00
return array();
}
return $this->_queries[$namespace];
}
2007-02-01 01:51:26 +03:00
return $this->_queries;
}
2007-02-01 01:51:26 +03:00
/**
* 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);
}
2007-02-01 02:00:43 +03:00
/**
* 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
if ($rand <= $this->_options['cleanPropability']) {
2007-02-09 23:02:49 +03:00
$queries = $this->readStats();
2007-01-29 16:13:43 +03:00
2007-02-09 01:06:03 +03:00
$stats = array();
2007-02-09 01:06:03 +03:00
foreach ($queries as $query) {
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);
2007-02-09 01:06:03 +03:00
$i = $this->_options['size'];
2007-02-09 01:06:03 +03:00
while ($i--) {
$element = next($stats);
$query = key($stats);
2007-02-09 23:56:04 +03:00
2007-02-09 23:02:49 +03:00
$hash = md5($query);
2007-02-09 01:06:03 +03:00
$this->_driver->delete($hash);
2007-01-29 16:13:43 +03:00
}
}
}
2007-02-09 23:02:49 +03:00
/**
* readStats
*
* @return array
*/
public function readStats()
{
if ($this->_options['statsFile'] !== false) {
$content = file_get_contents($this->_options['statsFile']);
$e = explode("\n", $content);
return array_map('unserialize', $e);
}
return array();
2007-02-09 23:02:49 +03:00
}
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
{
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']) {
2007-02-09 23:02:49 +03:00
file_put_contents($this->_options['statsFile'], implode("\n", array_map('serialize', $this->_queries)));
2007-02-09 01:06:03 +03:00
}
2007-02-06 23:45:51 +03:00
}
}
2007-02-06 23:45:51 +03:00
/**
* preQuery
* listens on the Doctrine_Event preQuery event
2007-02-06 23:45:51 +03:00
*
* adds the issued query to internal query stack
* and checks if cached element exists
*
* @return boolean
*/
public function preQuery(Doctrine_Event $event)
2007-02-10 02:39:01 +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 (strtoupper(substr(ltrim($query), 0, 6)) != 'SELECT') {
return false;
}
2007-02-01 01:51:26 +03:00
$this->add($query, $event->getInvoker()->getName());
2007-02-09 00:29:06 +03:00
$data = $this->_driver->fetch(md5(serialize($query)));
2007-02-05 15:04:56 +03:00
$this->success = ($data) ? true : false;
2007-02-07 19:04:50 +03:00
if ( ! $data) {
$rand = (mt_rand() / mt_getrandmax());
2007-02-09 00:29:06 +03:00
if ($rand < $this->_options['savePropability']) {
$stmt = $event->getInvoker()->getAdapter()->query($query);
2007-02-09 00:29:06 +03:00
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
2007-02-09 00:29:06 +03:00
$this->success = true;
2007-02-09 00:29:06 +03:00
$this->_driver->save(md5(serialize($query)), $data);
2007-02-10 02:39:01 +03:00
}
2007-02-05 15:04:56 +03:00
}
if ($this->success)
{
$this->_data = $data;
return true;
}
2007-02-10 02:39:01 +03:00
return false;
2007-02-01 01:51:26 +03:00
}
2007-02-06 23:45:51 +03:00
/**
* preFetch
* listens the preFetch event of Doctrine_Connection_Statement
2007-02-06 23:45:51 +03:00
*
* advances the internal pointer of cached data and returns
* the current element
*
* @return array
*/
public function preFetch(Doctrine_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);
2007-02-06 23:45:51 +03:00
return $ret;
2007-02-01 01:51:26 +03:00
}
2007-02-06 23:45:51 +03:00
/**
* preFetch
* listens the preFetchAll event of Doctrine_Connection_Statement
2007-02-06 23:45:51 +03:00
*
* returns the current cache data array
*
* @return array
*/
public function preFetchAll(Doctrine_Event $event)
2007-02-05 15:04:56 +03:00
{
return $this->_data;
}
2007-02-06 23:45:51 +03:00
/**
* preExecute
* listens the preExecute event of Doctrine_Connection_Statement
2007-02-06 23:45:51 +03:00
*
* adds the issued query to internal query stack
* and checks if cached element exists
*
* @return boolean
*/
public function preExecute(Doctrine_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 (strtoupper(substr(ltrim($query), 0, 6)) != 'SELECT') {
return false;
}
2007-02-01 01:51:26 +03:00
$this->add($query, $event->getInvoker()->getDbh()->getName());
2007-02-07 19:04:50 +03:00
$data = $this->_driver->fetch(md5(serialize(array($query, $event->getParams()))));
2007-02-05 15:04:56 +03:00
$this->success = ($data) ? true : false;
2007-02-07 19:04:50 +03:00
if ( ! $data) {
$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()->getStatement();
2007-02-09 01:06:03 +03:00
$stmt->execute($event->getParams());
2007-02-09 00:29:06 +03:00
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
2007-02-09 00:29:06 +03:00
$this->success = true;
2007-02-09 00:29:06 +03:00
$this->_driver->save(md5(serialize(array($query, $event->getParams()))), $data);
2007-02-10 02:39:01 +03:00
}
2007-02-05 15:04:56 +03:00
}
if ($this->success)
{
$this->_data = $data;
return true;
}
2007-02-10 02:39:01 +03:00
return false;
2007-02-01 01:51:26 +03:00
}
2007-01-29 16:13:43 +03:00
}