Commit 6f4c3c20 authored by zYne's avatar zYne

--no commit message

--no commit message
parent 880422ca
......@@ -37,7 +37,7 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
*/
protected $_options = array('size' => 1000,
'lifeTime' => 3600,
'statsPropability' => 0.75,
'addStatsPropability' => 0.25,
'savePropability' => 0.10,
'cleanPropability' => 0.01,
'statsFile' => '../data/stats.cache',
......@@ -211,8 +211,11 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
*
* @return boolean
*/
public function processAll()
public function clean()
{
$rand = (mt_rand() / mt_getrandmax());
if ($rand <= $this->_options['cleanPropability']) {
$content = file_get_contents($this->_statsFile);
$queries = explode("\n", $content);
......@@ -235,18 +238,24 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
while ($i--) {
$element = next($stats);
$query = key($stats);
$conn = Doctrine_Manager::getConnection($element[1]);
$data = $conn->fetchAll($query);
$this->_driver->save(serialize($data), $query, $this->_options['lifetime']);
if (is_array($query)) {
$hash = md5(serialize($query));
} else {
$hash = md5($query);
}
$this->_driver->delete($hash);
}
}
}
/**
* flush
* appendStats
*
* adds all queries to stats file
* @return void
*/
public function flush()
public function appendStats()
{
if ($this->_options['statsFile'] !== false) {
......@@ -254,9 +263,13 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
throw new Doctrine_Cache_Exception("Couldn't save cache statistics. Cache statistics file doesn't exists!");
}
$rand = (mt_rand() / mt_getrandmax());
if ($rand <= $this->_options['addStatsPropability']) {
file_put_contents($this->_options['statsFile'], implode("\n", $this->_queries));
}
}
}
/**
* onPreQuery
* listens the onPreQuery event of Doctrine_Db
......@@ -281,10 +294,10 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
$this->success = ($data) ? true : false;
if ( ! $data) {
$rand = (rand(1, 10000) / (10000 * 100));
$rand = (mt_rand() / mt_getrandmax());
if ($rand < $this->_options['savePropability']) {
$stmt = $event->getInvoker()->query($query);
$stmt = $event->getInvoker()->getAdapter()->query($query);
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
......@@ -349,10 +362,13 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
$this->success = ($data) ? true : false;
if ( ! $data) {
$rand = (rand(1, 10000) / (10000 * 100));
$rand = (mt_rand() / mt_getrandmax());
if ($rand < $this->_options['savePropability']) {
$stmt = $event->getInvoker()->execute($event->getParams());
if ($rand <= $this->_options['savePropability']) {
$stmt = $event->getInvoker()->getStatement();
$stmt->execute($event->getParams());
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
......@@ -367,17 +383,4 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
}
return (bool) $data;
}
/**
* processStats
*
* @return void
*/
public function processStats()
{
$rand = (rand(1, 10000) / (10000 * 100));
if($rand > $this->_options['statSlamDefense']) {
$this->flush();
}
}
}
......@@ -31,7 +31,7 @@
* @version $Revision$
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
*/
class Doctrine_Cache_Array
class Doctrine_Cache_Array implements Countable
{
/**
* @var array $data an array of cached data
......@@ -88,4 +88,22 @@ class Doctrine_Cache_Array
{
unset($this->data[$id]);
}
/**
* Remove all cache record
*
* @return boolean true if no problem
*/
public function deleteAll()
{
$this->data = array();
}
/**
* count
*
* @return integer
*/
public function count()
{
return count($this->data);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment