Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 36 additions & 74 deletions src/Illuminate/Cache/DynamoDbStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Aws\DynamoDb\Exception\DynamoDbException;
use Illuminate\Contracts\Cache\LockProvider;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\InteractsWithTime;
Expand All @@ -16,41 +17,6 @@ class DynamoDbStore implements LockProvider, Store
{
use InteractsWithTime;

/**
* The DynamoDB client instance.
*
* @var \Aws\DynamoDb\DynamoDbClient
*/
protected $dynamo;

/**
* The table name.
*
* @var string
*/
protected $table;

/**
* The name of the attribute that should hold the key.
*
* @var string
*/
protected $keyAttribute;

/**
* The name of the attribute that should hold the value.
*
* @var string
*/
protected $valueAttribute;

/**
* The name of the attribute that should hold the expiration timestamp.
*
* @var string
*/
protected $expirationAttribute;

/**
* A string that should be prepended to keys.
*
Expand All @@ -61,27 +27,21 @@ class DynamoDbStore implements LockProvider, Store
/**
* Create a new store instance.
*
* @param \Aws\DynamoDb\DynamoDbClient $dynamo
* @param string $table
* @param string $keyAttribute
* @param string $valueAttribute
* @param string $expirationAttribute
* @param \Aws\DynamoDb\DynamoDbClient $dynamo The DynamoDB client instance.
* @param string $table The table name.
* @param string $keyAttribute The name of the attribute that should hold the key.
* @param string $valueAttribute The name of the attribute that should hold the value.
* @param string $expirationAttribute The name of the attribute that should hold the expiration timestamp.
* @param string $prefix
*/
public function __construct(
DynamoDbClient $dynamo,
$table,
$keyAttribute = 'key',
$valueAttribute = 'value',
$expirationAttribute = 'expires_at',
protected DynamoDbClient $dynamo,
protected $table,
protected $keyAttribute = 'key',
protected $valueAttribute = 'value',
protected $expirationAttribute = 'expires_at',
$prefix = '',
) {
$this->table = $table;
$this->dynamo = $dynamo;
$this->keyAttribute = $keyAttribute;
$this->valueAttribute = $valueAttribute;
$this->expirationAttribute = $expirationAttribute;

$this->setPrefix($prefix);
}

Expand Down Expand Up @@ -142,34 +102,32 @@ public function many(array $keys)
'RequestItems' => [
$this->table => [
'ConsistentRead' => false,
'Keys' => (new Collection($prefixedKeys))->map(function ($key) {
return [
$this->keyAttribute => [
'S' => $key,
],
];
})->all(),
'Keys' => (new Collection($prefixedKeys))->map(fn ($key) => [
$this->keyAttribute => [
'S' => $key,
],
])->all(),
],
],
]);

$now = Carbon::now();

return array_merge((new Collection(array_flip($keys)))->map(function () {
//
})->all(), (new Collection($response['Responses'][$this->table]))->mapWithKeys(function ($response) use ($now) {
if ($this->isExpired($response, $now)) {
$value = null;
} else {
$value = $this->unserialize(
$response[$this->valueAttribute]['S'] ??
$response[$this->valueAttribute]['N'] ??
null
);
}

return [Str::replaceFirst($this->prefix, '', $response[$this->keyAttribute]['S']) => $value];
})->all());
return array_merge(
Arr::mapWithKeys($keys, fn ($key) => [$key => null]),
(new Collection($response['Responses'][$this->table]))->mapWithKeys(function ($response) use ($now) {
if ($this->isExpired($response, $now)) {
$value = null;
} else {
$value = $this->unserialize(
$response[$this->valueAttribute]['S'] ??
$response[$this->valueAttribute]['N'] ??
null
);
}

return [Str::replaceFirst($this->prefix, '', $response[$this->keyAttribute]['S']) => $value];
})->all());
}

/**
Expand Down Expand Up @@ -307,6 +265,8 @@ public function add($key, $value, $seconds)
* @param string $key
* @param mixed $value
* @return int|false
*
* @throws \Aws\DynamoDb\Exception\DynamoDbException
*/
public function increment($key, $value = 1)
{
Expand Down Expand Up @@ -352,6 +312,8 @@ public function increment($key, $value = 1)
* @param string $key
* @param mixed $value
* @return int|false
*
* @throws \Aws\DynamoDb\Exception\DynamoDbException
*/
public function decrement($key, $value = 1)
{
Expand Down Expand Up @@ -451,7 +413,7 @@ public function forget($key)
/**
* Remove all items from the cache.
*
* @return bool
* @return never
*
* @throws \RuntimeException
*/
Expand Down
Loading