-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4313c2
commit 99d99a9
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
use WindowsAzure\Common\ServicesBuilder; | ||
use WindowsAzure\Common\ServiceException; | ||
use WindowsAzure\Table\Models\Entity; | ||
use WindowsAzure\Table\Models\EdmType; | ||
use WindowsAzure\Table\Models\TableServiceOptions; | ||
|
||
class AzureTableSessionHandler implements SessionHandlerInterface | ||
{ | ||
protected $tableProxy; | ||
protected $containerName; | ||
protected $partitionName; | ||
|
||
public function __construct($storageAccount, $storageKey, $containerName = 'phpsess', $partitionName = 'sessions') | ||
{ | ||
$connectionString = "DefaultEndpointsProtocol=https;" . | ||
"AccountName=" . $storageAccount . | ||
";AccountKey=" . $storageKey; | ||
$this->tableProxy = ServicesBuilder::getInstance()->createTableService($connectionString); | ||
$this->containerName = $containerName; | ||
$this->partitionName = $partitionName; | ||
|
||
register_shutdown_function('session_write_close'); | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
session_write_close(); | ||
} | ||
|
||
public function open($savePath, $sessionName) | ||
{ | ||
try { | ||
$this->tableProxy->getTable($this->containerName); | ||
} catch (ServiceException $e) { | ||
$this->tableProxy->createTable($this->containerName); | ||
} | ||
return true; | ||
} | ||
|
||
public function close() | ||
{ | ||
return true; | ||
} | ||
|
||
public function read($id) | ||
{ | ||
try { | ||
$result = $this->tableProxy->getEntity($this->containerName, $this->partitionName, $id); | ||
$entity = $result->getEntity(); | ||
return unserialize(base64_decode($entity->getPropertyValue('data'))); | ||
} catch (ServiceException $e) { | ||
return ''; | ||
} | ||
} | ||
|
||
public function write($id, $data) | ||
{ | ||
$serializedData = base64_encode(serialize($data)); | ||
|
||
try { | ||
$result = $this->tableProxy->getEntity($this->containerName, $this->partitionName, $id); | ||
$entity = $result->getEntity(); | ||
$entity->setPropertyValue('data', $serializedData); | ||
$entity->setPropertyValue('createdat', time()); | ||
$this->tableProxy->updateEntity($this->containerName, $entity); | ||
} catch (ServiceException $e) { | ||
$entity = new Entity(); | ||
$entity->setPartitionKey($this->partitionName); | ||
$entity->setRowKey($id); | ||
$entity->addProperty('data', EdmType::STRING, $serializedData); | ||
$entity->addProperty('createdat', EdmType::INT32, time()); | ||
$this->tableProxy->insertEntity($this->containerName, $entity); | ||
} | ||
return true; | ||
} | ||
|
||
public function gc($lifetime) | ||
{ | ||
$filter = "PartitionKey eq '" . $this->partitionName . "' and createdat lt " . intval(time() - $lifetime); | ||
try { | ||
$result = $this->tableProxy->queryEntities($this->containerName, $filter); | ||
$entities = $result->getEntities(); | ||
foreach ($entities as $entity) { | ||
$this->tableProxy->deleteEntity($this->containerName, $this->partitionName, $entity->getRowKey()); | ||
} | ||
return true; | ||
} catch (ServiceException $e) { | ||
return false; | ||
} | ||
} | ||
|
||
public function destroy($id) | ||
{ | ||
try { | ||
$this->tableProxy->deleteEntity($this->containerName, $this->partitionName, $id); | ||
return true; | ||
} catch (ServiceException $e) { | ||
return false; | ||
} | ||
} | ||
} |