Skip to content

Commit

Permalink
Merge 0a794d5 into 0a44b4b
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Sep 30, 2020
2 parents 0a44b4b + 0a794d5 commit 552ba67
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 7 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ two subkeys:
- `region` - the AWS region (default: **us-east-1**)
- `version` - the service version (default: **latest**)
- no other options are available, if you need to configure more, please create and assign custom `client_service`
- `encoder` - contains the settings for encoders:
- `service` - the service which will be used as the encoder
- can be one of built-in services (`rikudou.dynamo_cache.encoder.serialize`, `rikudou.dynamo_cache.encoder.json`)
- can be a custom service implementing the `\Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface` interface
- default value: **rikudou.dynamo_cache.encoder.serialize**
- `json_options` - settings for the json encoder, ignored if another encoder is used
- `encode_flags` - the same flags you would pass to `json_encode()`
- `decode_flags` - the same flags you would pass to `json_decode()`
- `depth` - the depth argument for both `json_encode()` and `json_decode()`
- `primary_key_field` - the name of the field that will be used as the primary key (default: **id**)
- `ttl_field` - the name of the field that will be used as the ttl field (default: **ttl**)
- `value_field` - the name of the field that will be used as the value field (default: **value**)
Expand Down Expand Up @@ -57,6 +66,22 @@ rikudou_dynamo_db_cache:

# The service version
version: latest
encoder:

# The service to be used as the encoder/decoder
service: rikudou.dynamo_cache.encoder.serialize

# Settings for the json encoder
json_options:

# The flags that will be passed when encoding
encode_flags: 0

# The flags that will be passed when decoding
decode_flags: 0

# The depth of the JSON parsing for encoding/decoding
depth: 512

# The field to be used as primary key
primary_key_field: id
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"license": "MIT",
"require": {
"symfony/framework-bundle": "^5.0",
"rikudou/psr6-dynamo-db": "^1.2",
"rikudou/psr6-dynamo-db": "^1.3.1",
"php": "^7.2",
"symfony/cache": "^5.0"
},
Expand Down
22 changes: 21 additions & 1 deletion src/Converter/CacheItemConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,30 @@
use Psr\Cache\CacheItemInterface;
use ReflectionException;
use ReflectionProperty;
use Rikudou\Clock\ClockInterface;
use Rikudou\DynamoDbCache\Converter\CacheItemConverterInterface;
use Rikudou\DynamoDbCache\DynamoCacheItem;
use Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface;
use Symfony\Component\Cache\CacheItem;

final class CacheItemConverter implements CacheItemConverterInterface
{
/**
* @var ClockInterface
*/
private $clock;

/**
* @var CacheItemEncoderInterface
*/
private $encoder;

public function __construct(ClockInterface $clock, CacheItemEncoderInterface $encoder)
{
$this->clock = $clock;
$this->encoder = $encoder;
}

public function supports(CacheItemInterface $cacheItem): bool
{
return $cacheItem instanceof CacheItem;
Expand Down Expand Up @@ -41,7 +59,9 @@ public function convert(CacheItemInterface $cacheItem): DynamoCacheItem
$cacheItem->getKey(),
$cacheItem->isHit(),
$cacheItem->get(),
$expiry
$expiry,
$this->clock,
$this->encoder
);
}

Expand Down
27 changes: 27 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ public function getConfigTreeBuilder()
->end()
->end()
->end()
->arrayNode('encoder')
->addDefaultsIfNotSet()
->children()
->scalarNode('service')
->info('The service to be used as the encoder/decoder')
->defaultValue('rikudou.dynamo_cache.encoder.serialize')
->end()
->arrayNode('json_options')
->info('Settings for the json encoder')
->addDefaultsIfNotSet()
->children()
->integerNode('encode_flags')
->info('The flags that will be passed when encoding')
->defaultValue(0)
->end()
->integerNode('decode_flags')
->info('The flags that will be passed when decoding')
->defaultValue(0)
->end()
->integerNode('depth')
->info('The depth of the JSON parsing for encoding/decoding')
->defaultValue(512)
->end()
->end()
->end()
->end()
->end()
->scalarNode('primary_key_field')
->info('The field to be used as primary key')
->defaultValue('id')
Expand Down
38 changes: 37 additions & 1 deletion src/DependencyInjection/RikudouDynamoDbCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function load(array $configs, ContainerBuilder $container): void
$configs = $this->processConfiguration(new Configuration(), $configs);
$clientService = $this->createDynamoClient($container, $configs);
$this->createCacheClient($container, $configs, $clientService);
$container->setParameter('rikudou.dynamo_cache.internal.replace_adapter', $configs['replace_default_adapter']);
$this->createDefaultEncoder($container, $configs);
$this->createParameters($container, $configs);
}

/**
Expand Down Expand Up @@ -65,4 +66,39 @@ private function createCacheClient(ContainerBuilder $container, array $configs,
$definition->addArgument($configs['ttl_field']);
$definition->addArgument($configs['value_field']);
}

/**
* @param ContainerBuilder $container
* @param array<string, mixed> $configs
*/
private function createDefaultEncoder(ContainerBuilder $container, array $configs): void
{
$container->removeDefinition('rikudou.dynamo_cache.encoder.default');
$container->setAlias('rikudou.dynamo_cache.encoder.default', $configs['encoder']['service']);
}

/**
* @param ContainerBuilder $container
* @param array<string, mixed> $configs
*/
private function createParameters(ContainerBuilder $container, array $configs): void
{
$container->setParameter(
'rikudou.dynamo_cache.internal.replace_adapter',
$configs['replace_default_adapter']
);

$container->setParameter(
'rikudou.dynamo_cache.json_encoder.encode_flags',
$configs['encoder']['json_options']['encode_flags']
);
$container->setParameter(
'rikudou.dynamo_cache.json_encoder.decode_flags',
$configs['encoder']['json_options']['decode_flags']
);
$container->setParameter(
'rikudou.dynamo_cache.json_encoder.depth',
$configs['encoder']['json_options']['depth']
);
}
}
1 change: 1 addition & 0 deletions src/Resources/config/aliases.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
services:
Rikudou\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter: '@rikudou.dynamo_cache.adapter'
Rikudou\DynamoDbCache\DynamoDbCache: '@rikudou.dynamo_cache.cache'
Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface: '@rikudou.dynamo_cache.encoder.default'
21 changes: 20 additions & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
services:
rikudou.clock.default:
class: Rikudou\Clock\Clock

rikudou.dynamo_cache.encoder.serialize:
class: Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder

rikudou.dynamo_cache.encoder.json:
class: Rikudou\DynamoDbCache\Encoder\JsonItemEncoder
arguments:
- '%rikudou.dynamo_cache.json_encoder.encode_flags%'
- '%rikudou.dynamo_cache.json_encoder.decode_flags%'
- '%rikudou.dynamo_cache.json_encoder.depth%'

rikudou.dynamo_cache.encoder.default: '@rikudou.dynamo_cache.encoder.serialize' # redefined in extension

rikudou.dynamo_cache.cache: # arguments defined in extension
class: Rikudou\DynamoDbCache\DynamoDbCache
arguments:
$clock: ~
$clock: '@rikudou.clock.default'
$converter: '@rikudou.dynamo_cache.converter_registry'
$encoder: '@rikudou.dynamo_cache.encoder.default'

rikudou.dynamo_cache.adapter:
class: Rikudou\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter
Expand All @@ -13,6 +29,9 @@ services:

rikudou.dynamo_cache.converter.cache_item:
class: Rikudou\DynamoDbCacheBundle\Converter\CacheItemConverter
arguments:
- '@rikudou.clock.default'
- '@rikudou.dynamo_cache.encoder.default'
tags:
- rikudou.dynamo_cache.converter

Expand Down
4 changes: 3 additions & 1 deletion tests/AbstractCacheItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use ReflectionProperty;
use Rikudou\Clock\Clock;
use Rikudou\DynamoDbCache\DynamoCacheItem;
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
use Symfony\Component\Cache\CacheItem;

abstract class AbstractCacheItemTest extends TestCase
Expand Down Expand Up @@ -48,7 +50,7 @@ protected function createDynamoCacheItem(
string $value = 'value',
?DateTimeInterface $dateTime = null
): DynamoCacheItem {
return new DynamoCacheItem($key, $isHit, $value, $dateTime);
return new DynamoCacheItem($key, $isHit, $value, $dateTime, new Clock(), new SerializeItemEncoder());
}

protected function getRandomCacheItem(
Expand Down
7 changes: 6 additions & 1 deletion tests/Cache/DynamoDbCacheAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\Result;
use ReflectionObject;
use Rikudou\Clock\Clock;
use Rikudou\DynamoDbCache\DynamoDbCache;
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
use Rikudou\DynamoDbCacheBundle\Cache\DynamoDbCacheAdapter;
use Rikudou\DynamoDbCacheBundle\Converter\CacheItemConverter;
use Rikudou\Tests\DynamoDbCacheBundle\AbstractCacheItemTest;
Expand Down Expand Up @@ -59,7 +61,10 @@ protected function setUp(): void
{
$this->instance = new DynamoDbCacheAdapter(
new DynamoDbCache('test', $this->getFakeClient($this->itemPoolDefault)),
new CacheItemConverter()
new CacheItemConverter(
new Clock(),
new SerializeItemEncoder()
)
);
}

Expand Down
7 changes: 6 additions & 1 deletion tests/Converter/CacheItemConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Rikudou\Tests\DynamoDbCacheBundle\Converter;

use DateTime;
use Rikudou\Clock\Clock;
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
use Rikudou\DynamoDbCacheBundle\Converter\CacheItemConverter;
use Rikudou\Tests\DynamoDbCacheBundle\AbstractCacheItemTest;

Expand All @@ -15,7 +17,10 @@ final class CacheItemConverterTest extends AbstractCacheItemTest

protected function setUp(): void
{
$this->instance = new CacheItemConverter();
$this->instance = new CacheItemConverter(
new Clock(),
new SerializeItemEncoder()
);
}

public function testSupports()
Expand Down

0 comments on commit 552ba67

Please sign in to comment.