Skip to content

Commit

Permalink
Dynamically create table
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Mar 25, 2021
1 parent 2a50ccf commit a20c9e5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
16 changes: 1 addition & 15 deletions src/Illuminate/Cache/CacheManager.php
Expand Up @@ -2,12 +2,10 @@

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 @@ -226,21 +224,9 @@ protected function createDatabaseDriver(array $config)
*/
protected function createDynamodbDriver(array $config)
{
$dynamoConfig = [
'region' => $config['region'],
'version' => 'latest',
'endpoint' => $config['endpoint'] ?? null,
];

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

return $this->repository(
new DynamoDbStore(
new DynamoDbClient($dynamoConfig),
$this->app['cache.dynamodb.client'],
$config['table'],
$config['attributes']['key'] ?? 'key',
$config['attributes']['value'] ?? 'value',
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Cache/CacheServiceProvider.php
Expand Up @@ -2,7 +2,9 @@

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 @@ -30,6 +32,24 @@ 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 Down
33 changes: 33 additions & 0 deletions tests/Integration/Cache/DynamoDbStoreTest.php
Expand Up @@ -75,5 +75,38 @@ public function testLocksCanBeAcquired()
protected function getEnvironmentSetUp($app)
{
$app['config']->set('cache.default', 'dynamodb');

$config = $app['config']->get('cache.stores.dynamodb');

/** @var \Aws\DynamoDb\DynamoDbClient $client */
$client = $app['cache.dynamodb.client'];

$client->createTable([
'TableName' => $config['table'],
'KeySchema' => [
[
'AttributeName' => $config['attributes']['key'] ?? 'key',
'KeyType' => 'hash',
],
],
'AttributeDefinitions' => [
[
'AttributeName' => $config['attributes']['key'] ?? 'key',
'AttributeType' => 'S',
],
[
'AttributeName' => $config['attributes']['value'] ?? 'value',
'AttributeType' => 'S',
],
[
'AttributeName' => $config['attributes']['expiration'] ?? 'expires_at',
'AttributeType' => 'S',
],
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 1,
'WriteCapacityUnits' => 1,
],
]);
}
}

0 comments on commit a20c9e5

Please sign in to comment.