Skip to content

Commit 16ef628

Browse files
authored
[12.x] Clean up DynamoDbStore (#58116)
* clean up dynamodbstore * style ci * move to collection * oooh that's why there was a map there * use an array map instead
1 parent b63ee45 commit 16ef628

File tree

1 file changed

+36
-74
lines changed

1 file changed

+36
-74
lines changed

src/Illuminate/Cache/DynamoDbStore.php

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Aws\DynamoDb\Exception\DynamoDbException;
77
use Illuminate\Contracts\Cache\LockProvider;
88
use Illuminate\Contracts\Cache\Store;
9+
use Illuminate\Support\Arr;
910
use Illuminate\Support\Carbon;
1011
use Illuminate\Support\Collection;
1112
use Illuminate\Support\InteractsWithTime;
@@ -16,41 +17,6 @@ class DynamoDbStore implements LockProvider, Store
1617
{
1718
use InteractsWithTime;
1819

19-
/**
20-
* The DynamoDB client instance.
21-
*
22-
* @var \Aws\DynamoDb\DynamoDbClient
23-
*/
24-
protected $dynamo;
25-
26-
/**
27-
* The table name.
28-
*
29-
* @var string
30-
*/
31-
protected $table;
32-
33-
/**
34-
* The name of the attribute that should hold the key.
35-
*
36-
* @var string
37-
*/
38-
protected $keyAttribute;
39-
40-
/**
41-
* The name of the attribute that should hold the value.
42-
*
43-
* @var string
44-
*/
45-
protected $valueAttribute;
46-
47-
/**
48-
* The name of the attribute that should hold the expiration timestamp.
49-
*
50-
* @var string
51-
*/
52-
protected $expirationAttribute;
53-
5420
/**
5521
* A string that should be prepended to keys.
5622
*
@@ -61,27 +27,21 @@ class DynamoDbStore implements LockProvider, Store
6127
/**
6228
* Create a new store instance.
6329
*
64-
* @param \Aws\DynamoDb\DynamoDbClient $dynamo
65-
* @param string $table
66-
* @param string $keyAttribute
67-
* @param string $valueAttribute
68-
* @param string $expirationAttribute
30+
* @param \Aws\DynamoDb\DynamoDbClient $dynamo The DynamoDB client instance.
31+
* @param string $table The table name.
32+
* @param string $keyAttribute The name of the attribute that should hold the key.
33+
* @param string $valueAttribute The name of the attribute that should hold the value.
34+
* @param string $expirationAttribute The name of the attribute that should hold the expiration timestamp.
6935
* @param string $prefix
7036
*/
7137
public function __construct(
72-
DynamoDbClient $dynamo,
73-
$table,
74-
$keyAttribute = 'key',
75-
$valueAttribute = 'value',
76-
$expirationAttribute = 'expires_at',
38+
protected DynamoDbClient $dynamo,
39+
protected $table,
40+
protected $keyAttribute = 'key',
41+
protected $valueAttribute = 'value',
42+
protected $expirationAttribute = 'expires_at',
7743
$prefix = '',
7844
) {
79-
$this->table = $table;
80-
$this->dynamo = $dynamo;
81-
$this->keyAttribute = $keyAttribute;
82-
$this->valueAttribute = $valueAttribute;
83-
$this->expirationAttribute = $expirationAttribute;
84-
8545
$this->setPrefix($prefix);
8646
}
8747

@@ -142,34 +102,32 @@ public function many(array $keys)
142102
'RequestItems' => [
143103
$this->table => [
144104
'ConsistentRead' => false,
145-
'Keys' => (new Collection($prefixedKeys))->map(function ($key) {
146-
return [
147-
$this->keyAttribute => [
148-
'S' => $key,
149-
],
150-
];
151-
})->all(),
105+
'Keys' => (new Collection($prefixedKeys))->map(fn ($key) => [
106+
$this->keyAttribute => [
107+
'S' => $key,
108+
],
109+
])->all(),
152110
],
153111
],
154112
]);
155113

156114
$now = Carbon::now();
157115

158-
return array_merge((new Collection(array_flip($keys)))->map(function () {
159-
//
160-
})->all(), (new Collection($response['Responses'][$this->table]))->mapWithKeys(function ($response) use ($now) {
161-
if ($this->isExpired($response, $now)) {
162-
$value = null;
163-
} else {
164-
$value = $this->unserialize(
165-
$response[$this->valueAttribute]['S'] ??
166-
$response[$this->valueAttribute]['N'] ??
167-
null
168-
);
169-
}
170-
171-
return [Str::replaceFirst($this->prefix, '', $response[$this->keyAttribute]['S']) => $value];
172-
})->all());
116+
return array_merge(
117+
Arr::mapWithKeys($keys, fn ($key) => [$key => null]),
118+
(new Collection($response['Responses'][$this->table]))->mapWithKeys(function ($response) use ($now) {
119+
if ($this->isExpired($response, $now)) {
120+
$value = null;
121+
} else {
122+
$value = $this->unserialize(
123+
$response[$this->valueAttribute]['S'] ??
124+
$response[$this->valueAttribute]['N'] ??
125+
null
126+
);
127+
}
128+
129+
return [Str::replaceFirst($this->prefix, '', $response[$this->keyAttribute]['S']) => $value];
130+
})->all());
173131
}
174132

175133
/**
@@ -307,6 +265,8 @@ public function add($key, $value, $seconds)
307265
* @param string $key
308266
* @param mixed $value
309267
* @return int|false
268+
*
269+
* @throws \Aws\DynamoDb\Exception\DynamoDbException
310270
*/
311271
public function increment($key, $value = 1)
312272
{
@@ -352,6 +312,8 @@ public function increment($key, $value = 1)
352312
* @param string $key
353313
* @param mixed $value
354314
* @return int|false
315+
*
316+
* @throws \Aws\DynamoDb\Exception\DynamoDbException
355317
*/
356318
public function decrement($key, $value = 1)
357319
{
@@ -451,7 +413,7 @@ public function forget($key)
451413
/**
452414
* Remove all items from the cache.
453415
*
454-
* @return bool
416+
* @return never
455417
*
456418
* @throws \RuntimeException
457419
*/

0 commit comments

Comments
 (0)