Skip to content

Commit

Permalink
integrate dynamodb cache and session driver
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 10, 2019
1 parent 8071e06 commit ea745e1
Show file tree
Hide file tree
Showing 5 changed files with 664 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use InvalidArgumentException;
use Aws\DynamoDb\DynamoDbClient;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Contracts\Cache\Factory as FactoryContract;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
Expand Down Expand Up @@ -216,6 +217,35 @@ protected function createDatabaseDriver(array $config)
);
}

/**
* Create an instance of the DynamoDB cache driver.
*
* @param array $config
* @return \Illuminate\Cache\DynamoDbStore
*/
protected function createDynamodbDriver(array $config)
{
$dynamo = DynamoDbClient::factory([
'region' => $config['region'],
'version' => 'latest',
'credentials' => array_filter([
'key' => $config['key'] ?? null,
'secret' => $config['secret'] ?? null,
'token' => $config['token'] ?? null,
]),
]);

return $this->repository(
new DynamoDbStore(
$dynamo,
$config['table'],
$config['attributes']['key'] ?? 'key',
$config['attributes']['value'] ?? 'value',
$config['attributes']['expiration'] ?? 'expires_at'
)
);
}

/**
* Create a new cache repository with the given implementation.
*
Expand Down
53 changes: 53 additions & 0 deletions src/Illuminate/Cache/DynamoDbLock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Illuminate\Cache;

use Illuminate\Cache\Lock;
use Aws\DynamoDb\DynamoDbClient;

class DynamoDbLock extends Lock
{
/**
* The DynamoDB client instance.
*
* @var \Illuminate\Cache\DynamoDbStore
*/
protected $dynamo;

/**
* Create a new lock instance.
*
* @param \Illuminate\Cache\DynamoDbStore $dynamo
* @param string $name
* @param int $seconds
* @return void
*/
public function __construct(DynamoDbStore $dynamo, $name, $seconds)
{
parent::__construct($name, $seconds);

$this->dynamo = $dynamo;
}

/**
* Attempt to acquire the lock.
*
* @return bool
*/
public function acquire()
{
return $this->dynamo->add(
$this->name, 1, $this->seconds / 60
);
}

/**
* Release the lock.
*
* @return void
*/
public function release()
{
$this->dynamo->forget($this->name);
}
}
Loading

0 comments on commit ea745e1

Please sign in to comment.