Skip to content

Commit

Permalink
✨ key tranformer
Browse files Browse the repository at this point in the history
🐛 strstr empty needle
  • Loading branch information
bnomei committed Oct 10, 2021
1 parent 03c7146 commit cbb5cfe
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 22 deletions.
11 changes: 9 additions & 2 deletions classes/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
94 changes: 79 additions & 15 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
9 changes: 9 additions & 0 deletions tests/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
3 changes: 3 additions & 0 deletions tests/site/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
];
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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(),
Expand Down

0 comments on commit cbb5cfe

Please sign in to comment.