Skip to content

Commit

Permalink
Merge a53bc97 into cd3cb78
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Sep 25, 2020
2 parents cd3cb78 + a53bc97 commit 4e69a32
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Tests](https://github.com/RikudouSage/DynamoDbCachePsr6/workflows/Tests/badge.svg)](https://github.com/RikudouSage/DynamoDbCachePsr6/actions?query=workflow%3ATests)
[![Coverage Status](https://coveralls.io/repos/github/RikudouSage/DynamoDbCachePsr6/badge.svg?branch=master)](https://coveralls.io/github/RikudouSage/DynamoDbCachePsr6?branch=master)

Library for storing cache in DynamoDB implementing the PSR-6 interface.
Library for storing cache in DynamoDB implementing the PSR-6 and PSR-16 interfaces.
See also [Symfony bundle](https://github.com/RikudouSage/DynamoDbCachePsr6Bundle) of this library.

## Installation
Expand All @@ -11,7 +11,7 @@ See also [Symfony bundle](https://github.com/RikudouSage/DynamoDbCachePsr6Bundle
## Usage

The usage is pretty straight-forward, you just define the details in constructor and then use it as any other
PSR-6 implementation:
PSR-6 or PSR-16 implementation:

```php
<?php
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "rikudou/psr6-dynamo-db",
"description": "PSR-6 cache implementation using AWS DynamoDB",
"description": "PSR-6 and PSR-16 cache implementation using AWS DynamoDB",
"minimum-stability": "stable",
"license": "MIT",
"require": {
"psr/cache": "^1.0",
"php": "^7.2",
"aws/aws-sdk-php": "^3.0",
"rikudou/clock": "^1.0"
"rikudou/clock": "^1.0",
"psr/simple-cache": "^1.0"
},
"autoload": {
"psr-4": {
Expand All @@ -25,7 +26,8 @@
"phpunit/phpunit": "^9.3"
},
"provide": {
"psr/cache-implementation": "1.0"
"psr/cache-implementation": "1.0",
"psr/simple-cache-implementation": "1.0"
},
"scripts": {
"fixer": "php-cs-fixer fix src --verbose",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
treatPhpDocTypesAsCertain: false
140 changes: 139 additions & 1 deletion src/DynamoDbCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@

use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Exception\DynamoDbException;
use DateInterval;
use DateTime;
use DateTimeImmutable;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Psr\SimpleCache\CacheInterface;
use Rikudou\Clock\Clock;
use Rikudou\Clock\ClockInterface;
use Rikudou\DynamoDbCache\Converter\CacheItemConverterRegistry;
use Rikudou\DynamoDbCache\Exception\InvalidArgumentException;

final class DynamoDbCache implements CacheItemPoolInterface
final class DynamoDbCache implements CacheItemPoolInterface, CacheInterface
{
private const RESERVED_CHARACTERS = '{}()/\@:';

Expand Down Expand Up @@ -191,6 +193,8 @@ public function getItems(array $keys = [])
/**
* @param string $key
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function hasItem($key)
Expand Down Expand Up @@ -280,6 +284,8 @@ public function deleteItems(array $keys)
/**
* @param CacheItemInterface $item
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function save(CacheItemInterface $item)
Expand Down Expand Up @@ -317,6 +323,8 @@ public function save(CacheItemInterface $item)
/**
* @param CacheItemInterface $item
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function saveDeferred(CacheItemInterface $item)
Expand All @@ -332,6 +340,8 @@ public function saveDeferred(CacheItemInterface $item)
}

/**
* @throws InvalidArgumentException
*
* @return bool
*/
public function commit()
Expand All @@ -349,6 +359,119 @@ public function commit()
return $result;
}

/**
* @param string $key
* @param mixed $default
*
* @throws InvalidArgumentException
*
* @return mixed
*/
public function get($key, $default = null)
{
$item = $this->getItem($key);
if (!$item->isHit()) {
return $default;
}

return $item->get();
}

/**
* @param string $key
* @param mixed $value
* @param int|DateInterval|null $ttl
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function set($key, $value, $ttl = null)
{
$item = $this->getItem($key);
if ($ttl !== null) {
$item->expiresAfter($ttl);
}
$item->set($value);

return $this->save($item);
}

/**
* @param string $key
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function delete($key)
{
return $this->deleteItem($key);
}

/**
* @param iterable<string> $keys
* @param mixed $default
*
* @return mixed[]
*/
public function getMultiple($keys, $default = null)
{
return array_map(function (DynamoCacheItem $item) use ($default) {
if ($item->isHit()) {
return $item->get();
}

return $default;
}, $this->iterableToArray($keys));
}

/**
* @param iterable<string,mixed> $values
* @param int|DateInterval|null $ttl
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function setMultiple($values, $ttl = null)
{
foreach ($values as $key => $value) {
$item = $this->getItem($key);
$item->set($value);
if ($ttl !== null) {
$item->expiresAfter($ttl);
}
$this->saveDeferred($item);
}

return $this->commit();
}

/**
* @param iterable<string> $keys
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function deleteMultiple($keys)
{
return $this->deleteItems($this->iterableToArray($keys));
}

/**
* @param string $key
*
* @throws InvalidArgumentException
*
* @return bool
*/
public function has($key)
{
return $this->hasItem($key);
}

private function getExceptionForInvalidKey(string $key): ?InvalidArgumentException
{
if (strpbrk($key, self::RESERVED_CHARACTERS) !== false) {
Expand All @@ -363,4 +486,19 @@ private function getExceptionForInvalidKey(string $key): ?InvalidArgumentExcepti

return null;
}

/**
* @param iterable<mixed,mixed> $iterable
*
* @return array<mixed,mixed>
*/
private function iterableToArray(iterable $iterable): array
{
if (is_array($iterable)) {
return $iterable;
} else {
/** @noinspection PhpParamsInspection */
return iterator_to_array($iterable);
}
}
}
3 changes: 2 additions & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Exception;
use Psr\Cache\InvalidArgumentException as PsrException;
use Psr\SimpleCache\InvalidArgumentException as SimplePsrException;

final class InvalidArgumentException extends Exception implements PsrException
final class InvalidArgumentException extends Exception implements PsrException, SimplePsrException
{
}

0 comments on commit 4e69a32

Please sign in to comment.