Exception
11 months ago
BinaryValue.php
11 months ago
DynamoDbClient.php
11 months ago
LockingSessionConnection.php
11 months ago
Marshaler.php
11 months ago
NumberValue.php
11 months ago
SessionConnectionConfigTrait.php
11 months ago
SessionConnectionInterface.php
11 months ago
SessionHandler.php
11 months ago
SetValue.php
11 months ago
StandardSessionConnection.php
11 months ago
WriteRequestBatch.php
11 months ago
StandardSessionConnection.php
149 lines
| 1 | <?php |
| 2 | namespace Aws\DynamoDb; |
| 3 | |
| 4 | use Aws\DynamoDb\Exception\DynamoDbException; |
| 5 | |
| 6 | /** |
| 7 | * The standard connection performs the read and write operations to DynamoDB. |
| 8 | */ |
| 9 | class StandardSessionConnection implements SessionConnectionInterface |
| 10 | { |
| 11 | use SessionConnectionConfigTrait; |
| 12 | |
| 13 | /** @var DynamoDbClient The DynamoDB client */ |
| 14 | protected $client; |
| 15 | |
| 16 | /** |
| 17 | * @param DynamoDbClient $client DynamoDB client |
| 18 | * @param array $config Session handler config |
| 19 | */ |
| 20 | public function __construct(DynamoDbClient $client, array $config = []) |
| 21 | { |
| 22 | $this->client = $client; |
| 23 | $this->initConfig($config); |
| 24 | } |
| 25 | |
| 26 | public function read($id) |
| 27 | { |
| 28 | $item = []; |
| 29 | try { |
| 30 | // Execute a GetItem command to retrieve the item. |
| 31 | $result = $this->client->getItem([ |
| 32 | 'TableName' => $this->getTableName(), |
| 33 | 'Key' => $this->formatKey($id), |
| 34 | 'ConsistentRead' => $this->isConsistentRead(), |
| 35 | ]); |
| 36 | |
| 37 | // Get the item values |
| 38 | $result = isset($result['Item']) ? $result['Item'] : []; |
| 39 | foreach ($result as $key => $value) { |
| 40 | $item[$key] = current($value); |
| 41 | } |
| 42 | } catch (DynamoDbException $e) { |
| 43 | // Could not retrieve item, so return nothing. |
| 44 | } |
| 45 | |
| 46 | return $item; |
| 47 | } |
| 48 | |
| 49 | public function write($id, $data, $isChanged) |
| 50 | { |
| 51 | // Prepare the attributes |
| 52 | $expires = time() + $this->getSessionLifetime(); |
| 53 | $attributes = [ |
| 54 | $this->getSessionLifetimeAttribute() => ['Value' => ['N' => (string) $expires]], |
| 55 | 'lock' => ['Action' => 'DELETE'], |
| 56 | ]; |
| 57 | if ($isChanged) { |
| 58 | if ($data != '') { |
| 59 | $type = $this->getDataAttributeType(); |
| 60 | if ($type == 'binary') { |
| 61 | $attributes[$this->getDataAttribute()] = ['Value' => ['B' => $data]]; |
| 62 | } else { |
| 63 | $attributes[$this->getDataAttribute()] = ['Value' => ['S' => $data]]; |
| 64 | } |
| 65 | |
| 66 | } else { |
| 67 | $attributes[$this->getDataAttribute()] = ['Action' => 'DELETE']; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | // Perform the UpdateItem command |
| 72 | try { |
| 73 | return (bool) $this->client->updateItem([ |
| 74 | 'TableName' => $this->getTableName(), |
| 75 | 'Key' => $this->formatKey($id), |
| 76 | 'AttributeUpdates' => $attributes, |
| 77 | ]); |
| 78 | } catch (DynamoDbException $e) { |
| 79 | return $this->triggerError("Error writing session $id: {$e->getMessage()}"); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public function delete($id) |
| 84 | { |
| 85 | try { |
| 86 | return (bool) $this->client->deleteItem([ |
| 87 | 'TableName' => $this->getTableName(), |
| 88 | 'Key' => $this->formatKey($id), |
| 89 | ]); |
| 90 | } catch (DynamoDbException $e) { |
| 91 | return $this->triggerError("Error deleting session $id: {$e->getMessage()}"); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | public function deleteExpired() |
| 96 | { |
| 97 | // Create a Scan iterator for finding expired session items |
| 98 | $scan = $this->client->getPaginator('Scan', [ |
| 99 | 'TableName' => $this->getTableName(), |
| 100 | 'AttributesToGet' => [$this->getHashKey()], |
| 101 | 'ScanFilter' => [ |
| 102 | $this->getSessionLifetimeAttribute() => [ |
| 103 | 'ComparisonOperator' => 'LT', |
| 104 | 'AttributeValueList' => [['N' => (string) time()]], |
| 105 | ], |
| 106 | 'lock' => [ |
| 107 | 'ComparisonOperator' => 'NULL', |
| 108 | ] |
| 109 | ], |
| 110 | ]); |
| 111 | |
| 112 | // Create a WriteRequestBatch for deleting the expired items |
| 113 | $batch = new WriteRequestBatch($this->client, $this->getBatchConfig()); |
| 114 | |
| 115 | // Perform Scan and BatchWriteItem (delete) operations as needed |
| 116 | foreach ($scan->search('Items') as $item) { |
| 117 | $batch->delete( |
| 118 | [$this->getHashKey() => $item[$this->getHashKey()]], |
| 119 | $this->getTableName() |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | // Delete any remaining items that were not auto-flushed |
| 124 | $batch->flush(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param string $key |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | protected function formatKey($key) |
| 133 | { |
| 134 | return [$this->getHashKey() => ['S' => $key]]; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * @param string $error |
| 139 | * |
| 140 | * @return bool |
| 141 | */ |
| 142 | protected function triggerError($error) |
| 143 | { |
| 144 | trigger_error($error, E_USER_WARNING); |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | } |
| 149 |