Refactoring Cache key generation, fixing SQLite cache, and invalidating cache on database reconnect

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7455 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-08-12 21:08:51 +00:00
parent 995040a98a
commit 1fa9939965
5 changed files with 57 additions and 26 deletions

View file

@ -252,7 +252,7 @@ class Cache extends Object {
return false;
}
if (!$key = $_this->__key($key)) {
if (!$key = $_this->_Engine[$engine]->key($key)) {
return false;
}
@ -300,7 +300,7 @@ class Cache extends Object {
if (!$_this->isInitialized($engine)) {
return false;
}
if (!$key = $_this->__key($key)) {
if (!$key = $_this->_Engine[$engine]->key($key)) {
return false;
}
@ -332,7 +332,7 @@ class Cache extends Object {
return false;
}
if (!$key = $_this->__key($key)) {
if (!$key = $_this->_Engine[$engine]->key($key)) {
return false;
}
@ -405,20 +405,6 @@ class Cache extends Object {
}
return array();
}
/**
* generates a safe key
*
* @param string $key the key passed over
* @return mixed string $key or false
* @access private
*/
function __key($key) {
if (empty($key)) {
return false;
}
$key = str_replace(array(DS, '/', '.'), '_', strval($key));
return $key;
}
}
/**
* Storage engine for CakePHP caching
@ -508,5 +494,20 @@ class CacheEngine extends Object {
function settings() {
return $this->settings;
}
/**
* generates a safe key
*
* @param string $key the key passed over
* @return mixed string $key or false
* @access public
*/
function key($key) {
if (empty($key)) {
return false;
}
$key = Inflector::slug(str_replace(array(DS, '/', '.'), '_', strval($key)));
return $key;
}
}
?>

View file

@ -198,7 +198,8 @@ class DataSource extends Object {
return $this->_sources;
}
$key = ConnectionManager::getSourceName($this) . '_' . Inflector::slug($this->config['database']) . '_list';
$key = ConnectionManager::getSourceName($this) . '_' . $this->config['database'] . '_list';
$key = preg_replace('/[^A-Za-z0-9_\-\.+]/', '_', $key);
$sources = Cache::read($key, '_cake_model_');
if (empty($sources)) {

View file

@ -159,17 +159,14 @@ class DboSqlite extends DboSource {
* @return array Array of tablenames in the database
*/
function listSources() {
$db = $this->config['database'];
$this->config['database'] = basename($this->config['database']);
$cache = parent::listSources();
if ($cache != null) {
return $cache;
}
$result = $this->fetchAll("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;", false);
if (!$result || empty($result)) {
if (empty($result)) {
return array();
} else {
$tables = array();
@ -177,11 +174,8 @@ class DboSqlite extends DboSource {
$tables[] = $table[0]['name'];
}
parent::listSources($tables);
$this->config['database'] = $db;
return $tables;
}
$this->config['database'] = $db;
return array();
}
/**

View file

@ -121,6 +121,8 @@ class DboSource extends DataSource {
function reconnect($config = null) {
$this->disconnect();
$this->setConfig($config);
$this->_sources = null;
return $this->connect();
}
/**

View file

@ -156,6 +156,39 @@ class DboSqliteTest extends CakeTestCase {
$this->db->query('DROP TABLE foo_test;');
$this->assertFalse(in_array('foo_test', $this->db->listSources()));
}
/**
* Tests that cached table descriptions are saved under the sanitized key name
*
* @access public
*/
function testCacheKeyName() {
Configure::write('Cache.disable', false);
$dbName = 'db' . rand() . '$(*%&).db';
$this->assertFalse(file_exists(TMP . $dbName));
$config = $this->db->config;
$db = new DboSqlite(array_merge($this->db->config, array('database' => TMP . $dbName)));
$this->assertTrue(file_exists(TMP . $dbName));
$db->execute("CREATE TABLE test_list (id VARCHAR(255));");
$db->cacheSources = true;
$this->assertEqual($db->listSources(), array('test_list'));
$db->cacheSources = false;
$fileName = '_' . preg_replace('/[^A-Za-z0-9_\-+]/', '_', TMP . $dbName) . '_list';
while (strpos($fileName, '__') !== false) {
$fileName = str_replace('__', '_', $fileName);
}
$result = Cache::read($fileName, '_cake_model_');
$this->assertEqual($result, array('test_list'));
Cache::delete($fileName, '_cake_model_');
Configure::write('Cache.disable', true);
}
}
?>