DynamoDB#

../_images/dynamodb.png

DynamoDB is a NoSQL document database hosted on Amazon Web Services. In terms of features and use cases, it is roughly comparable to MongoDB and other NoSQL databases. It is an especially good fit for serverless applications running on AWS Lambda.

Warning

DynamoDB binary item sizes are limited to 400KB. If you need to cache larger responses, consider using a different backend.

Creating Tables#

Tables will be automatically created if they don’t already exist. This is convienient if you just want to quickly test out DynamoDB as a cache backend, but in a production environment you will likely want to create the tables yourself, for example with CloudFormation or Terraform. Here are the details you’ll need:

  • Tables: two tables, named responses and redirects

  • Partition key (aka namespace): namespace

  • Range key (aka sort key): key

  • Attributes: namespace (string) and key (string)

Connection Options#

The DynamoDB backend accepts any keyword arguments for boto3.session.Session.resource():

>>> backend = DynamoDbCache(region_name='us-west-2')
>>> session = CachedSession('http_cache', backend=backend)

API Reference#

DynamoDbCache

DynamoDB cache backend

DynamoDbDict

A dictionary-like interface for DynamoDB key-value store

class requests_cache.backends.dynamodb.DynamoDbCache(table_name='http_cache', connection=None, **kwargs)[source]#

Bases: requests_cache.backends.base.BaseCache

DynamoDB cache backend

Parameters
bulk_delete(keys)#

Remove multiple responses and their associated redirects from the cache

Parameters

keys (Iterable[str]) –

clear()#

Delete all items from the cache

contains(key=None, request=None, url=None)#

Check if the specified request is cached :type key: Optional[str] :param key: Check for a specific cache key :type request: Union[Request, PreparedRequest, CachedRequest, None] :param request: Check for a matching request, according to current request matching settings :type url: Optional[str] :param url: Check for a matching GET request with the specified URL

create_key(request=None, **kwargs)#

Create a normalized cache key from a request object

Parameters

request (Union[Request, PreparedRequest, CachedRequest, None]) –

Return type

str

delete(*keys, expired=False, invalid=False, requests=None, urls=None)#

Remove responses from the cache according one or more conditions. :type keys: str :param keys: Remove responses with these cache keys :type expired: bool :param expired: Remove all expired responses :type invalid: bool :param invalid: Remove all invalid responses (that can’t be deserialized with current settings) :type requests: Optional[Iterable[Union[Request, PreparedRequest, CachedRequest]]] :param requests: Remove matching responses, according to current request matching settings :type urls: Optional[Iterable[str]] :param urls: Remove matching GET requests for the specified URL(s)

delete_url(url, method='GET', **kwargs)#
Parameters
  • url (str) –

  • method (str) –

delete_urls(urls, method='GET', **kwargs)#
Parameters
filter(valid=True, expired=True, invalid=False)#

Get responses from the cache, with optional filters :type valid: bool :param valid: Include valid and unexpired responses; set to False to get only

expired/invalid/old responses

Parameters
  • expired (bool) – Include expired responses

  • invalid (bool) – Include invalid responses (as an empty CachedResponse)

Return type

Iterator[CachedResponse]

get_response(key, default=None)#

Retrieve a response from the cache, if it exists

Parameters
  • key (str) – Cache key for the response

  • default – Value to return if key is not in the cache

Return type

Optional[CachedResponse]

has_key(key)#
Parameters

key (str) –

Return type

bool

has_url(url, method='GET', **kwargs)#
Parameters
  • url (str) –

  • method (str) –

Return type

bool

keys(check_expiry=False)#
Parameters

check_expiry (bool) –

Return type

Iterator[str]

remove_expired_responses(expire_after=None)#
Parameters

expire_after (Union[None, int, float, str, datetime, timedelta]) –

reset_expiration(expire_after=None)#

Set a new expiration value on existing cache items :type expire_after: Union[None, int, float, str, datetime, timedelta] :param expire_after: New expiration value, relative to the current time

response_count(check_expiry=False)#
Parameters

check_expiry (bool) –

Return type

int

save_response(response, cache_key=None, expires=None)#

Save a response to the cache

Parameters
  • cache_key (Optional[str]) – Cache key for this response; will otherwise be generated based on request

  • response (Union[Response, CachedResponse]) – Response to save

  • expires (Optional[datetime]) – Absolute expiration time for this response

update(other)#

Update this cache with the contents of another cache

Parameters

other (BaseCache) –

property urls#

Get all URLs currently in the cache (excluding redirects)

Return type

Iterator[str]

values(check_expiry=False)#
Parameters

check_expiry (bool) –

Return type

Iterator[CachedResponse]

class requests_cache.backends.dynamodb.DynamoDbDict(table_name, namespace='http_cache', connection=None, read_capacity_units=1, write_capacity_units=1, **kwargs)[source]#

Bases: requests_cache.backends.base.BaseStorage

A dictionary-like interface for DynamoDB key-value store

Notes:
  • The actual table name on the Dynamodb server will be namespace:table_name

  • In order to deal with how DynamoDB stores data, all values are serialized.

Parameters
  • table_name – DynamoDB table name

  • namespace – Name of DynamoDB hash map

  • connectionDynamoDB Resource object to use instead of creating a new one

  • kwargs – Additional keyword arguments for resource()

bulk_delete(keys)[source]#

Delete multiple keys from the cache. Does not raise errors for missing keys.

Parameters

keys (Iterable[str]) –

clear() None.  Remove all items from D.[source]#
composite_key(key)[source]#
Parameters

key (str) –

Return type

Dict[str, str]

get(k[, d]) D[k] if k in D, else d.  d defaults to None.#
items() a set-like object providing a view on D's items#
keys() a set-like object providing a view on D's keys#
pop(k[, d]) v, remove specified key and return the corresponding value.#

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair#

as a 2-tuple; but raise KeyError if D is empty.

property serializer#
setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D#
update([E, ]**F) None.  Update D from mapping/iterable E and F.#

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values#