Skip to content

Commit a20c9e5

Browse files
committed
Dynamically create table
1 parent 2a50ccf commit a20c9e5

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

src/Illuminate/Cache/CacheManager.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace Illuminate\Cache;
44

5-
use Aws\DynamoDb\DynamoDbClient;
65
use Closure;
76
use Illuminate\Contracts\Cache\Factory as FactoryContract;
87
use Illuminate\Contracts\Cache\Store;
98
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
10-
use Illuminate\Support\Arr;
119
use InvalidArgumentException;
1210

1311
/**
@@ -226,21 +224,9 @@ protected function createDatabaseDriver(array $config)
226224
*/
227225
protected function createDynamodbDriver(array $config)
228226
{
229-
$dynamoConfig = [
230-
'region' => $config['region'],
231-
'version' => 'latest',
232-
'endpoint' => $config['endpoint'] ?? null,
233-
];
234-
235-
if ($config['key'] && $config['secret']) {
236-
$dynamoConfig['credentials'] = Arr::only(
237-
$config, ['key', 'secret', 'token']
238-
);
239-
}
240-
241227
return $this->repository(
242228
new DynamoDbStore(
243-
new DynamoDbClient($dynamoConfig),
229+
$this->app['cache.dynamodb.client'],
244230
$config['table'],
245231
$config['attributes']['key'] ?? 'key',
246232
$config['attributes']['value'] ?? 'value',

src/Illuminate/Cache/CacheServiceProvider.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Illuminate\Cache;
44

5+
use Aws\DynamoDb\DynamoDbClient;
56
use Illuminate\Contracts\Support\DeferrableProvider;
7+
use Illuminate\Support\Arr;
68
use Illuminate\Support\ServiceProvider;
79
use Symfony\Component\Cache\Adapter\Psr16Adapter;
810

@@ -30,6 +32,24 @@ public function register()
3032
$this->app->singleton('memcached.connector', function () {
3133
return new MemcachedConnector;
3234
});
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+
});
3353
}
3454

3555
/**

tests/Integration/Cache/DynamoDbStoreTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,38 @@ public function testLocksCanBeAcquired()
7575
protected function getEnvironmentSetUp($app)
7676
{
7777
$app['config']->set('cache.default', 'dynamodb');
78+
79+
$config = $app['config']->get('cache.stores.dynamodb');
80+
81+
/** @var \Aws\DynamoDb\DynamoDbClient $client */
82+
$client = $app['cache.dynamodb.client'];
83+
84+
$client->createTable([
85+
'TableName' => $config['table'],
86+
'KeySchema' => [
87+
[
88+
'AttributeName' => $config['attributes']['key'] ?? 'key',
89+
'KeyType' => 'hash',
90+
],
91+
],
92+
'AttributeDefinitions' => [
93+
[
94+
'AttributeName' => $config['attributes']['key'] ?? 'key',
95+
'AttributeType' => 'S',
96+
],
97+
[
98+
'AttributeName' => $config['attributes']['value'] ?? 'value',
99+
'AttributeType' => 'S',
100+
],
101+
[
102+
'AttributeName' => $config['attributes']['expiration'] ?? 'expires_at',
103+
'AttributeType' => 'S',
104+
],
105+
],
106+
'ProvisionedThroughput' => [
107+
'ReadCapacityUnits' => 1,
108+
'WriteCapacityUnits' => 1,
109+
],
110+
]);
78111
}
79112
}

0 commit comments

Comments
 (0)