-
Notifications
You must be signed in to change notification settings - Fork 156
/
ddbcache.py
42 lines (38 loc) · 1.36 KB
/
ddbcache.py
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
import boto3
import os
import time
from botocore.exceptions import ClientError
class DDBCache(object):
def __init__(self, table_name: str):
self._sess = boto3.Session()
ddb = self._sess.resource('dynamodb', region_name=os.environ.get("AWS_REGION"))
self._table = ddb.Table(table_name)
def get(self, repo_slug: str):
try:
resp = self._table.get_item(Key={'repoSlug': repo_slug})
if 'Item' not in resp:
return None
# Return None if we're beyond TTL limits, so that we can re-populate the item.
if int(time.time()) > resp['Item']['ttl']:
return None
return resp['Item']
except ClientError as e:
print(e.response['Error']['Message'])
def put(self, repo_slug: str, data: any):
try:
resp = self._table.update_item(
Key={
'repoSlug': repo_slug
},
UpdateExpression="set #ts = :ts, repodata = :repodata",
ExpressionAttributeValues={
':ts': int(time.time()) + 300,
':repodata': data
},
ExpressionAttributeNames={
'#ts': 'ttl'
}
)
return resp
except ClientError as e:
print("ERR: ", e)