1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
from typing import Dict, Iterable
import boto3
from boto3.dynamodb.types import Binary
from boto3.resources.base import ServiceResource
from botocore.exceptions import ClientError
from . import BaseCache, BaseStorage, get_valid_kwargs
class DynamoDbCache(BaseCache):
"""DynamoDB cache backend
Args:
table_name: DynamoDb table name
namespace: Name of DynamoDb hash map
connection: :boto3:`DynamoDb Resource <services/dynamodb.html#DynamoDB.ServiceResource>`
object to use instead of creating a new one
kwargs: Additional keyword arguments for :py:meth:`~boto3.session.Session.resource`
"""
def __init__(self, table_name: str = 'http_cache', connection: ServiceResource = None, **kwargs):
super().__init__(**kwargs)
self.responses = DynamoDbDict(table_name, 'responses', connection=connection, **kwargs)
self.redirects = DynamoDbDict(
table_name, 'redirects', connection=self.responses.connection, **kwargs
)
class DynamoDbDict(BaseStorage):
"""A dictionary-like interface for DynamoDB key-value store
**Note:** The actual key name on the dynamodb server will be ``namespace``:``table_name``
In order to deal with how dynamodb stores data/keys,
everything, i.e. keys and data, must be pickled.
Args:
table_name: DynamoDb table name
namespace: Name of DynamoDb hash map
connection: :boto3:`DynamoDb Resource <services/dynamodb.html#DynamoDB.ServiceResource>`
object to use instead of creating a new one
kwargs: Additional keyword arguments for :py:meth:`~boto3.session.Session.resource`
"""
def __init__(
self,
table_name,
namespace='http_cache',
connection=None,
read_capacity_units=1,
write_capacity_units=1,
**kwargs,
):
super().__init__(**kwargs)
connection_kwargs = get_valid_kwargs(boto3.Session, kwargs, extras=['endpoint_url'])
self.connection = connection or boto3.resource('dynamodb', **connection_kwargs)
self._self_key = namespace
try:
self.connection.create_table(
AttributeDefinitions=[
{
'AttributeName': 'namespace',
'AttributeType': 'S',
},
{
'AttributeName': 'key',
'AttributeType': 'S',
},
],
TableName=table_name,
KeySchema=[
{'AttributeName': 'namespace', 'KeyType': 'HASH'},
{'AttributeName': 'key', 'KeyType': 'RANGE'},
],
ProvisionedThroughput={
'ReadCapacityUnits': read_capacity_units,
'WriteCapacityUnits': write_capacity_units,
},
)
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._self_key, 'key': str(key)}
def __getitem__(self, key):
result = self._table.get_item(Key=self.composite_key(key))
if 'Item' not in result:
raise KeyError
# Depending on the serializer, the value may be either a string or Binary object
raw_value = result['Item']['value']
return self.serializer.loads(raw_value.value if isinstance(raw_value, Binary) else raw_value)
def __setitem__(self, key, value):
item = {**self.composite_key(key), 'value': self.serializer.dumps(value)}
self._table.put_item(Item=item)
def __delitem__(self, key):
response = self._table.delete_item(Key=self.composite_key(key), ReturnValues='ALL_OLD')
if 'Attributes' not in response:
raise KeyError
def __len__(self):
return self.__count_table()
def __iter__(self):
response = self.__scan_table()
for v in response['Items']:
yield v['key']
def bulk_delete(self, keys: Iterable[str]):
"""Delete multiple keys from the cache. Does not raise errors for missing keys."""
with self._table.batch_writer() as batch:
for key in keys:
batch.delete_item(Key=self.composite_key(key))
def clear(self):
response = self.__scan_table()
for v in response['Items']:
composite_key = {'namespace': v['namespace'], 'key': v['key']}
self._table.delete_item(Key=composite_key)
def __scan_table(self):
expression_attribute_values = {':Namespace': self._self_key}
expression_attribute_names = {'#N': 'namespace'}
key_condition_expression = '#N = :Namespace'
return self._table.query(
ExpressionAttributeValues=expression_attribute_values,
ExpressionAttributeNames=expression_attribute_names,
KeyConditionExpression=key_condition_expression,
)
def __count_table(self):
expression_attribute_values = {':Namespace': self._self_key}
expression_attribute_names = {'#N': 'namespace'}
key_condition_expression = '#N = :Namespace'
return self._table.query(
Select='COUNT',
ExpressionAttributeValues=expression_attribute_values,
ExpressionAttributeNames=expression_attribute_names,
KeyConditionExpression=key_condition_expression,
)['Count']
|