From b7cac8cb4e7e22c88a10c297491a5ecc599c6623 Mon Sep 17 00:00:00 2001 From: zYne Date: Mon, 29 Jan 2007 13:13:43 +0000 Subject: [PATCH] added cache core --- lib/Doctrine/Cache.php | 93 ++++++++++++++++++++++++++++++++ lib/Doctrine/Cache/Interface.php | 2 +- lib/Doctrine/Db/Cache.php | 79 +++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 1 deletion(-) create mode 100644 lib/Doctrine/Cache.php create mode 100644 lib/Doctrine/Db/Cache.php diff --git a/lib/Doctrine/Cache.php b/lib/Doctrine/Cache.php new file mode 100644 index 000000000..cfd4dbdc7 --- /dev/null +++ b/lib/Doctrine/Cache.php @@ -0,0 +1,93 @@ +. + */ +/** + * Doctrine_Cache + * + * @package Doctrine + * @subpackage Doctrine_Cache + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Cache +{ + protected $_options = array('size' => 1000, + 'lifetime' => 3600, + ); + + protected $_queries = array(); + + protected $_driver; + /** + * process + * + * @param string $query sql query string + * @return void + */ + public function process($query) + { + $this->queries[] = $query; + } + /** + * save + * + * @return boolean + */ + public function save() + { + $content = file_get_contents($this->_statsFile); + $queries = explode("\n", $content); + + $stats = array(); + + foreach ($queries as $query) { + if (isset($stats[$query])) { + $stats[$query]++; + } else { + $stats[$query] = 1; + } + } + sort($stats); + + $i = $this->_options['size']; + + 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']); + } + } + /** + * flush + * + * adds all queries to stats file + * @return void + */ + public function flush() + { + file_put_contents($this->_statsFile, implode("\n", $this->queries)); + } +} diff --git a/lib/Doctrine/Cache/Interface.php b/lib/Doctrine/Cache/Interface.php index 4788996d5..19abaccd9 100644 --- a/lib/Doctrine/Cache/Interface.php +++ b/lib/Doctrine/Cache/Interface.php @@ -62,7 +62,7 @@ interface Doctrine_Cache_Interface * @param int $lifeTime if != false, set a specific lifetime for this cache record (null => infinite lifeTime) * @return boolean true if no problem */ - public function save($data, $id, $tags = array(), $lifeTime = false); + public function save($data, $id, $lifeTime = false); /** * Remove a cache record diff --git a/lib/Doctrine/Db/Cache.php b/lib/Doctrine/Db/Cache.php new file mode 100644 index 000000000..4929631a9 --- /dev/null +++ b/lib/Doctrine/Db/Cache.php @@ -0,0 +1,79 @@ +. + */ +/** + * Doctrine_Db_Cache + * + * @package Doctrine + * @subpackage Doctrine_Db + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Db_Cache extends Doctrine_Db_EventListener +{ + + public function __construct($cacheDriver) + { + + } + public function onPreQuery(Doctrine_Db_Event $event) + { + $query = $event->getQuery(); + + $this->cache->process($query); + } + public function onQuery(Doctrine_Db_Event $event) + { + + } + + public function onPrePrepare(Doctrine_Db_Event $event) + { + + } + public function onPrepare(Doctrine_Db_Event $event) + { + + } + + public function onPreExec(Doctrine_Db_Event $event) + { + + } + public function onExec(Doctrine_Db_Event $event) + { + + } + + public function onPreExecute(Doctrine_Db_Event $event) + { + $query = $event->getQuery(); + + $this->cache->process($query); + } + public function onExecute(Doctrine_Db_Event $event) + { + + } +}