Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix Cache store with a name other than 'dynamodb' #37145

Merged
merged 2 commits into from Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/Illuminate/Cache/CacheManager.php
Expand Up @@ -2,10 +2,12 @@

namespace Illuminate\Cache;

use Aws\DynamoDb\DynamoDbClient;
use Closure;
use Illuminate\Contracts\Cache\Factory as FactoryContract;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Support\Arr;
use InvalidArgumentException;

/**
Expand Down Expand Up @@ -224,9 +226,11 @@ protected function createDatabaseDriver(array $config)
*/
protected function createDynamodbDriver(array $config)
{
$client = $this->newDynamodbClient($config);

return $this->repository(
new DynamoDbStore(
$this->app['cache.dynamodb.client'],
$client,
$config['table'],
$config['attributes']['key'] ?? 'key',
$config['attributes']['value'] ?? 'value',
Expand All @@ -236,6 +240,28 @@ protected function createDynamodbDriver(array $config)
);
}

/**
* Create new DynamoDb Client instance.
*
* @return DynamoDbClient
*/
protected function newDynamodbClient(array $config)
{
$dynamoConfig = [
'region' => $config['region'],
'version' => 'latest',
'endpoint' => $config['endpoint'] ?? null,
];

if (isset($config['key']) && isset($config['secret'])) {
$dynamoConfig['credentials'] = Arr::only(
$config, ['key', 'secret', 'token']
);
}

return new DynamoDbClient($dynamoConfig);
}

/**
* Create a new cache repository with the given implementation.
*
Expand Down
22 changes: 1 addition & 21 deletions src/Illuminate/Cache/CacheServiceProvider.php
Expand Up @@ -2,9 +2,7 @@

namespace Illuminate\Cache;

use Aws\DynamoDb\DynamoDbClient;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Cache\Adapter\Psr16Adapter;

Expand Down Expand Up @@ -32,24 +30,6 @@ public function register()
$this->app->singleton('memcached.connector', function () {
return new MemcachedConnector;
});

$this->app->singleton('cache.dynamodb.client', function ($app) {
$config = $app['config']->get('cache.stores.dynamodb');

$dynamoConfig = [
'region' => $config['region'],
'version' => 'latest',
'endpoint' => $config['endpoint'] ?? null,
];

if ($config['key'] && $config['secret']) {
$dynamoConfig['credentials'] = Arr::only(
$config, ['key', 'secret', 'token']
);
}

return new DynamoDbClient($dynamoConfig);
});
}

/**
Expand All @@ -60,7 +40,7 @@ public function register()
public function provides()
{
return [
'cache', 'cache.store', 'cache.psr6', 'memcached.connector', 'cache.dynamodb.client',
'cache', 'cache.store', 'cache.psr6', 'memcached.connector',
];
}
}
10 changes: 10 additions & 0 deletions src/Illuminate/Cache/DynamoDbStore.php
Expand Up @@ -525,4 +525,14 @@ public function setPrefix($prefix)
{
$this->prefix = ! empty($prefix) ? $prefix.':' : '';
}

/**
* Get the DynamoDb Client instance.
*
* @return DynamoDbClient
*/
public function getClient()
{
return $this->dynamo;
}
}
3 changes: 2 additions & 1 deletion tests/Integration/Cache/DynamoDbStoreTest.php
Expand Up @@ -4,6 +4,7 @@

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

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

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