Skip to content

Commit

Permalink
Refactor cache management
Browse files Browse the repository at this point in the history
- strip the "management" part from the name(space)
- move it into the "Converter" namespace
- refine class/method names
  • Loading branch information
jdreesen committed Jan 28, 2023
1 parent 2e39722 commit 51e3c1a
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 108 deletions.
6 changes: 3 additions & 3 deletions docs/cached-converter.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Maybe in our `User` example there will be a unique user ID (uuid) then your `Cac
should be the following:

```php
use Neusta\ConverterBundle\CacheManagement\CacheKeyFactory;
use Neusta\ConverterBundle\Converter\Cache\CacheKeyFactory;

/**
* @implements CacheKeyFactory<User>
Expand Down Expand Up @@ -53,11 +53,11 @@ neusta_converter:
key_factory: YourNamespace\UserKeyFactory
```

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

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

## Why?!
Expand Down
30 changes: 0 additions & 30 deletions src/CacheManagement/CacheManagement.php

This file was deleted.

42 changes: 0 additions & 42 deletions src/CacheManagement/DefaultCacheManagement.php

This file was deleted.

25 changes: 25 additions & 0 deletions src/Converter/Cache/Cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Neusta\ConverterBundle\Converter\Cache;

/**
* @template TSource of object
* @template TTarget of object
*/
interface Cache
{
/**
* @param TSource $source
*
* @return TTarget|null
*/
public function get(object $source): ?object;

/**
* @param TSource $source
* @param TTarget $target
*/
public function set(object $source, object $target): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
declare(strict_types=1);


namespace Neusta\ConverterBundle\CacheManagement;
namespace Neusta\ConverterBundle\Converter\Cache;

/**
* @template TSource of object
Expand All @@ -13,5 +13,5 @@ interface CacheKeyFactory
/**
* @param TSource $source
*/
public function createCacheKey(object $source): string;
public function createFor(object $source): string;
}
37 changes: 37 additions & 0 deletions src/Converter/Cache/InMemoryCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Neusta\ConverterBundle\Converter\Cache;

/**
* @template TSource of object
* @template TTarget of object
*
* @implements Cache<TSource, TTarget>
*/
final class InMemoryCache implements Cache
{
/**
* @var array<string, TTarget>
*/
private array $targets = [];

/**
* @param CacheKeyFactory<TSource> $keyFactory
*/
public function __construct(
private CacheKeyFactory $keyFactory,
) {
}

public function get(object $source): ?object
{
return $this->targets[$this->keyFactory->createFor($source)] ?? null;
}

public function set(object $source, object $target): void
{
$this->targets[$this->keyFactory->createFor($source)] = $target;
}
}
12 changes: 6 additions & 6 deletions src/Converter/CachedConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Neusta\ConverterBundle\Converter;

use Neusta\ConverterBundle\CacheManagement\CacheManagement;
use Neusta\ConverterBundle\Converter\Cache\Cache;

/**
* @template TSource of object
Expand All @@ -17,11 +17,11 @@ class CachedConverter implements Converter
{
/**
* @param Converter<TSource, TTarget, TContext> $inner
* @param CacheManagement<TSource, TTarget> $cacheManagement
* @param Cache<TSource, TTarget> $cache
*/
public function __construct(
private Converter $inner,
private CacheManagement $cacheManagement,
private Cache $cache,
) {
}

Expand All @@ -33,13 +33,13 @@ public function __construct(
*/
public function convert(object $source, ?object $ctx = null): object
{
if ($this->cacheManagement->isInCache($source)) {
return $this->cacheManagement->get($source);
if ($target = $this->cache->get($source)) {
return $target;
}

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

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

return $target;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private function addConverterSection(ArrayNodeDefinition $rootNode): void
->info('Whether the result should be cached')
->children()
->scalarNode('service')
->info('Service id to override the cache management entirely')
->info('Service id to override the cache entirely')
->defaultNull()
->end()
->scalarNode('key_factory')
Expand Down
8 changes: 4 additions & 4 deletions src/DependencyInjection/NeustaConverterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Neusta\ConverterBundle\DependencyInjection;

use Neusta\ConverterBundle\CacheManagement\DefaultCacheManagement;
use Neusta\ConverterBundle\Converter\Cache\InMemoryCache;
use Neusta\ConverterBundle\Converter\CachedConverter;
use Neusta\ConverterBundle\Converter\Converter;
use Neusta\ConverterBundle\Populator\MappedPropertyPopulator;
Expand Down Expand Up @@ -56,8 +56,8 @@ private function registerConverterConfiguration(string $id, array $config, Conta
]);

if (isset($config['cached'])) {
if (!$cacheManagementId = $config['cached']['service'] ?? null) {
$container->register($cacheManagementId = "{$id}.cache_management", DefaultCacheManagement::class)
if (!$cacheId = $config['cached']['service'] ?? null) {
$container->register($cacheId = "{$id}.cache", InMemoryCache::class)
->setArguments([
'$keyFactory' => new Reference($config['cached']['key_factory']),
]);
Expand All @@ -67,7 +67,7 @@ private function registerConverterConfiguration(string $id, array $config, Conta
->setDecoratedService($id)
->setArguments([
'$inner' => new Reference('.inner'),
'$cacheManagement' => new Reference($cacheManagementId),
'$cache' => new Reference($cacheId),
]);
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Converter/CachedConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Neusta\ConverterBundle\Tests\Converter;

use Neusta\ConverterBundle\CacheManagement\DefaultCacheManagement;
use Neusta\ConverterBundle\Converter\Cache\InMemoryCache;
use Neusta\ConverterBundle\Converter\Converter;
use Neusta\ConverterBundle\Converter\CachedConverter;
use Neusta\ConverterBundle\Converter\DefaultConverter;
use Neusta\ConverterBundle\Tests\Fixtures\CacheManagement\UserKeyFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache\UserKeyFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Factory\PersonFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Model\Person;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;
Expand All @@ -29,7 +29,7 @@ protected function setUp(): void
new PersonNamePopulator()
]
),
new DefaultCacheManagement(new UserKeyFactory())
new InMemoryCache(new UserKeyFactory())
);
}

Expand Down
16 changes: 8 additions & 8 deletions tests/DependencyInjection/NeustaConverterExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace Neusta\ConverterBundle\Tests\DependencyInjection;

use Neusta\ConverterBundle\CacheManagement\DefaultCacheManagement;
use Neusta\ConverterBundle\Converter\Cache\InMemoryCache;
use Neusta\ConverterBundle\Converter\CachedConverter;
use Neusta\ConverterBundle\Converter\Converter;
use Neusta\ConverterBundle\Converter\DefaultConverter;
use Neusta\ConverterBundle\DependencyInjection\NeustaConverterExtension;
use Neusta\ConverterBundle\NeustaConverterBundle;
use Neusta\ConverterBundle\Populator\MappedPropertyPopulator;
use Neusta\ConverterBundle\Tests\Fixtures\CacheManagement\UserKeyFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache\UserKeyFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Factory\PersonFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Populator\PersonNamePopulator;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -117,12 +117,12 @@ public function test_with_default_cached_converter(): void
self::assertSame(CachedConverter::class, $cachedConverter->getClass());
self::assertSame('foobar', $cachedConverter->getDecoratedService()[0]);
self::assertIsReference('.inner', $cachedConverter->getArgument('$inner'));
self::assertIsReference('foobar.cache_management', $cachedConverter->getArgument('$cacheManagement'));
self::assertIsReference('foobar.cache', $cachedConverter->getArgument('$cache'));

// cache management
$cacheManagement = $container->getDefinition('foobar.cache_management');
self::assertSame(DefaultCacheManagement::class, $cacheManagement->getClass());
self::assertIsReference(UserKeyFactory::class, $cacheManagement->getArgument('$keyFactory'));
// cache
$cache = $container->getDefinition('foobar.cache');
self::assertSame(InMemoryCache::class, $cache->getClass());
self::assertIsReference(UserKeyFactory::class, $cache->getArgument('$keyFactory'));
}

public function test_with_cache_with_service_and_key_factory_defined(): void
Expand All @@ -138,7 +138,7 @@ public function test_with_cache_with_service_and_key_factory_defined(): void
PersonNamePopulator::class,
],
'cached' => [
'service' => DefaultCacheManagement::class,
'service' => InMemoryCache::class,
'key_factory' => UserKeyFactory::class,
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace Neusta\ConverterBundle\Tests\Fixtures\CacheManagement;
namespace Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache;

use Neusta\ConverterBundle\CacheManagement\CacheKeyFactory;
use Neusta\ConverterBundle\Converter\Cache\CacheKeyFactory;
use Neusta\ConverterBundle\Tests\Fixtures\Model\User;

/**
* @implements CacheKeyFactory<User>
*/
class UserKeyFactory implements CacheKeyFactory
{
public function createCacheKey(object $source): string
public function createFor(object $source): string
{
return (string) $source->getUuid();
}
Expand Down
4 changes: 2 additions & 2 deletions tests/app/config/packages/neusta_converter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ neusta_converter:
populators:
- Neusta\ConverterBundle\Tests\Fixtures\Populator\PersonNamePopulator
cached:
# service: user.cache_management
key_factory: Neusta\ConverterBundle\Tests\Fixtures\CacheManagement\UserKeyFactory
# service: user.cache
key_factory: Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache\UserKeyFactory
8 changes: 4 additions & 4 deletions tests/app/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ services:
Neusta\ConverterBundle\Tests\Fixtures\Factory\PersonFactory: ~
Neusta\ConverterBundle\Tests\Fixtures\Factory\AddressFactory: ~

Neusta\ConverterBundle\Tests\Fixtures\CacheManagement\UserKeyFactory: ~
Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache\UserKeyFactory: ~

user.cache_management:
class: Neusta\ConverterBundle\CacheManagement\DefaultCacheManagement
user.cache:
class: Neusta\ConverterBundle\Converter\Cache\InMemoryCache
arguments:
$keyFactory: '@Neusta\ConverterBundle\Tests\Fixtures\CacheManagement\UserKeyFactory'
$keyFactory: '@Neusta\ConverterBundle\Tests\Fixtures\Converter\Cache\UserKeyFactory'

0 comments on commit 51e3c1a

Please sign in to comment.