Skip to content

Commit 51e3c1a

Browse files
committed
Refactor cache management
- strip the "management" part from the name(space) - move it into the "Converter" namespace - refine class/method names
1 parent 2e39722 commit 51e3c1a

File tree

14 files changed

+98
-108
lines changed

14 files changed

+98
-108
lines changed

docs/cached-converter.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Maybe in our `User` example there will be a unique user ID (uuid) then your `Cac
1616
should be the following:
1717

1818
```php
19-
use Neusta\ConverterBundle\CacheManagement\CacheKeyFactory;
19+
use Neusta\ConverterBundle\Converter\Cache\CacheKeyFactory;
2020

2121
/**
2222
* @implements CacheKeyFactory<User>
@@ -53,11 +53,11 @@ neusta_converter:
5353
key_factory: YourNamespace\UserKeyFactory
5454
```
5555

56-
This will use the `DefaultCacheManagement`, which is offering a simple array-based cache of converted objects
56+
This will use the `InMemoryCache`, which is offering a simple array-based cache of converted objects
5757
using the `key_factory` to determine the cache key. This allows you to implement very domain-specific identifications
5858
of your object conversions.
5959

60-
> Note: You can also use a custom implementation of the `CacheManagement` interface by using the `service`
60+
> Note: You can also use a custom implementation of the `Cache` interface by using the `service`
6161
> instead of the `key_factory` keyword.
6262

6363
## Why?!

src/CacheManagement/CacheManagement.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/CacheManagement/DefaultCacheManagement.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/Converter/Cache/Cache.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Neusta\ConverterBundle\Converter\Cache;
6+
7+
/**
8+
* @template TSource of object
9+
* @template TTarget of object
10+
*/
11+
interface Cache
12+
{
13+
/**
14+
* @param TSource $source
15+
*
16+
* @return TTarget|null
17+
*/
18+
public function get(object $source): ?object;
19+
20+
/**
21+
* @param TSource $source
22+
* @param TTarget $target
23+
*/
24+
public function set(object $source, object $target): void;
25+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
declare(strict_types=1);
44

55

6-
namespace Neusta\ConverterBundle\CacheManagement;
6+
namespace Neusta\ConverterBundle\Converter\Cache;
77

88
/**
99
* @template TSource of object
@@ -13,5 +13,5 @@ interface CacheKeyFactory
1313
/**
1414
* @param TSource $source
1515
*/
16-
public function createCacheKey(object $source): string;
16+
public function createFor(object $source): string;
1717
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Neusta\ConverterBundle\Converter\Cache;
6+
7+
/**
8+
* @template TSource of object
9+
* @template TTarget of object
10+
*
11+
* @implements Cache<TSource, TTarget>
12+
*/
13+
final class InMemoryCache implements Cache
14+
{
15+
/**
16+
* @var array<string, TTarget>
17+
*/
18+
private array $targets = [];
19+
20+
/**
21+
* @param CacheKeyFactory<TSource> $keyFactory
22+
*/
23+
public function __construct(
24+
private CacheKeyFactory $keyFactory,
25+
) {
26+
}
27+
28+
public function get(object $source): ?object
29+
{
30+
return $this->targets[$this->keyFactory->createFor($source)] ?? null;
31+
}
32+
33+
public function set(object $source, object $target): void
34+
{
35+
$this->targets[$this->keyFactory->createFor($source)] = $target;
36+
}
37+
}

src/Converter/CachedConverter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Neusta\ConverterBundle\Converter;
66

7-
use Neusta\ConverterBundle\CacheManagement\CacheManagement;
7+
use Neusta\ConverterBundle\Converter\Cache\Cache;
88

99
/**
1010
* @template TSource of object
@@ -17,11 +17,11 @@ class CachedConverter implements Converter
1717
{
1818
/**
1919
* @param Converter<TSource, TTarget, TContext> $inner
20-
* @param CacheManagement<TSource, TTarget> $cacheManagement
20+
* @param Cache<TSource, TTarget> $cache
2121
*/
2222
public function __construct(
2323
private Converter $inner,
24-
private CacheManagement $cacheManagement,
24+
private Cache $cache,
2525
) {
2626
}
2727

@@ -33,13 +33,13 @@ public function __construct(
3333
*/
3434
public function convert(object $source, ?object $ctx = null): object
3535
{
36-
if ($this->cacheManagement->isInCache($source)) {
37-
return $this->cacheManagement->get($source);
36+
if ($target = $this->cache->get($source)) {
37+
return $target;
3838
}
3939

4040
$target = $this->inner->convert($source, $ctx);
4141

42-
$this->cacheManagement->writeInCache($target, $source);
42+
$this->cache->set($source, $target);
4343

4444
return $target;
4545
}

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private function addConverterSection(ArrayNodeDefinition $rootNode): void
5656
->info('Whether the result should be cached')
5757
->children()
5858
->scalarNode('service')
59-
->info('Service id to override the cache management entirely')
59+
->info('Service id to override the cache entirely')
6060
->defaultNull()
6161
->end()
6262
->scalarNode('key_factory')

src/DependencyInjection/NeustaConverterExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Neusta\ConverterBundle\DependencyInjection;
66

7-
use Neusta\ConverterBundle\CacheManagement\DefaultCacheManagement;
7+
use Neusta\ConverterBundle\Converter\Cache\InMemoryCache;
88
use Neusta\ConverterBundle\Converter\CachedConverter;
99
use Neusta\ConverterBundle\Converter\Converter;
1010
use Neusta\ConverterBundle\Populator\MappedPropertyPopulator;
@@ -56,8 +56,8 @@ private function registerConverterConfiguration(string $id, array $config, Conta
5656
]);
5757

5858
if (isset($config['cached'])) {
59-
if (!$cacheManagementId = $config['cached']['service'] ?? null) {
60-
$container->register($cacheManagementId = "{$id}.cache_management", DefaultCacheManagement::class)
59+
if (!$cacheId = $config['cached']['service'] ?? null) {
60+
$container->register($cacheId = "{$id}.cache", InMemoryCache::class)
6161
->setArguments([
6262
'$keyFactory' => new Reference($config['cached']['key_factory']),
6363
]);
@@ -67,7 +67,7 @@ private function registerConverterConfiguration(string $id, array $config, Conta
6767
->setDecoratedService($id)
6868
->setArguments([
6969
'$inner' => new Reference('.inner'),
70-
'$cacheManagement' => new Reference($cacheManagementId),
70+
'$cache' => new Reference($cacheId),
7171
]);
7272
}
7373
}

tests/Converter/CachedConverterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
namespace Neusta\ConverterBundle\Tests\Converter;
66

7-
use Neusta\ConverterBundle\CacheManagement\DefaultCacheManagement;
7+
use Neusta\ConverterBundle\Converter\Cache\InMemoryCache;
88
use Neusta\ConverterBundle\Converter\Converter;
99
use Neusta\ConverterBundle\Converter\CachedConverter;
1010
use Neusta\ConverterBundle\Converter\DefaultConverter;
11-
use Neusta\ConverterBundle\Tests\Fixtures\CacheManagement\UserKeyFactory;
11+
use Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache\UserKeyFactory;
1212
use Neusta\ConverterBundle\Tests\Fixtures\Factory\PersonFactory;
1313
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
1414
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
@@ -29,7 +29,7 @@ protected function setUp(): void
2929
new PersonNamePopulator()
3030
]
3131
),
32-
new DefaultCacheManagement(new UserKeyFactory())
32+
new InMemoryCache(new UserKeyFactory())
3333
);
3434
}
3535

0 commit comments

Comments
 (0)