Skip to content

Commit

Permalink
Introduced Garbage Collector Service
Browse files Browse the repository at this point in the history
  • Loading branch information
GBeushausen committed Jan 13, 2018
1 parent f6a95e2 commit 6458f19
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'];
64 changes: 53 additions & 11 deletions src/DynamoDbSession/DynamoDbSessionServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
<?php

namespace DynamoDbSession;
/*
* This file is part of Silex-DynamoDb-Session-Provider.
*
* Copyright (c) 2018 Gunnar Beushausen
* https://www.gunnar-beushausen.de
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DynamoDbSession;

use Aws\DynamoDb\SessionHandler;
use Aws\Sdk;
Expand All @@ -14,25 +38,43 @@ public function register(Container $app)
{
/** @noinspection PhpParamsInspection */
$app['session.storage.handler'] = function () use ($app) {
$config = $app['session.dynamodb.options'];
if (!array_key_exists('dynamodb_client', $config)) {
$config['dynamodb_client'] = $this->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();
}
}
}
}

0 comments on commit 6458f19

Please sign in to comment.