Last active
August 29, 2015 14:07
-
-
Save stojg/49e78332d4b75b2c2f16 to your computer and use it in GitHub Desktop.
AWS Dynamodb Session for SilverStirpe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Aws\DynamoDb\DynamoDbClient; | |
use Aws\DynamoDb\Session\SessionHandler; | |
// DISCLAIMER: untested and should just be used as guideline | |
// | |
// Either fetches the required credentials from the ec2 server or requires to add them to a _ss_environment.php file | |
// AWS_REGION_NAME <- required | |
// AWS_DYNAMODB_SESSION_TABLE <- required | |
// AWS_SECRET_KEY | |
// AWS_DYNAMODB_SESSION_LIFETIME | |
$dynamoOptions = array('region' => AWS_REGION_NAME); | |
if(defined('AWS_ACCESS_KEY') && defined('AWS_SECRET_KEY')) { | |
$dynamoOptions['key'] = AWS_ACCESS_KEY; | |
$dynamoOptions['secret'] = AWS_SECRET_KEY; | |
} else { | |
// cache credentials when IAM fetches the credentials from EC2 metadata service | |
// this will use doctrine/cache (included via composer) to do the actual caching into the filesystem | |
// http://docs.aws.amazon.com/aws-sdk-php/guide/latest/performance.html#cache-instance-profile-credentials | |
$dynamoOptions['credentials.cache'] = true; | |
} | |
if(defined('AWS_DYNAMODB_SESSION_LIFETIME')) { | |
$dynamoOptions['session_lifetime'] = AWS_DYNAMODB_SESSION_LIFETIME; | |
} | |
if(!isset($dynamoOptions['session_lifetime'])) { | |
$timeout = Config::inst()->get('Session', 'timeout'); | |
if($timeout != null) { | |
$dynamoOptions['session_lifetime'] = $timeout; | |
} | |
} | |
$client = DynamoDbClient::factory($dynamoOptions); | |
$handler = SessionHandler::factory(array( | |
'dynamodb_client' => $client, | |
'table_name' => AWS_DYNAMODB_SESSION_TABLE | |
)); | |
$handler->register(); | |
// The garbage collection command below needs to be run at regular interval, see aws docs for it | |
// $handler->garbageCollect() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment