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
|
2008-01-23 01:52:53 +03:00
|
|
|
* <http://www.phpdoctrine.org>.
|
2007-01-29 16:13:43 +03:00
|
|
|
*/
|
2008-05-30 16:09:24 +04:00
|
|
|
|
2007-01-29 16:13:43 +03:00
|
|
|
/**
|
|
|
|
* Doctrine_Cache
|
|
|
|
*
|
|
|
|
* @package Doctrine
|
2007-10-04 01:43:22 +04:00
|
|
|
* @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
|
2008-02-22 21:11:35 +03:00
|
|
|
* @link www.phpdoctrine.org
|
2007-01-29 16:13:43 +03:00
|
|
|
* @since 1.0
|
2008-06-15 19:56:28 +04:00
|
|
|
* @version $Revision$
|
|
|
|
* @todo Can this be removed?
|
2007-01-29 16:13:43 +03:00
|
|
|
*/
|
2007-06-25 21:48:44 +04:00
|
|
|
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,
|
2007-11-18 23:37:44 +03:00
|
|
|
'lifeTime' => 3600,
|
|
|
|
'addStatsPropability' => 0.25,
|
|
|
|
'savePropability' => 0.10,
|
|
|
|
'cleanPropability' => 0.01,
|
|
|
|
'statsFile' => '../data/stats.cache',
|
|
|
|
);
|
2007-10-21 10:23:59 +04: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-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
|
|
|
* @var array $data current cache data array
|
|
|
|
*/
|
2007-02-10 02:16:08 +03:00
|
|
|
protected $_data = array();
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-02-07 19:04:50 +03:00
|
|
|
/**
|
|
|
|
* @var boolean $success the success of last operation
|
|
|
|
*/
|
|
|
|
protected $_success = false;
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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())
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if (is_object($driver)) {
|
2007-11-18 23:37:44 +03:00
|
|
|
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-11-18 23:37:44 +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.');
|
|
|
|
}
|
2007-11-18 23:37:44 +03:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
$this->_driver = new $class($options);
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
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)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
// 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-09-03 18:57:18 +04:00
|
|
|
}
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-09-03 18:57:18 +04: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-10-21 10:23:59 +04: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-10-21 10:23:59 +04: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)
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if (isset($namespace)) {
|
2007-02-01 01:51:26 +03:00
|
|
|
$this->_queries[$namespace][] = $query;
|
|
|
|
} else {
|
|
|
|
$this->_queries[] = $query;
|
|
|
|
}
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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)) {
|
2007-09-03 18:57:18 +04:00
|
|
|
if ( ! isset($this->_queries[$namespace])) {
|
2007-02-01 01:51:26 +03:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_queries[$namespace];
|
|
|
|
}
|
2007-11-18 23:37:44 +03:00
|
|
|
|
2007-02-01 01:51:26 +03:00
|
|
|
return $this->_queries;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
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-10-21 10:23:59 +04: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-10-21 10:23:59 +04:00
|
|
|
|
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-09-03 18:57:18 +04: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-11-18 23:37:44 +03:00
|
|
|
|
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-11-18 23:37:44 +03:00
|
|
|
|
2007-02-09 01:06:03 +03:00
|
|
|
$i = $this->_options['size'];
|
2007-11-18 23:37:44 +03:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
2007-02-09 23:02:49 +03:00
|
|
|
/**
|
|
|
|
* readStats
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function readStats()
|
|
|
|
{
|
2007-09-03 18:57:18 +04:00
|
|
|
if ($this->_options['statsFile'] !== false) {
|
2007-11-18 23:37:44 +03:00
|
|
|
$content = file_get_contents($this->_options['statsFile']);
|
|
|
|
|
|
|
|
$e = explode("\n", $content);
|
|
|
|
|
|
|
|
return array_map('unserialize', $e);
|
2007-09-03 18:57:18 +04:00
|
|
|
}
|
|
|
|
return array();
|
2007-02-09 23:02:49 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04: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
|
|
|
{
|
2007-09-03 18:57:18 +04: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-11-18 23:37:44 +03:00
|
|
|
|
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-10-21 10:23:59 +04:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
2007-06-27 02:30:23 +04: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
|
|
|
|
*/
|
2007-06-27 02:30:23 +04:00
|
|
|
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
|
2007-11-18 23:37:44 +03:00
|
|
|
if (strtoupper(substr(ltrim($query), 0, 6)) != 'SELECT') {
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$this->add($query, $event->getInvoker()->getName());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$data = $this->_driver->fetch(md5(serialize($query)));
|
2007-02-05 15:04:56 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$this->success = ($data) ? true : false;
|
2007-02-07 19:04:50 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
if ( ! $data) {
|
|
|
|
$rand = (mt_rand() / mt_getrandmax());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
if ($rand < $this->_options['savePropability']) {
|
|
|
|
$stmt = $event->getInvoker()->getAdapter()->query($query);
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$this->success = true;
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +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
|
|
|
}
|
2007-11-18 23:37:44 +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-10-21 10:23:59 +04:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
2007-06-27 02:30:23 +04: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
|
|
|
|
*/
|
2007-06-27 02:30:23 +04:00
|
|
|
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);
|
2007-09-03 18:57:18 +04:00
|
|
|
next($this->_data);
|
2007-02-06 23:45:51 +03:00
|
|
|
return $ret;
|
2007-02-01 01:51:26 +03:00
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
2007-06-27 02:30:23 +04: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
|
|
|
|
*/
|
2007-06-27 02:30:23 +04:00
|
|
|
public function preFetchAll(Doctrine_Event $event)
|
2007-02-05 15:04:56 +03:00
|
|
|
{
|
|
|
|
return $this->_data;
|
|
|
|
}
|
2007-10-21 10:23:59 +04:00
|
|
|
|
2007-02-06 23:45:51 +03:00
|
|
|
/**
|
2007-06-27 02:30:23 +04: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
|
|
|
|
*/
|
2007-06-27 02:30:23 +04:00
|
|
|
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
|
2007-11-18 23:37:44 +03:00
|
|
|
if (strtoupper(substr(ltrim($query), 0, 6)) != 'SELECT') {
|
|
|
|
return false;
|
|
|
|
}
|
2007-02-01 01:51:26 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$this->add($query, $event->getInvoker()->getDbh()->getName());
|
2007-02-07 19:04:50 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$data = $this->_driver->fetch(md5(serialize(array($query, $event->getParams()))));
|
2007-02-05 15:04:56 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$this->success = ($data) ? true : false;
|
2007-02-07 19:04:50 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
if ( ! $data) {
|
|
|
|
$rand = (mt_rand() / mt_getrandmax());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
if ($rand <= $this->_options['savePropability']) {
|
2007-02-09 01:06:03 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$stmt = $event->getInvoker()->getStatement();
|
2007-02-09 01:06:03 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$stmt->execute($event->getParams());
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +03:00
|
|
|
$this->success = true;
|
2007-02-09 00:29:06 +03:00
|
|
|
|
2007-11-18 23:37:44 +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
|
|
|
}
|
2007-11-18 23:37:44 +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
|
|
|
}
|