From 6458f19bc50d3864b66395fed90bd9caa92738a5 Mon Sep 17 00:00:00 2001 From: Gunnar Beushausen Date: Sat, 13 Jan 2018 09:38:39 +0100 Subject: [PATCH] Introduced Garbage Collector Service --- README.md | 7 +- .../DynamoDbSessionServiceProvider.php | 64 +++++++++++++++---- 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 5a5187e..8e95549 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,8 @@ Typically PHP Sessions are not scalable. As soon as a load balanced / multi serv a better solution. Amazons DynamoDB NoSQL storage engine is the fast, easy and scalable solution for that problem. Just implement it in your project ( see below ). You also need a solution to get rid of stale sessions from the table. Since -deleting many old sessions can take a lot of write capacity units, it's best to write an AWS Lambda function which does that -overnight. +deleting many old sessions can take a lot of write capacity units, it's best to configure a cron job and run the Garbage +Collector over night. Gunnar Beushausen @@ -42,3 +42,6 @@ Basic Usage $app->register(new SessionServiceProvider()); $app->register(new DynamoDbSessionServiceProvider()); + + //To run the Garbage Collector in order to remove stale and old sessions from the table, run the following code: + $app['session.dynamodb.garbagecollect']; diff --git a/src/DynamoDbSession/DynamoDbSessionServiceProvider.php b/src/DynamoDbSession/DynamoDbSessionServiceProvider.php index 31b4862..8dd9367 100644 --- a/src/DynamoDbSession/DynamoDbSessionServiceProvider.php +++ b/src/DynamoDbSession/DynamoDbSessionServiceProvider.php @@ -1,7 +1,31 @@ getDynamoDbClient($app['AwsSdk']); - } - - $sessionHandler = SessionHandler::fromClient($config['dynamodb_client'], [ + $ddbClient = $this->getDynamoDbClient($app); + $sessionHandler = SessionHandler::fromClient($ddbClient, [ 'table_name' => $app['session.dynamodb.options']['table_name'], ]); $sessionHandler->register(); return $sessionHandler; }; + + $app['session.dynamodb.garbagecollect'] = function() use ($app) { + $ddbClient = $this->getDynamoDbClient($app); + $sessionHandler = SessionHandler::fromClient($ddbClient, [ + 'table_name' => $app['session.dynamodb.options']['table_name'], + 'batch_config' => [ + 'before' => function ($command) use ($app) { + //Deleting many small session rows from the DynamoDB table can take a huge amount of write capacity. + //We can configure a delay between each delete operation to save write capacity Units + $command['@http']['delay'] = isset($app['session.dynamodb.options']['delay']) ? $app['session.dynamodb.options']['delay'] : 500; + } + ] + ]); + + $sessionHandler->garbageCollect(); + }; } /** - * @param Sdk $aws + * @param Container $app * @return DynamoDbClient */ - private function getDynamoDbClient(Sdk $aws) + private function getDynamoDbClient(Container $app) { - return $aws->createDynamoDb(); + $config = $app['session.dynamodb.options']; + if (array_key_exists('dynamodb_client', $config)) { + return $config['dynamodb_client']; + } + else { + return $app['AwsSdk']->createDynamoDb(); + } } -} \ No newline at end of file +}