Skip to content

Commit da1646a

Browse files
authored
Feat: Add support for cache key specifier (#5)
1 parent e2d8da4 commit da1646a

File tree

7 files changed

+50
-2
lines changed

7 files changed

+50
-2
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@ cache for your application.
189189
If the `default_memoize_seconds` is set to `-1` this config is ignored and a default in-memory implementation
190190
is used (service `rikudou.memoize.internal_cache`, class [`InMemoryCachePool`](src/Cache/InMemoryCachePool.php)).
191191

192+
### key_specifier_service
193+
194+
This parameter allows you to specify a service that will alter the cache key. This is useful if your app relies on
195+
some kind of global state (like currently authenticated user etc.). It must implement the
196+
[CacheKeySpecifier](src/Cache/KeySpecifier/CacheKeySpecifier.php) interface.
197+
192198
### Default configuration
193199

194200
Generated by `php bin/console config:dump rikudou_memoize`
@@ -205,6 +211,9 @@ rikudou_memoize:
205211

206212
# The default cache service to use. If default_memoize_seconds is set to -1 this setting is ignored and internal service is used.
207213
cache_service: cache.app
214+
215+
# The service to use to alter the cache key. Useful if you need to alter the cache key based on some global state.
216+
key_specifier_service: rikudou.memoize.key_specifier.null
208217
```
209218
210219
## Example proxy class
@@ -258,18 +267,20 @@ class Calculator implements CalculatorInterface
258267

259268
namespace App\Memoized;
260269

261-
final class Calculator_Proxy_97bcf9ddeba8fffab6ecd550cf2ea6d6 implements \App\Service\CalculatorInterface
270+
final class Calculator_Proxy_422705a42881a5490e7996fe93245734 implements \App\Service\CalculatorInterface
262271
{
263272
public function __construct(
264273
private readonly \App\Service\Calculator $original,
265274
private readonly \Psr\Cache\CacheItemPoolInterface $cache,
266275
private readonly \Rikudou\MemoizeBundle\Cache\InMemoryCachePool $internalCache,
276+
private readonly \Rikudou\MemoizeBundle\Cache\KeySpecifier\CacheKeySpecifier $cacheKeySpecifier,
267277
) {}
268278

269279
public function add(int $a, int $b): int {
270280
$cacheKey = '';
271281
$cacheKey .= serialize($a);
272282
$cacheKey .= serialize($b);
283+
$cacheKey .= $this->cacheKeySpecifier->generate();
273284
$cacheKey = hash('sha512', $cacheKey);
274285
$cacheKey = "rikudou_memoize_AppServiceCalculator_add_{$cacheKey}";
275286

@@ -288,6 +299,7 @@ final class Calculator_Proxy_97bcf9ddeba8fffab6ecd550cf2ea6d6 implements \App\Se
288299
$cacheKey = '';
289300
$cacheKey .= serialize($a);
290301
$cacheKey .= serialize($b);
302+
$cacheKey .= $this->cacheKeySpecifier->generate();
291303
$cacheKey = hash('sha512', $cacheKey);
292304
$cacheKey = "rikudou_memoize_AppServiceCalculator_sub_{$cacheKey}";
293305

@@ -308,6 +320,7 @@ final class Calculator_Proxy_97bcf9ddeba8fffab6ecd550cf2ea6d6 implements \App\Se
308320

309321
public function someVoidMethod(): void {
310322
$cacheKey = '';
323+
$cacheKey .= $this->cacheKeySpecifier->generate();
311324
$cacheKey = hash('sha512', $cacheKey);
312325
$cacheKey = "rikudou_memoize_AppServiceCalculator_someVoidMethod_{$cacheKey}";
313326

@@ -326,4 +339,4 @@ final class Calculator_Proxy_97bcf9ddeba8fffab6ecd550cf2ea6d6 implements \App\Se
326339
}
327340

328341
}
329-
```
342+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Rikudou\MemoizeBundle\Cache\KeySpecifier;
4+
5+
interface CacheKeySpecifier
6+
{
7+
public function generate(): string;
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rikudou\MemoizeBundle\Cache\KeySpecifier;
4+
5+
use Rikudou\MemoizeBundle\Cache\KeySpecifier\CacheKeySpecifier;
6+
7+
final class NullCacheKeySpecifier implements CacheKeySpecifier
8+
{
9+
public function generate(): string
10+
{
11+
return '';
12+
}
13+
}

src/DependencyInjection/Compiler/MemoizeProxyCreatorCompilerPass.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Rikudou\MemoizeBundle\Attribute\Memoize;
1818
use Rikudou\MemoizeBundle\Attribute\NoMemoize;
1919
use Rikudou\MemoizeBundle\Cache\InMemoryCachePool;
20+
use Rikudou\MemoizeBundle\Cache\KeySpecifier\CacheKeySpecifier;
2021
use RuntimeException;
2122
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2223
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -43,6 +44,8 @@ public function process(ContainerBuilder $container): void
4344

4445
$cacheServiceName = $container->getParameter('rikudou.memoize.cache_service');
4546
assert(is_string($cacheServiceName));
47+
$keySpecifierServiceName = $container->getParameter('rikudou.memoize.key_specifier_service');
48+
assert(is_string($keySpecifierServiceName));
4649
$services = array_keys($container->findTaggedServiceIds('rikudou.memoize.memoizable_service'));
4750

4851
foreach ($services as $service) {
@@ -72,6 +75,7 @@ public function process(ContainerBuilder $container): void
7275
new Reference('.inner'),
7376
new Reference($cacheServiceName),
7477
new Reference('rikudou.memoize.internal_cache'),
78+
new Reference($keySpecifierServiceName),
7579
]);
7680
$newDefinition->setDecoratedService($service);
7781
$container->setDefinition($proxyClass, $newDefinition);
@@ -143,11 +147,13 @@ private function getConstructor(Definition $serviceDefinition): string
143147
$originalClass = $serviceDefinition->getClass();
144148
$cacheClass = CacheItemPoolInterface::class;
145149
$internalCacheClass = InMemoryCachePool::class;
150+
$cacheKeySpecifierClass = CacheKeySpecifier::class;
146151

147152
$constructor = "\tpublic function __construct(\n";
148153
$constructor .= "\t\tprivate readonly \\{$originalClass} \$original,\n";
149154
$constructor .= "\t\tprivate readonly \\{$cacheClass} \$cache,\n";
150155
$constructor .= "\t\tprivate readonly \\{$internalCacheClass} \$internalCache,\n";
156+
$constructor .= "\t\tprivate readonly \\{$cacheKeySpecifierClass} \$cacheKeySpecifier,\n";
151157
$constructor .= "\t) {}";
152158

153159
return $constructor;
@@ -189,6 +195,7 @@ private function getMemoizedMethod(ReflectionMethod $method, string $serviceId):
189195
}
190196
$methodContent .= "\t\t\$cacheKey .= serialize({$parameter});\n";
191197
}
198+
$methodContent .= "\t\t\$cacheKey .= \$this->cacheKeySpecifier->generate();\n";
192199
$methodContent .= "\t\t\$cacheKey = hash('sha512', \$cacheKey);\n";
193200
$methodContent .= "\t\t\$cacheKey = \"rikudou_memoize_{$serviceName}_{$method->getName()}_{\$cacheKey}\";\n\n";
194201
if ($expiresAfter < 0) {

src/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ public function getConfigTreeBuilder(): TreeBuilder
2727
->info('The default cache service to use. If default_memoize_seconds is set to -1 this setting is ignored and internal service is used.')
2828
->defaultValue('cache.app')
2929
->end()
30+
->scalarNode('key_specifier_service')
31+
->info('The service to use to alter the cache key. Useful if you need to alter the cache key based on some global state.')
32+
->defaultValue('rikudou.memoize.key_specifier.null')
33+
->end()
3034
->end();
3135

3236
return $treeBuilder;

src/DependencyInjection/RikudouMemoizeExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ static function (ChildDefinition $definition): void {
3636
$container->setParameter('rikudou.memoize.cache_service', $configs['cache_service']);
3737
$container->setParameter('rikudou.memoize.default_memoize_seconds', $configs['default_memoize_seconds']);
3838
$container->setParameter('rikudou.memoize.enabled', $configs['enabled']);
39+
$container->setParameter('rikudou.memoize.key_specifier_service', $configs['key_specifier_service']);
3940
}
4041
}

src/Resources/config/services.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
services:
22
rikudou.memoize.internal_cache:
33
class: Rikudou\MemoizeBundle\Cache\InMemoryCachePool
4+
rikudou.memoize.key_specifier.null:
5+
class: Rikudou\MemoizeBundle\Cache\KeySpecifier\NullCacheKeySpecifier

0 commit comments

Comments
 (0)