Skip to content

Commit

Permalink
Change DynamoDBCacheBackend to serialize cached value as json string
Browse files Browse the repository at this point in the history
  • Loading branch information
mat-sop committed Oct 12, 2023
1 parent 637a08c commit b05cb22
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## UNRELEASED

- Changed `DynamoDBCacheBackend` to serialize cached value as json string.


## 0.2.0 (2023-09-25)

- Added `CloudflareCacheBackend`.
Expand Down
8 changes: 6 additions & 2 deletions ariadne_graphql_proxy/contrib/aws/cache_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import time
from typing import Any, Optional

Expand Down Expand Up @@ -60,7 +61,10 @@ def _query_by_key(self, key: str, max_ttl: int) -> dict:
)

async def set(self, key: str, value: Any, ttl: Optional[int] = None):
item = {self.partition_key: key, self.value_attribute_name: value}
item: dict[str, Any] = {
self.partition_key: key,
self.value_attribute_name: json.dumps(value),
}
if ttl:
now = int(time.time())
item[self.ttl_attribute] = now + ttl
Expand All @@ -74,7 +78,7 @@ async def get(self, key: str, default: Any = None) -> Any:
if len(items) < 1:
return default

return items[0].get(self.value_attribute_name, default)
return json.loads(items[0][self.value_attribute_name])

async def clear_all(self):
pass
34 changes: 28 additions & 6 deletions tests/contrib/aws/test_cache_backend.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import time

Expand Down Expand Up @@ -43,13 +44,30 @@ def test_init_raises_dynamodb_cache_error_for_unavailable_table(aws_credentials)


@pytest.mark.asyncio
async def test_set_creates_correct_item_in_table(test_table):
@pytest.mark.parametrize(
"test_value",
[
"test",
14,
10.11,
True,
{"val": 5.5},
{"val": "test"},
{"val": 22},
{"val": False},
["x", "y", "z"],
[1, 2, 3],
[1.1, 2.2, 3.3],
[True, False, True],
],
)
async def test_set_creates_correct_item_in_table(test_table, test_value):
cache = DynamoDBCacheBackend(table_name="test_table")

await cache.set(key="test_key", value="test_value")
await cache.set(key="test_key", value=test_value)

response = test_table.get_item(Key={"key": "test_key"})
assert response["Item"] == {"key": "test_key", "value": "test_value"}
assert response["Item"] == {"key": "test_key", "value": json.dumps(test_value)}


@pytest.mark.asyncio
Expand All @@ -62,14 +80,14 @@ async def test_set_creates_item_with_ttl(test_table):
response = test_table.get_item(Key={"key": "test_key"})
assert response["Item"] == {
"key": "test_key",
"value": "test_value",
"value": json.dumps("test_value"),
"ttl": int(time.time()) + 300,
}


@pytest.mark.asyncio
async def test_get_returns_value_from_table(test_table):
test_table.put_item(Item={"key": "test_key", "value": "test_value"})
test_table.put_item(Item={"key": "test_key", "value": json.dumps("test_value")})
cache = DynamoDBCacheBackend(table_name="test_table")

assert await cache.get(key="test_key") == "test_value"
Expand All @@ -86,7 +104,11 @@ async def test_get_returns_default_for_not_exisitng_key(test_table):
@freeze_time("2023-01-01 12:00:00")
async def test_get_returns_not_expired_item(test_table):
test_table.put_item(
Item={"key": "test_key", "value": "test_value", "ttl": int(time.time()) + 900}
Item={
"key": "test_key",
"value": json.dumps("test_value"),
"ttl": int(time.time()) + 900,
}
)
cache = DynamoDBCacheBackend(table_name="test_table")

Expand Down

0 comments on commit b05cb22

Please sign in to comment.