1
0
mirror of synced 2024-12-14 23:26:04 +03:00
This commit is contained in:
zYne 2007-02-08 21:29:06 +00:00
parent 4282758023
commit 35a760aadc

View File

@ -38,8 +38,8 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
protected $_options = array('size' => 1000, protected $_options = array('size' => 1000,
'lifeTime' => 3600, 'lifeTime' => 3600,
'statsPropability' => 0.75, 'statsPropability' => 0.75,
'savePropability' => 0.80, 'savePropability' => 0.10,
'cleanPropability' => 0.98, 'cleanPropability' => 0.01,
'statsFile' => '../data/stats.cache', 'statsFile' => '../data/stats.cache',
); );
/** /**
@ -270,6 +270,7 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
{ {
$query = $event->getQuery(); $query = $event->getQuery();
$data = false;
// only process SELECT statements // only process SELECT statements
if (substr(trim(strtoupper($query)), 0, 6) == 'SELECT') { if (substr(trim(strtoupper($query)), 0, 6) == 'SELECT') {
@ -279,12 +280,22 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
$this->success = ($data) ? true : false; $this->success = ($data) ? true : false;
$this->_data = $data; if ( ! $data) {
$rand = (rand(1, 10000) / (10000 * 100));
return true; if ($rand < $this->_options['savePropability']) {
$stmt = $event->getInvoker()->query($query);
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
$this->success = true;
$this->_driver->save(md5($query), $data);
} }
}
return false; $this->_data = $data;
}
return (bool) $data;
} }
/** /**
* onPreFetch * onPreFetch
@ -326,6 +337,8 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
{ {
$query = $event->getQuery(); $query = $event->getQuery();
$data = false;
// only process SELECT statements // only process SELECT statements
if (substr(trim(strtoupper($query)), 0, 6) == 'SELECT') { if (substr(trim(strtoupper($query)), 0, 6) == 'SELECT') {
@ -335,28 +348,31 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
$this->success = ($data) ? true : false; $this->success = ($data) ? true : false;
if ( ! $data) {
$rand = (rand(1, 10000) / (10000 * 100));
if ($rand < $this->_options['savePropability']) {
$stmt = $event->getInvoker()->execute($event->getParams());
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
$this->success = true;
$this->_driver->save(md5(serialize(array($query, $event->getParams()))), $data);
}
}
$this->_data = $data; $this->_data = $data;
return true;
} }
return (bool) $data;
return false;
} }
/** /**
* onExecute * processStats
* listens the onExecute event of Doctrine_Db_Statement
*
* adds the issued query to internal query stack
* and checks if cached element exists
*
* @return boolean
*/
/**
* destructor
* *
* @return void * @return void
*/ */
public function process() public function processStats()
{ {
$rand = (rand(1, 10000) / (10000 * 100)); $rand = (rand(1, 10000) / (10000 * 100));