Skip to content

Commit

Permalink
Merge pull request #809 from demiankatz/more-cache-improvements
Browse files Browse the repository at this point in the history
Add support for # in cache key; add test.
  • Loading branch information
TomHAnderson authored Jun 9, 2023
2 parents 07b4e0f + 9eea9a7 commit b7b08fd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ public function getValidatorConfig(): array
*/
public function getCachesConfig(): array
{
$defaultOptions = [
'namespace' => 'DoctrineModule',
'key_pattern' => '/^[a-z0-9_\+\-\[\]\\\\$]*$/Di',
];
$defaultOptions = ['namespace' => 'DoctrineModule'];

return [
'doctrinemodule.cache.apcu' => [
Expand All @@ -133,7 +130,11 @@ public function getCachesConfig(): array
],
'doctrinemodule.cache.filesystem' => [
'adapter' => 'filesystem',
'options' => $defaultOptions + ['cache_dir' => 'data/DoctrineModule/cache'],
'options' => $defaultOptions + [
'cache_dir' => 'data/DoctrineModule/cache',
// We need to be slightly less restrictive than Filesystem defaults:
'key_pattern' => '/^[a-z0-9_\+\-\[\]\\\\$#]*$/Di',
],
'plugins' => [['name' => 'serializer']],
],
'doctrinemodule.cache.memcached' => [
Expand Down
10 changes: 10 additions & 0 deletions tests/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace DoctrineModuleTest;

use DoctrineModule\ConfigProvider;
use Laminas\Cache\Storage\Adapter\Filesystem;
use PHPUnit\Framework\TestCase;

use function serialize;
Expand Down Expand Up @@ -35,4 +36,13 @@ public function testInvokeHasCorrectKeys(): void

self::assertSame($config, unserialize(serialize($config)));
}

public function testDoctrineCompatibleCacheKeyConfiguration(): void
{
$config = (new ConfigProvider())->getCachesConfig()['doctrinemodule.cache.filesystem'];
$adapter = new Filesystem($config['options']);
$key = 'MyTestKey[something\inside\here#with$specialChars]';
$adapter->setItem($key, 'foo');
$this->assertEquals('foo', $adapter->getItem($key));
}
}

0 comments on commit b7b08fd

Please sign in to comment.