public function deleteExpired() { // Create a Scan iterator for finding expired session items $scan = $this->client->getPaginator('Scan', ['TableName' => $this->config['table_name'], 'AttributesToGet' => [$this->config['hash_key']], 'ScanFilter' => ['expires' => ['ComparisonOperator' => 'LT', 'AttributeValueList' => [['N' => (string) time()]]], 'lock' => ['ComparisonOperator' => 'NULL']]]); // Create a WriteRequestBatch for deleting the expired items $batch = new WriteRequestBatch($this->client, $this->config['batch_config']); // Perform Scan and BatchWriteItem (delete) operations as needed foreach ($scan->search('Items') as $item) { $batch->delete([$this->config['hash_key'] => $item[$this->config['hash_key']]], $this->config['table_name']); } // Delete any remaining items that were not auto-flushed $batch->flush(); }
/** * Removes finished ( done or error ) jobs from a table. * * @param int $ttl The time in - in milliseconds - to leave a finished job before removing it. * @return int */ public function cleanupTable($ttl = 0) { // Create a Scan iterator for finding finished jobs $scan = $this->client->getPaginator('Scan', ['TableName' => $this->config['table_name'], 'ProjectionExpression' => 'id, #state', 'ExpressionAttributeNames' => ['#state' => 'state', '#updated' => 'updated'], 'ExpressionAttributeValues' => [':done' => ['S' => AbstractJob::STATE_DONE], ':error' => ['S' => AbstractJob::STATE_ERROR], ':notUpdatedSince' => ['N' => (string) (round(microtime(true) * 1000) - $ttl)]], 'FilterExpression' => '#state IN (:done, :error) AND #updated < :notUpdatedSince']); //--- // Create a WriteRequestBatch for deleting the expired jobs $batch = new WriteRequestBatch($this->client, ['error' => function ($v) { if ($v instanceof Exception) { throw $v; } }]); //--- $deletedJobs = 0; foreach ($scan->search('Items') as $item) { $batch->delete(['id' => $item['id']], $this->config['table_name']); $deletedJobs++; } // Delete any remaining jobs that were not auto-flushed $batch->flush(); //--- return $deletedJobs; }
/** * {@inheritDoc} */ public function batchWriteItemMultipleAndRetry(AwsDynamoDbRequest $request) { $batch = new WriteRequestBatch($this->client); /* Iterate every table. */ foreach ($request->getRequestItems() as $table => $items) { /* Iterates every item. */ /** @var AwsDynamoDbRequest $item */ foreach ($items as $item) { empty($item->getPutRequest()->toArray()) ? $batch->delete($item->getDeleteRequest()->getKey()->toArray(), $table) : $batch->put($item->getPutRequest()->getItem()->toArray(), $table); } } try { $batch->flush(true); } catch (AwsException $e) { /* Return an error response. */ return new AwsDynamoDbResponse(null, $e); } return new AwsDynamoDbResponse(null, null); }