Use msgpack for redis cache

This commit is contained in:
2023-02-13 21:43:14 +01:00
parent a3619470fd
commit 5eb3d47656
3 changed files with 37 additions and 5 deletions

33
poetry.lock generated
View File

@@ -752,6 +752,37 @@ postgres = ["asyncpg (>=0.24,<0.28)", "psycopg2-binary (>=2.9.1,<3.0.0)"]
postgresql = ["asyncpg (>=0.24,<0.28)", "psycopg2-binary (>=2.9.1,<3.0.0)"] postgresql = ["asyncpg (>=0.24,<0.28)", "psycopg2-binary (>=2.9.1,<3.0.0)"]
sqlite = ["aiosqlite (>=0.17,<0.19)"] sqlite = ["aiosqlite (>=0.17,<0.19)"]
[[package]]
name = "ormsgpack"
version = "1.2.5"
description = "Fast, correct Python msgpack library supporting dataclasses, datetimes, and numpy"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "ormsgpack-1.2.5-cp310-cp310-macosx_10_7_x86_64.whl", hash = "sha256:95b97d8d184df1b75bfd3a460ba4aea32ac5b29382d96a282a04ae6cb7d7d6e5"},
{file = "ormsgpack-1.2.5-cp310-cp310-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:afca4b4580469fdc50b16427c9cd71bf8fd2dabf7f709408f0891b350c9effa9"},
{file = "ormsgpack-1.2.5-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:55a2d59d0e443e60b1218adfe6e8a85cdb8427e49890bc6c9c1e11877fab2ec5"},
{file = "ormsgpack-1.2.5-cp310-none-win_amd64.whl", hash = "sha256:5d0b823a6cef057ebedd34872c6df1f7b89d969faa1344ad11b93c5cce25c95b"},
{file = "ormsgpack-1.2.5-cp311-cp311-macosx_10_7_x86_64.whl", hash = "sha256:b95efb8c1a27bd83190bec67b4b94951c84509b9bf79564dfbaa088e458f4c60"},
{file = "ormsgpack-1.2.5-cp311-cp311-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:55500ebdb807e4fa0f797c753cbe9efbbf9bbac1dc9c6589e51bad75382b793c"},
{file = "ormsgpack-1.2.5-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:c4ccfd2475cf7cf985c69ef81d975ff3bf9a3989901b6cca41eeb1e580f3e1be"},
{file = "ormsgpack-1.2.5-cp311-none-win_amd64.whl", hash = "sha256:56b7056285d81451523bd9d79d84bee2993110887759db20854a7901c3d7a6cd"},
{file = "ormsgpack-1.2.5-cp37-cp37m-macosx_10_7_x86_64.whl", hash = "sha256:e5fb54e19be45f0079be496dcb8e6fea3e00731da6c5df445c68d33d701c4d48"},
{file = "ormsgpack-1.2.5-cp37-cp37m-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:9de408792fdc47795f2780ca950d95441b366f9d9e30fec35f0f1bbe2c2af6ef"},
{file = "ormsgpack-1.2.5-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6c05479bed27e9a0d33798628891a38d3e55c22fc851e37f76c73684196c74cc"},
{file = "ormsgpack-1.2.5-cp37-none-win_amd64.whl", hash = "sha256:6b25ae7c50c7bd2442a3c56ad507a656bd02ba2bb17fad8c57f7694297509287"},
{file = "ormsgpack-1.2.5-cp38-cp38-macosx_10_7_x86_64.whl", hash = "sha256:41eed49ea3ec4193d35fc17add4d838289c59a29c976891aec22019d8c2a2587"},
{file = "ormsgpack-1.2.5-cp38-cp38-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:47e89c455b33fb4d6088a763a9e81203f5c88c6aabbcbdbd118bffbe0a4638ff"},
{file = "ormsgpack-1.2.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:dc95c9cb26e99312e7f073aedecdce91a208e245f86e47a2cb719b27b2d1e9d1"},
{file = "ormsgpack-1.2.5-cp38-none-win_amd64.whl", hash = "sha256:16e09a3bedea1ff5ffe40b29ad566932520752e81964b55f20751a7a15edca83"},
{file = "ormsgpack-1.2.5-cp39-cp39-macosx_10_7_x86_64.whl", hash = "sha256:a7f1c5d76197d8d5d61c7ce15951db2f8e52ab7388c797e7a6b9d383752dc263"},
{file = "ormsgpack-1.2.5-cp39-cp39-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl", hash = "sha256:a423d470f06e6de73bd789dbb029c149309140ae85001c9c870755404bfb04f1"},
{file = "ormsgpack-1.2.5-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:4f1e2eae32bbd6f8871f1df34e51a5fe5bc4da4ed371d40ae9b4ce2f4e8de2f4"},
{file = "ormsgpack-1.2.5-cp39-none-win_amd64.whl", hash = "sha256:5621f63350e0506bb0c02f137ea9de587ebd1e90fc925a7ec2be6bdc2590e3b4"},
{file = "ormsgpack-1.2.5.tar.gz", hash = "sha256:f761a67ff2019d7e33f558de1622c815c71e35daf67548b183b2f9d6d985de2c"},
]
[[package]] [[package]]
name = "platformdirs" name = "platformdirs"
version = "2.6.2" version = "2.6.2"
@@ -1450,4 +1481,4 @@ files = [
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.11" python-versions = "^3.11"
content-hash = "122e849f8af6d64f1aff5fa2dd9c1bcd47c1d0ba4a180e2161dbd7f3ffff736f" content-hash = "a93a240cad8958a709a0029c81d58846bd27fc77c7ff66a83d0f79f5a44bcb6c"

View File

@@ -18,6 +18,7 @@ uvloop = "^0.17.0"
gunicorn = "^20.1.0" gunicorn = "^20.1.0"
sentry-sdk = "^1.14.0" sentry-sdk = "^1.14.0"
redis = {extras = ["hiredis"], version = "^4.4.2"} redis = {extras = ["hiredis"], version = "^4.4.2"}
ormsgpack = "^1.2.5"
[tool.poetry.dev-dependencies] [tool.poetry.dev-dependencies]

View File

@@ -1,7 +1,7 @@
from typing import Optional, Union from typing import Optional, Union
from fastapi import HTTPException, status from fastapi import HTTPException, status
import orjson import ormsgpack
from redis import asyncio as aioredis from redis import asyncio as aioredis
from app.models import User from app.models import User
@@ -18,7 +18,7 @@ class UsersDataManager:
@classmethod @classmethod
def _get_cache_key(cls, user_id: int) -> str: def _get_cache_key(cls, user_id: int) -> str:
return f"user_{user_id}" return f"user_v2_{user_id}"
@classmethod @classmethod
async def _get_user_from_cache( async def _get_user_from_cache(
@@ -31,7 +31,7 @@ class UsersDataManager:
if data is None: if data is None:
return None return None
return UserDetail.parse_obj(orjson.loads(data)) return UserDetail.parse_obj(ormsgpack.unpackb(data))
except aioredis.RedisError: except aioredis.RedisError:
return None return None
@@ -40,7 +40,7 @@ class UsersDataManager:
async def _cache_user(cls, user: User, redis: aioredis.Redis) -> bool: async def _cache_user(cls, user: User, redis: aioredis.Redis) -> bool:
try: try:
key = cls._get_cache_key(user.id) key = cls._get_cache_key(user.id)
data = orjson.dumps(user.dict()) data = ormsgpack.packb(user.dict())
await redis.set(key, data) await redis.set(key, data)
return True return True
except aioredis.RedisError: except aioredis.RedisError: