From a9a9a824e2dfcb12f3aeb990de3cbef3dc71a79c Mon Sep 17 00:00:00 2001 From: gwoo Date: Mon, 22 Oct 2007 20:39:36 +0000 Subject: [PATCH] update Cache to specify key paths with dot syntax, like Cache::write('my.key.is.here', $data); git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5866 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/cache.php | 26 +++++++++++++++++------ cake/tests/cases/libs/cache/file.test.php | 15 +++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/cake/libs/cache.php b/cake/libs/cache.php index c1b607a9c..414a9d7b8 100644 --- a/cake/libs/cache.php +++ b/cake/libs/cache.php @@ -191,10 +191,10 @@ class Cache extends Object { return false; } - $key = strval($key); - if (empty($key)) { + if (!$key = $_this->__key($key)) { return false; } + if (is_resource($value)) { return false; } @@ -228,8 +228,7 @@ class Cache extends Object { if (!$_this->isInitialized($engine)) { return false; } - $key = strval($key); - if (empty($key)) { + if (!$key = $_this->__key($key)) { return false; } $success = $_this->_Engine[$engine]->read($key); @@ -253,10 +252,11 @@ class Cache extends Object { if (!$_this->isInitialized($engine)) { return false; } - $key = strval($key); - if (empty($key)) { + + if (!$key = $_this->__key($key)) { return false; } + $success = $_this->_Engine[$engine]->delete($key); $_this->_Engine[$engine]->init($settings); return $success; @@ -317,6 +317,20 @@ 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 = r(array(DS, '/', '.'), '_', strval($key)); + return $key; + } } /** * Storage engine for CakePHP caching diff --git a/cake/tests/cases/libs/cache/file.test.php b/cake/tests/cases/libs/cache/file.test.php index 9e0f546cb..ff72d9b1f 100644 --- a/cake/tests/cases/libs/cache/file.test.php +++ b/cake/tests/cases/libs/cache/file.test.php @@ -50,6 +50,9 @@ class FileEngineTest extends UnitTestCase { } function testReadAndWriteCache() { + $result = Cache::write(null, 'here'); + $this->assertFalse($result); + $result = Cache::read('test'); $expecting = ''; $this->assertEqual($result, $expecting); @@ -151,6 +154,18 @@ class FileEngineTest extends UnitTestCase { } + function testKeyPath() { + $result = Cache::write('views.countries.something', 'here'); + $this->assertTrue($result); + $this->assertTrue(file_exists(CACHE . 'cake_views_countries_something')); + + $result = Cache::read('views.countries.something'); + $this->assertEqual($result, 'here'); + + $result = Cache::clear(); + $this->assertTrue($result); + } + function tearDown() { Cache::config('default'); }