diff --git a/pyproject.toml b/pyproject.toml index 32fbb554..893a841a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -37,6 +37,7 @@ dependencies = [ "zstandard>=0.23.0", "pydantic>=2.10.4", "amplitude-analytics>=1.1.4", + "msgpack>=1.1.0", ] [build-system] diff --git a/shared/helpers/cache.py b/shared/helpers/cache.py index a172039b..aae2913e 100644 --- a/shared/helpers/cache.py +++ b/shared/helpers/cache.py @@ -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__) @@ -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 @@ -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) diff --git a/uv.lock b/uv.lock index 1642081b..0afcd421 100644 --- a/uv.lock +++ b/uv.lock @@ -761,6 +761,25 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/6b/20/471f41173930550f279ccb65596a5ac19b9ac974a8d93679bcd3e0c31498/mock-5.1.0-py3-none-any.whl", hash = "sha256:18c694e5ae8a208cdb3d2c20a993ca1a7b0efa258c247a1e565150f477f83744", size = 30938 }, ] +[[package]] +name = "msgpack" +version = "1.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/cb/d0/7555686ae7ff5731205df1012ede15dd9d927f6227ea151e901c7406af4f/msgpack-1.1.0.tar.gz", hash = "sha256:dd432ccc2c72b914e4cb77afce64aab761c1137cc698be3984eee260bcb2896e", size = 167260 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c8/b0/380f5f639543a4ac413e969109978feb1f3c66e931068f91ab6ab0f8be00/msgpack-1.1.0-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:071603e2f0771c45ad9bc65719291c568d4edf120b44eb36324dcb02a13bfddf", size = 151142 }, + { url = "https://files.pythonhosted.org/packages/c8/ee/be57e9702400a6cb2606883d55b05784fada898dfc7fd12608ab1fdb054e/msgpack-1.1.0-cp313-cp313-macosx_10_13_x86_64.whl", hash = "sha256:0f92a83b84e7c0749e3f12821949d79485971f087604178026085f60ce109330", size = 84523 }, + { url = "https://files.pythonhosted.org/packages/7e/3a/2919f63acca3c119565449681ad08a2f84b2171ddfcff1dba6959db2cceb/msgpack-1.1.0-cp313-cp313-macosx_11_0_arm64.whl", hash = "sha256:4a1964df7b81285d00a84da4e70cb1383f2e665e0f1f2a7027e683956d04b734", size = 81556 }, + { url = "https://files.pythonhosted.org/packages/7c/43/a11113d9e5c1498c145a8925768ea2d5fce7cbab15c99cda655aa09947ed/msgpack-1.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:59caf6a4ed0d164055ccff8fe31eddc0ebc07cf7326a2aaa0dbf7a4001cd823e", size = 392105 }, + { url = "https://files.pythonhosted.org/packages/2d/7b/2c1d74ca6c94f70a1add74a8393a0138172207dc5de6fc6269483519d048/msgpack-1.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:0907e1a7119b337971a689153665764adc34e89175f9a34793307d9def08e6ca", size = 399979 }, + { url = "https://files.pythonhosted.org/packages/82/8c/cf64ae518c7b8efc763ca1f1348a96f0e37150061e777a8ea5430b413a74/msgpack-1.1.0-cp313-cp313-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:65553c9b6da8166e819a6aa90ad15288599b340f91d18f60b2061f402b9a4915", size = 383816 }, + { url = "https://files.pythonhosted.org/packages/69/86/a847ef7a0f5ef3fa94ae20f52a4cacf596a4e4a010197fbcc27744eb9a83/msgpack-1.1.0-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:7a946a8992941fea80ed4beae6bff74ffd7ee129a90b4dd5cf9c476a30e9708d", size = 380973 }, + { url = "https://files.pythonhosted.org/packages/aa/90/c74cf6e1126faa93185d3b830ee97246ecc4fe12cf9d2d31318ee4246994/msgpack-1.1.0-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:4b51405e36e075193bc051315dbf29168d6141ae2500ba8cd80a522964e31434", size = 387435 }, + { url = "https://files.pythonhosted.org/packages/7a/40/631c238f1f338eb09f4acb0f34ab5862c4e9d7eda11c1b685471a4c5ea37/msgpack-1.1.0-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:b4c01941fd2ff87c2a934ee6055bda4ed353a7846b8d4f341c428109e9fcde8c", size = 399082 }, + { url = "https://files.pythonhosted.org/packages/e9/1b/fa8a952be252a1555ed39f97c06778e3aeb9123aa4cccc0fd2acd0b4e315/msgpack-1.1.0-cp313-cp313-win32.whl", hash = "sha256:7c9a35ce2c2573bada929e0b7b3576de647b0defbd25f5139dcdaba0ae35a4cc", size = 69037 }, + { url = "https://files.pythonhosted.org/packages/b6/bc/8bd826dd03e022153bfa1766dcdec4976d6c818865ed54223d71f07862b3/msgpack-1.1.0-cp313-cp313-win_amd64.whl", hash = "sha256:bce7d9e614a04d0883af0b3d4d501171fbfca038f12c77fa838d9f198147a23f", size = 75140 }, +] + [[package]] name = "multidict" version = "6.1.0" @@ -1377,6 +1396,7 @@ dependencies = [ { name = "ijson" }, { name = "minio" }, { name = "mmh3" }, + { name = "msgpack" }, { name = "oauthlib" }, { name = "orjson" }, { name = "prometheus-client" }, @@ -1435,6 +1455,7 @@ requires-dist = [ { name = "ijson", specifier = ">=3.2.3" }, { name = "minio", specifier = ">=7.1.13" }, { name = "mmh3", specifier = ">=4.0.1" }, + { name = "msgpack", specifier = ">=1.1.0" }, { name = "oauthlib", specifier = ">=3.1.0" }, { name = "orjson", specifier = ">=3.10.9" }, { name = "prometheus-client", specifier = ">=0.17.1" },