Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies = [
"zstandard>=0.23.0",
"pydantic>=2.10.4",
"amplitude-analytics>=1.1.4",
"msgpack>=1.1.0",
]

[build-system]
Expand Down
22 changes: 3 additions & 19 deletions shared/helpers/cache.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import asyncio
import base64
import hashlib
import json
import logging
from functools import wraps
from typing import Any, Callable, Hashable

import msgpack
from redis import Redis, RedisError

log = logging.getLogger(__name__)
Expand All @@ -15,22 +15,6 @@
DEFAULT_TTL = 120


def attempt_json_dumps(value: Any) -> str:
def assert_string_keys(d: dict[Any, Any]) -> None:
for k, v in d.items():
if not isinstance(k, str):
raise TypeError(
f"Attempted to JSON-serialize a dictionary with non-string key: {k}"
)
if isinstance(v, dict):
assert_string_keys(v)

if isinstance(value, dict):
assert_string_keys(value)

return json.dumps(value)


def make_hash_sha256(o: Any) -> str:
"""Provides a machine-independent, consistent hash value for any object

Expand Down Expand Up @@ -114,13 +98,13 @@ def get(self, key: str) -> Any:
if serialized_value is None:
return NO_VALUE
try:
return json.loads(serialized_value)
return msgpack.loads(serialized_value)
except ValueError:
return NO_VALUE

def set(self, key: str, ttl: int, value: Any):
try:
serialized_value = attempt_json_dumps(value)
serialized_value = msgpack.dumps(value)
self.redis_connection.setex(key, ttl, serialized_value)
except RedisError:
log.warning("Unable to set cache on redis", exc_info=True)
Expand Down
21 changes: 21 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.