From cbb5cfef5dbae0c87f847dd6894b37bc03240837 Mon Sep 17 00:00:00 2001 From: Bruno Meilick <b@bnomei.com> Date: Sun, 10 Oct 2021 13:41:35 +0100 Subject: [PATCH] :sparkles: key tranformer :bug: strstr empty needle --- classes/Redis.php | 11 +++- composer.json | 2 +- composer.lock | 94 +++++++++++++++++++++++++++++------ index.php | 1 + tests/RedisTest.php | 9 ++++ tests/site/config/config.php | 3 ++ vendor/composer/installed.php | 8 +-- 7 files changed, 106 insertions(+), 22 deletions(-) diff --git a/classes/Redis.php b/classes/Redis.php index 3da979c..2265c0b 100644 --- a/classes/Redis.php +++ b/classes/Redis.php @@ -37,6 +37,7 @@ public function __construct(array $options = [], array $optionsClient = []) 'store' => \option('bnomei.redis-cachedriver.store'), 'store-ignore' => \option('bnomei.redis-cachedriver.store-ignore'), 'preload' => \option('bnomei.redis-cachedriver.preload'), + 'key' => \option('bnomei.redis-cachedriver.key'), 'host' => \option('bnomei.redis-cachedriver.host'), 'port' => \option('bnomei.redis-cachedriver.port'), ], $options); @@ -166,7 +167,7 @@ public function set(string $key, $value, int $minutes = 0): bool $key = $this->key($key); $value = (new Value($value, $minutes))->toJson(); - if ($this->option('store') && strstr($key, $this->option('store-ignore')) === false) { + if ($this->option('store') && str_contains($key, $this->option('store-ignore')) === false) { $this->store[$key] = $value; } $this->preload[$key] = time(); @@ -206,7 +207,7 @@ public function retrieve(string $key) $value = is_string($value) ? Value::fromJson($value) : null; - if ($this->option('store') && strstr($key, $this->option('store-ignore')) === false) { + if ($this->option('store') && str_contains($key, $this->option('store-ignore')) === false) { $this->store[$key] = $value; } @@ -244,6 +245,12 @@ public function remove(string $key): bool return false; } + public function key(string $key): string + { + $key = parent::key($key); + return $this->option('key')($key); + } + /** * @inheritDoc */ diff --git a/composer.json b/composer.json index c593586..e02d3cc 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "bnomei/kirby3-redis-cachedriver", "type": "kirby-plugin", - "version": "1.5.7", + "version": "1.6.0", "description": "Redis based Cache-Driver", "license": "MIT", "authors": [ diff --git a/composer.lock b/composer.lock index f86f188..01f8168 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "3a0b6315be3a71fb25e4a48a41344af4", + "content-hash": "18052c94f1802443d8214b8a6fee2c85", "packages": [ { "name": "getkirby/composer-installer", @@ -487,16 +487,16 @@ }, { "name": "guzzlehttp/promises", - "version": "1.4.1", + "version": "1.5.0", "source": { "type": "git", "url": "https://github.com/guzzle/promises.git", - "reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d" + "reference": "136a635e2b4a49b9d79e9c8fee267ffb257fdba0" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/promises/zipball/8e7d04f1f6450fef59366c399cfad4b9383aa30d", - "reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d", + "url": "https://api.github.com/repos/guzzle/promises/zipball/136a635e2b4a49b9d79e9c8fee267ffb257fdba0", + "reference": "136a635e2b4a49b9d79e9c8fee267ffb257fdba0", "shasum": "" }, "require": { @@ -508,7 +508,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.4-dev" + "dev-master": "1.5-dev" } }, "autoload": { @@ -524,10 +524,25 @@ "MIT" ], "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", + "homepage": "https://github.com/Tobion" } ], "description": "Guzzle promises library", @@ -536,22 +551,36 @@ ], "support": { "issues": "https://github.com/guzzle/promises/issues", - "source": "https://github.com/guzzle/promises/tree/1.4.1" + "source": "https://github.com/guzzle/promises/tree/1.5.0" }, - "time": "2021-03-07T09:25:29+00:00" + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/promises", + "type": "tidelift" + } + ], + "time": "2021-10-07T13:05:22+00:00" }, { "name": "guzzlehttp/psr7", - "version": "2.0.0", + "version": "2.1.0", "source": { "type": "git", "url": "https://github.com/guzzle/psr7.git", - "reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7" + "reference": "089edd38f5b8abba6cb01567c2a8aaa47cec4c72" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/psr7/zipball/1dc8d9cba3897165e16d12bb13d813afb1eb3fe7", - "reference": "1dc8d9cba3897165e16d12bb13d813afb1eb3fe7", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/089edd38f5b8abba6cb01567c2a8aaa47cec4c72", + "reference": "089edd38f5b8abba6cb01567c2a8aaa47cec4c72", "shasum": "" }, "require": { @@ -575,7 +604,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.0-dev" + "dev-master": "2.1-dev" } }, "autoload": { @@ -588,13 +617,34 @@ "MIT" ], "authors": [ + { + "name": "Graham Campbell", + "email": "hello@gjcampbell.co.uk", + "homepage": "https://github.com/GrahamCampbell" + }, { "name": "Michael Dowling", "email": "mtdowling@gmail.com", "homepage": "https://github.com/mtdowling" }, + { + "name": "George Mponos", + "email": "gmponos@gmail.com", + "homepage": "https://github.com/gmponos" + }, + { + "name": "Tobias Nyholm", + "email": "tobias.nyholm@gmail.com", + "homepage": "https://github.com/Nyholm" + }, + { + "name": "Márk Sági-Kazár", + "email": "mark.sagikazar@gmail.com", + "homepage": "https://github.com/sagikazarmark" + }, { "name": "Tobias Schultze", + "email": "webmaster@tubo-world.de", "homepage": "https://github.com/Tobion" }, { @@ -616,9 +666,23 @@ ], "support": { "issues": "https://github.com/guzzle/psr7/issues", - "source": "https://github.com/guzzle/psr7/tree/2.0.0" + "source": "https://github.com/guzzle/psr7/tree/2.1.0" }, - "time": "2021-06-30T20:03:07+00:00" + "funding": [ + { + "url": "https://github.com/GrahamCampbell", + "type": "github" + }, + { + "url": "https://github.com/Nyholm", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/guzzlehttp/psr7", + "type": "tidelift" + } + ], + "time": "2021-10-06T17:43:30+00:00" }, { "name": "laminas/laminas-escaper", diff --git a/index.php b/index.php index 08cd9b7..2a5f0ee 100644 --- a/index.php +++ b/index.php @@ -13,6 +13,7 @@ 'store' => true, // php memory cache 'store-ignore' => '', // if key contains then ignore 'preload' => true, // or minutes + 'key' => function($key) { return $key; }, // redis 'host' => '127.0.0.1', diff --git a/tests/RedisTest.php b/tests/RedisTest.php index c47407b..c4e0b2d 100644 --- a/tests/RedisTest.php +++ b/tests/RedisTest.php @@ -130,4 +130,13 @@ public function testBenchmark() unset($this->redis); // will happen at end of pageview $this->assertTrue(true); } + + public function testReplaceInKey() + { + // see site/config/config.php + $this->redis->remove('HELLO'); + $this->redis->remove('WORLD'); + $this->redis->set('HELLO', 'world'); + $this->assertEquals('world', $this->redis->get('WORLD')); + } } diff --git a/tests/site/config/config.php b/tests/site/config/config.php index a5e71cb..c2eb4d8 100644 --- a/tests/site/config/config.php +++ b/tests/site/config/config.php @@ -2,4 +2,7 @@ return [ 'debug' => false, 'bnomei.redis-cachedriver.preload' => false, // no pipeline on my test localhost + 'bnomei.redis-cachedriver.key' => function($key) { + return str_replace('HELLO', 'WORLD', $key); + }, ]; diff --git a/vendor/composer/installed.php b/vendor/composer/installed.php index cf8382b..f8793ca 100644 --- a/vendor/composer/installed.php +++ b/vendor/composer/installed.php @@ -1,7 +1,7 @@ <?php return array( 'root' => array( - 'pretty_version' => '1.5.7', - 'version' => '1.5.7.0', + 'pretty_version' => '1.6.0', + 'version' => '1.6.0.0', 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(), @@ -11,8 +11,8 @@ ), 'versions' => array( 'bnomei/kirby3-redis-cachedriver' => array( - 'pretty_version' => '1.5.7', - 'version' => '1.5.7.0', + 'pretty_version' => '1.6.0', + 'version' => '1.6.0.0', 'type' => 'kirby-plugin', 'install_path' => __DIR__ . '/../../', 'aliases' => array(),