DynamoDB Cache Engine
AsyncAws.DynamoDb stores cache entries in a DynamoDB table. Configure it in config/bootstrap.php (legacy layout: app/Config/bootstrap.php) or config/core.php:
php
Cache::config('default', [
'engine' => 'AsyncAws.DynamoDb',
'table' => 'DYNAMODB_TABLE_NAME',
'prefix' => 'myapp_',
'duration' => '+1 hours',
]);The engine has the settings of every cache engine (prefix, duration, groups, ...) and these:
| Setting | Default | Description |
|---|---|---|
table | (required) | Name of the table. Cache::config() throws a CacheException when it is missing. |
keyAttribute | id | Name of the string partition key. |
valueAttribute | value | Name of the attribute storing the value. |
expiresAttribute | expires | Name of the attribute storing the expiration time (Unix timestamp), used by Time to Live. |
consistentRead | false | Use strongly consistent reads. |
clientConfig | [] | Options merged into the AsyncAws configuration for this cache, e.g. ['region' => 'us-east-1']. |
client | null | An AsyncAws\DynamoDb\DynamoDbClient to use instead of creating one. |
- Integers are stored as numbers, so
Cache::increment()andCache::decrement()are atomic. LikeMemcachedEngine, they fail (returnfalse) when the key does not exist or has expired. Other values are serialized and stored as binary. - A
durationof0writes items that never expire. - Expired items are never returned. Enable Time to Live on the table with the
expiresattribute so that DynamoDB removes them;gc()does nothing. Cache::clear()scans the whole table and deletes the items whose key starts with theprefixof the configuration (only the expired ones withCache::clear(true)). A scan reads, and is charged for, every item of the table, so give each application its ownprefix, or its own table, and do not call it often.- Groups work like those of
RedisEngine: each group has a version number stored in the table, andCache::clearGroup()increments it. The old entries stay in the table until they expire. - An item is limited to 400 KB. A larger value is not written:
Cache::write()returnsfalseand an error is logged. - Failed requests are logged to the
cachescope and the operation returnsfalse.
The items are the same as those of the Aws.DynamoDb engine of pieceofcake2/aws, so both engines can use the same table.
Creating the Table
The table needs a string partition key named id (keyAttribute). createTable() creates it with on-demand capacity and enables Time to Live:
php
Cache::engine('default')->createTable();Or with the AWS CLI:
bash
aws dynamodb create-table --table-name DYNAMODB_TABLE_NAME \
--attribute-definitions AttributeName=id,AttributeType=S \
--key-schema AttributeName=id,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
aws dynamodb update-time-to-live --table-name DYNAMODB_TABLE_NAME \
--time-to-live-specification "Enabled=true,AttributeName=expires"