Source for file Cache.php
Documentation is available at Cache.php
* $Id: Cache.php 1857 2007-06-26 22:30:23Z subzero2000 $
* 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>.
* @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
* @version $Revision: 1857 $
* @var array $_options an array of general caching options
'addStatsPropability' =>
0.25,
'savePropability' =>
0.10,
'cleanPropability' =>
0.01,
'statsFile' =>
'../data/stats.cache',
* @var array $_queries query stack
* @var Doctrine_Cache_Interface $_driver the cache driver object
* @var array $data current cache data array
* @var boolean $success the success of last operation
* @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())
$this->_driver->setOptions($options);
$this->_driver =
new $class($options);
* returns the current cache driver
* @return Doctrine_Cache_Driver
* @param mixed $option the option name
* @param mixed $value option value
* @return boolean TRUE on success, FALSE on failure
// sanity check (we need this since we are using isset() instead of array_key_exists())
* @param mixed $option the option name
* @return mixed option value
if ( ! isset
($this->_options[$option])) {
* adds a query to internal query stack
* @param string|array$query sql query string
* @param string $namespace connection namespace
public function add($query, $namespace =
null)
* @param string $namespace optional query namespace
* @return array an array of sql query strings
public function getAll($namespace =
null)
if( ! isset
($this->_queries[$namespace])) {
* pops a query from the stack
* removes all queries from the query stack
* @return integer the number of queries in the stack
* @return ArrayIterator an iterator that iterates through the query stack
return new ArrayIterator($this->_queries);
* @return boolean whether or not the last cache operation was successful
if ($rand <=
$this->_options['cleanPropability']) {
foreach ($queries as $query) {
if (isset
($stats[$query])) {
if ($this->_options['statsFile'] !==
false) {
* adds all queries to stats file
if ($this->_options['statsFile'] !==
false) {
if ($rand <=
$this->_options['addStatsPropability']) {
* listens on the Doctrine_Event preQuery event
* adds the issued query to internal query stack
* and checks if cached element exists
public function preQuery(Doctrine_Event $event)
$query =
$event->getQuery();
// only process SELECT statements
$this->add($query, $event->getInvoker()->getName());
$this->success =
($data) ?
true :
false;
if ($rand <
$this->_options['savePropability']) {
$stmt =
$event->getInvoker()->getAdapter()->query($query);
$data =
$stmt->fetchAll(Doctrine::FETCH_ASSOC);
* listens the preFetch event of Doctrine_Connection_Statement
* advances the internal pointer of cached data and returns
public function preFetch(Doctrine_Event $event)
* listens the preFetchAll event of Doctrine_Connection_Statement
* returns the current cache data array
* listens the preExecute event of Doctrine_Connection_Statement
* adds the issued query to internal query stack
* and checks if cached element exists
$query =
$event->getQuery();
// only process SELECT statements
$this->add($query, $event->getInvoker()->getDbh()->getName());
$this->success =
($data) ?
true :
false;
if ($rand <=
$this->_options['savePropability']) {
$stmt =
$event->getInvoker()->getStatement();
$stmt->execute($event->getParams());
$data =
$stmt->fetchAll(Doctrine::FETCH_ASSOC);