From cb3eccd3d166331f7edfb30b745a967b12f4f43d Mon Sep 17 00:00:00 2001 From: iturgeon Date: Wed, 8 Jul 2020 21:02:17 -0400 Subject: [PATCH] [fix] memcached cache delete_all() The problem in the memcached cache driver prevents \Cache::delete_all() from removingall the keys in the cache index. The change allows that branch of the code to execute and includes tests for the different states of cache_id and deleting with and without a section in the arguments. Signed-off-by: iturgeon --- classes/cache/storage/memcached.php | 14 ++--- tests/cache/storage/memcached.php | 87 ++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 9 deletions(-) diff --git a/classes/cache/storage/memcached.php b/classes/cache/storage/memcached.php index c2b8856cb2..a8d65b4f2e 100644 --- a/classes/cache/storage/memcached.php +++ b/classes/cache/storage/memcached.php @@ -145,22 +145,20 @@ public function delete() */ public function delete_all($section) { - // determine the section index name - $section = $this->config['cache_id'].(empty($section) ? '' : '.'.$section); - - // get the directory index $index = static::$memcached->get($this->config['cache_id'].'__DIR__'); if (is_array($index)) { - // limit the delete if we have a valid section - if ( ! empty($section)) + if (empty($section)) { - $dirs = in_array($section, $index) ? array($section) : array(); + // delete everything in the index + $dirs = $index; } else { - $dirs = $index; + // delete just the section + $section_key = $this->config['cache_id'].(empty($section) ? '' : '.'.$section); + $dirs = in_array($section_key, $index) ? array($section_key) : array(); } // loop through the indexes, delete all stored keys, then delete the indexes diff --git a/tests/cache/storage/memcached.php b/tests/cache/storage/memcached.php index 1960a469b8..8426cc36c2 100644 --- a/tests/cache/storage/memcached.php +++ b/tests/cache/storage/memcached.php @@ -20,5 +20,90 @@ */ class Test_Cache_Storage_Memcached extends TestCase { - public function test_foo() {} + public function test_foo() {} + + public function test_delete_all() + { + \Config::set('cache.memcached.cache_id', ''); + + \Cache::set('section_name.item', 'value'); + $a = \Cache::get('section_name.item'); // 'value' + + + \Cache::delete_all(); // expected to remove everything + + $b = 'unchanged'; + try{ + $b = \Cache::get('section_name.item'); + } catch(\CacheNotFoundException $e){ + $b = 'error'; + } + + $this->assertEquals($a, 'value'); + $this->assertEquals($b, 'error'); + } + + public function test_delete_all_with_cache_id() + { + \Config::set('cache.memcached.cache_id', 'test'); + + \Cache::set('section_name.item', 'value'); + $a = \Cache::get('section_name.item'); // 'value' + + + \Cache::delete_all(); // expected to remove everything + + $b = 'unchanged'; + try{ + $b = \Cache::get('section_name.item'); + } catch(\CacheNotFoundException $e){ + $b = 'error'; + } + + $this->assertEquals($a, 'value'); + $this->assertEquals($b, 'error'); + } + + public function test_delete_all_with_section() + { + \Config::set('cache.memcached.cache_id', ''); + + \Cache::set('section_name.item', 'value'); + $a = \Cache::get('section_name.item'); // 'value' + + + \Cache::delete_all('section_name'); + + $b = 'unchanged'; + try{ + $b = \Cache::get('section_name.item'); + } catch(\CacheNotFoundException $e){ + $b = 'error'; + } + + $this->assertEquals($a, 'value'); + $this->assertEquals($b, 'error'); + } + + public function test_delete_all_with_section_with_cache_id() + { + + \Config::set('cache.memcached.cache_id', 'test'); + + \Cache::set('section_name.item', 'value'); + $a = \Cache::get('section_name.item'); // 'value' + + + \Cache::delete_all('section_name'); + + $b = 'unchanged'; + try{ + $b = \Cache::get('section_name.item'); + } catch(\CacheNotFoundException $e){ + $b = 'error'; + } + + $this->assertEquals($a, 'value'); + $this->assertEquals($b, 'error'); + } }