Skip to content

Commit 74a79b2

Browse files
authored
[6.x] Fix Cache store with a name other than 'dynamodb' (#37145)
* Fix Cache store with a name other than 'dynamodb' * StyleCI
1 parent bf0b653 commit 74a79b2

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

src/Illuminate/Cache/CacheManager.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Illuminate\Cache;
44

5+
use Aws\DynamoDb\DynamoDbClient;
56
use Closure;
67
use Illuminate\Contracts\Cache\Factory as FactoryContract;
78
use Illuminate\Contracts\Cache\Store;
89
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
10+
use Illuminate\Support\Arr;
911
use InvalidArgumentException;
1012

1113
/**
@@ -224,9 +226,11 @@ protected function createDatabaseDriver(array $config)
224226
*/
225227
protected function createDynamodbDriver(array $config)
226228
{
229+
$client = $this->newDynamodbClient($config);
230+
227231
return $this->repository(
228232
new DynamoDbStore(
229-
$this->app['cache.dynamodb.client'],
233+
$client,
230234
$config['table'],
231235
$config['attributes']['key'] ?? 'key',
232236
$config['attributes']['value'] ?? 'value',
@@ -236,6 +240,28 @@ protected function createDynamodbDriver(array $config)
236240
);
237241
}
238242

243+
/**
244+
* Create new DynamoDb Client instance.
245+
*
246+
* @return DynamoDbClient
247+
*/
248+
protected function newDynamodbClient(array $config)
249+
{
250+
$dynamoConfig = [
251+
'region' => $config['region'],
252+
'version' => 'latest',
253+
'endpoint' => $config['endpoint'] ?? null,
254+
];
255+
256+
if (isset($config['key']) && isset($config['secret'])) {
257+
$dynamoConfig['credentials'] = Arr::only(
258+
$config, ['key', 'secret', 'token']
259+
);
260+
}
261+
262+
return new DynamoDbClient($dynamoConfig);
263+
}
264+
239265
/**
240266
* Create a new cache repository with the given implementation.
241267
*

src/Illuminate/Cache/CacheServiceProvider.php

+1-21
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace Illuminate\Cache;
44

5-
use Aws\DynamoDb\DynamoDbClient;
65
use Illuminate\Contracts\Support\DeferrableProvider;
7-
use Illuminate\Support\Arr;
86
use Illuminate\Support\ServiceProvider;
97
use Symfony\Component\Cache\Adapter\Psr16Adapter;
108

@@ -32,24 +30,6 @@ public function register()
3230
$this->app->singleton('memcached.connector', function () {
3331
return new MemcachedConnector;
3432
});
35-
36-
$this->app->singleton('cache.dynamodb.client', function ($app) {
37-
$config = $app['config']->get('cache.stores.dynamodb');
38-
39-
$dynamoConfig = [
40-
'region' => $config['region'],
41-
'version' => 'latest',
42-
'endpoint' => $config['endpoint'] ?? null,
43-
];
44-
45-
if ($config['key'] && $config['secret']) {
46-
$dynamoConfig['credentials'] = Arr::only(
47-
$config, ['key', 'secret', 'token']
48-
);
49-
}
50-
51-
return new DynamoDbClient($dynamoConfig);
52-
});
5333
}
5434

5535
/**
@@ -60,7 +40,7 @@ public function register()
6040
public function provides()
6141
{
6242
return [
63-
'cache', 'cache.store', 'cache.psr6', 'memcached.connector', 'cache.dynamodb.client',
43+
'cache', 'cache.store', 'cache.psr6', 'memcached.connector',
6444
];
6545
}
6646
}

src/Illuminate/Cache/DynamoDbStore.php

+10
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,14 @@ public function setPrefix($prefix)
525525
{
526526
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
527527
}
528+
529+
/**
530+
* Get the DynamoDb Client instance.
531+
*
532+
* @return DynamoDbClient
533+
*/
534+
public function getClient()
535+
{
536+
return $this->dynamo;
537+
}
528538
}

tests/Integration/Cache/DynamoDbStoreTest.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Aws\DynamoDb\DynamoDbClient;
66
use Aws\Exception\AwsException;
7+
use Illuminate\Contracts\Cache\Repository;
78
use Illuminate\Support\Facades\Cache;
89
use Illuminate\Support\Str;
910
use Orchestra\Testbench\TestCase;
@@ -85,7 +86,7 @@ protected function getEnvironmentSetUp($app)
8586
$config = $app['config']->get('cache.stores.dynamodb');
8687

8788
/** @var \Aws\DynamoDb\DynamoDbClient $client */
88-
$client = $app['cache.dynamodb.client'];
89+
$client = $app->make(Repository::class)->getStore()->getClient();
8990

9091
if ($this->dynamoTableExists($client, $config['table'])) {
9192
return;

0 commit comments

Comments
 (0)