DynamoDb Backend

class cachelib.dynamodb.DynamoDbCache(table_name='python-cache', default_timeout=300, key_field='cache_key', expiration_time_field='expiration_time', key_prefix=None, **kwargs)

Bases: BaseCache

Implementation of cachelib.BaseCache that uses an AWS DynamoDb table as the backend.

Your server process will require dynamodb:GetItem and dynamodb:PutItem IAM permissions on the cache table.

Limitations: DynamoDB table items are limited to 400 KB in size. Since this class stores cached items in a table, the max size of a cache entry will be slightly less than 400 KB, since the cache key and expiration time fields are also part of the item.

Parameters:
  • table_name (str | None) – The name of the DynamoDB table to use

  • default_timeout (int) – Set the timeout in seconds after which cache entries expire

  • key_field (str | None) – The name of the hash_key attribute in the DynamoDb table. This must be a string attribute.

  • expiration_time_field (str | None) – The name of the table attribute to store the expiration time in. This will be an int attribute. The timestamp will be stored as seconds past the epoch. If you configure this as the TTL field, then DynamoDB will automatically delete expired entries.

  • key_prefix (str | None) – A prefix that should be added to all keys.

  • kwargs (Any) –

add(key, value, timeout=None)

Works like set() but does not overwrite the values of already existing keys.

Parameters:
  • key (str) – the key to set

  • value (Any) – the value for the key

  • timeout (int | None) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.

Returns:

Same as set(), but also False for already existing keys.

Return type:

boolean

clear()

Clears the cache. Keep in mind that not all caches support completely clearing the cache.

Returns:

Whether the cache has been cleared.

Return type:

boolean

delete(key)

Deletes an item from the cache. This is a no-op if the item doesn’t exist

Parameters:

key (str) – Key of the item to delete.

Returns:

True if the key existed and was deleted

Return type:

bool

get(key)

Get a cache item

Parameters:

key (str) – The cache key of the item to fetch

Returns:

cache value if not expired, else None

Return type:

Any

has(key)

Checks if a key exists in the cache without returning it. This is a cheap operation that bypasses loading the actual data on the backend.

Parameters:

key (str) – the key to check

Return type:

bool

serializer = <cachelib.serializers.DynamoDbSerializer object>
set(key, value, timeout=None)

Add a new key/value to the cache (overwrites value, if key already exists in the cache).

Parameters:
  • key (str) – the key to set

  • value (Any) – the value for the key

  • timeout (int | None) – the cache timeout for the key in seconds (if not specified, it uses the default timeout). A timeout of 0 indicates that the cache never expires.

Returns:

True if key has been updated, False for backend errors. Pickling errors, however, will raise a subclass of pickle.PickleError.

Return type:

boolean