From ec6f806efec75da41eed50eaf508efd160d2f071 Mon Sep 17 00:00:00 2001 From: jepso Date: Fri, 17 Aug 2007 10:49:51 +0000 Subject: [PATCH] Doctrine_Cache_Db: * added tableName option * added deleteAll and createTable methods * fixed fetch method * fixed a bug with expire time in save method --- lib/Doctrine/Cache/Db.php | 88 +++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/Cache/Db.php b/lib/Doctrine/Cache/Db.php index 362838155..7b98c7692 100644 --- a/lib/Doctrine/Cache/Db.php +++ b/lib/Doctrine/Cache/Db.php @@ -20,7 +20,7 @@ */ /** - * Doctrine_Cache_Sqlite + * Doctrine_Cache_Db * * @package Doctrine * @subpackage Doctrine_Cache @@ -44,7 +44,14 @@ class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable ! ($options['connection'] instanceof Doctrine_Connection)) { throw new Doctrine_Cache_Exception('Connection option not set.'); - } + } + + if ( ! isset($options['tableName']) || + ! is_string($options['tableName'])) { + + throw new Doctrine_Cache_Exception('Table name option not set.'); + } + $this->_options = $options; } @@ -69,15 +76,20 @@ class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable */ public function fetch($id, $testCacheValidity = true) { - $sql = 'SELECT data, expires FROM cache WHERE id = ?'; + $sql = 'SELECT data, expire FROM ' . $this->_options['tableName'] + . ' WHERE id = ?'; if ($testCacheValidity) { $sql .= ' AND (expire=0 OR expire > ' . time() . ')'; } $result = $this->getConnection()->fetchAssoc($sql, array($id)); - - return unserialize($result['data']); + + if ( ! isset($result[0])) { + return false; + } + + return unserialize($result[0]['data']); } /** * Test if a cache is available or not (for the given id) @@ -87,7 +99,8 @@ class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable */ public function contains($id) { - $sql = 'SELECT expires FROM cache WHERE id = ? AND (expire=0 OR expire > ' . time() . ')'; + $sql = 'SELECT expire FROM ' . $this->_options['tableName'] + . ' WHERE id = ? AND (expire=0 OR expire > ' . time() . ')'; return $this->getConnection()->fetchOne($sql, array($id)); } @@ -103,9 +116,16 @@ class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable */ public function save($data, $id, $lifeTime = false) { - $sql = 'INSERT INTO cache (id, data, expires) VALUES (?, ?, ?)'; - - $params = array($id, serialize($data), (time() + $lifeTime)); + $sql = 'INSERT INTO ' . $this->_options['tableName'] + . ' (id, data, expire) VALUES (?, ?, ?)'; + + if ($lifeTime) { + $expire = time() + $lifeTime; + } else { + $expire = 0; + } + + $params = array($id, serialize($data), $expire); return (bool) $this->getConnection()->exec($sql, $params); } @@ -117,10 +137,23 @@ class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable */ public function delete($id) { - $sql = 'DELETE FROM cache WHERE id = ?'; + $sql = 'DELETE FROM ' . $this->_options['tableName'] . ' WHERE id = ?'; return (bool) $this->getConnection()->exec($sql, array($id)); - } + } + + /** + * Removes all cache records + * + * $return bool true on success, false on failure + */ + public function deleteAll() + { + $sql = 'DELETE FROM ' . $this->_options['tableName']; + + return (bool) $this->getConnection()->exec($sql); + } + /** * count * returns the number of cached elements @@ -128,7 +161,36 @@ class Doctrine_Cache_Db extends Doctrine_Cache_Driver implements Countable * @return integer */ public function count() - { - return (int) $this->getConnection()->fetchOne('SELECT COUNT(*) FROM cache'); + { + $sql = 'SELECT COUNT(*) FROM ' . $this->_options['tableName']; + + return (int) $this->getConnection()->fetchOne($sql); + } + + /** + * Creates the cache table. + */ + public function createTable() + { + $name = $this->_options['tableName']; + + $fields = array( + 'id' => array( + 'type' => 'string', + 'length' => 255 + ), + 'data' => array( + 'type' => 'blob' + ), + 'expire' => array( + 'type' => 'timestamp' + ) + ); + + $options = array( + 'primary' => array('id') + ); + + $this->getConnection()->export->createTable($name, $fields, $options); } }