1
0
mirror of synced 2024-12-14 15:16: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',
); );
/** /**
@ -269,22 +269,33 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
public function onPreQuery(Doctrine_Db_Event $event) public function onPreQuery(Doctrine_Db_Event $event)
{ {
$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') {
$this->add($query, $event->getInvoker()->getName()); $this->add($query, $event->getInvoker()->getName());
$data = $this->_driver->fetch(md5($query)); $data = $this->_driver->fetch(md5($query));
$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()->query($query);
$data = $stmt->fetchAll(Doctrine::FETCH_ASSOC);
$this->success = true;
$this->_driver->save(md5($query), $data);
}
}
$this->_data = $data; $this->_data = $data;
return true;
} }
return (bool) $data;
return false;
} }
/** /**
* onPreFetch * onPreFetch
@ -325,7 +336,9 @@ class Doctrine_Cache extends Doctrine_Db_EventListener implements Countable, Ite
public function onPreExecute(Doctrine_Db_Event $event) public function onPreExecute(Doctrine_Db_Event $event)
{ {
$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));