diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2022-04-19 17:00:42 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2022-04-19 17:19:14 -0500 |
commit | d19776ca25db85912529182daca5f03ba38f60a8 (patch) | |
tree | ded0b1c97bd1bfa6ad78d4cd9739f6a0acb5050e | |
parent | 108eea9c84ff650d07618b96d1a82b0be11ff946 (diff) | |
download | requests-cache-d19776ca25db85912529182daca5f03ba38f60a8.tar.gz |
Create default table in on-demand mode instead of provisioned
-rw-r--r-- | HISTORY.md | 4 | ||||
-rw-r--r-- | requests_cache/backends/dynamodb.py | 22 |
2 files changed, 13 insertions, 13 deletions
@@ -24,6 +24,8 @@ * MongoDB: * Store responses in plain (human-readable) document format instead of fully serialized binary * Add optional integration with MongoDB TTL to improve performance for removing expired responses +* DynamoDB: + * Create default table in on-demand mode instead of provisioned * SQLite, Redis, MongoDB, and GridFS: Close open database connections when `CachedSession` is used as a contextmanager, or if `CachedSession.close()` is called **Type hints:** @@ -34,7 +36,7 @@ * `from_cache` * `is_expired` * `OriginalResponse.cache_key` and `expires` will be populated for any new response that was written to the cache -* Add return type hints for all request wrapper methods (`CachedSession.get()`, `head()`, etc.) +* Add request wrapper methods with return type hints for all HTTP methods (`CachedSession.get()`, `head()`, etc.) **Request Matching & Filtering:** * Add serializer name to cache keys to avoid errors due to switching serializers diff --git a/requests_cache/backends/dynamodb.py b/requests_cache/backends/dynamodb.py index c96c20f..17721f6 100644 --- a/requests_cache/backends/dynamodb.py +++ b/requests_cache/backends/dynamodb.py @@ -53,11 +53,9 @@ class DynamoDbDict(BaseStorage): def __init__( self, - table_name, - namespace='http_cache', - connection=None, - read_capacity_units=1, - write_capacity_units=1, + table_name: str, + namespace: str = 'http_cache', + connection: ServiceResource = None, **kwargs, ): super().__init__(**kwargs) @@ -65,7 +63,12 @@ class DynamoDbDict(BaseStorage): self.connection = connection or boto3.resource('dynamodb', **connection_kwargs) self.namespace = namespace - # TODO: Create default table as on-demand instead of provisioned? + self._create_table(table_name) + self._table = self.connection.Table(table_name) + self._table.wait_until_exists() + + def _create_table(self, table_name: str): + """Create a default table if one does not already exist""" try: self.connection.create_table( AttributeDefinitions=[ @@ -83,15 +86,10 @@ class DynamoDbDict(BaseStorage): {'AttributeName': 'namespace', 'KeyType': 'HASH'}, {'AttributeName': 'key', 'KeyType': 'RANGE'}, ], - ProvisionedThroughput={ - 'ReadCapacityUnits': read_capacity_units, - 'WriteCapacityUnits': write_capacity_units, - }, + BillingMode="PAY_PER_REQUEST", ) except ClientError: pass - self._table = self.connection.Table(table_name) - self._table.wait_until_exists() def composite_key(self, key: str) -> Dict[str, str]: return {'namespace': self.namespace, 'key': str(key)} |