custom-encoder-fix

This commit is contained in:
2024-03-06 21:57:04 +03:00
parent 4aa4303a59
commit 2b89ab7c78
4 changed files with 22 additions and 10 deletions

View File

@@ -8,6 +8,7 @@ from orm.reaction import Reaction
from orm.shout import ShoutAuthor, Shout
from orm.topic import Topic, TopicFollower
from resolvers.stat import get_with_stat
from services.encoders import CustomJSONEncoder
from services.rediscache import redis
from services.logger import root_logger as logger
@@ -21,19 +22,19 @@ DEFAULT_FOLLOWS = {
async def set_author_cache(author: dict, ttl=25 * 60 * 60):
payload = json.dumps(author)
await redis.execute('SETEX', f'user:{author.get("user")}:author', ttl, payload)
await redis.execute('SETEX', f'id:{author.get("id")}:author', ttl, payload)
await redis.execute('SET', f'user:{author.get("user")}:author', ttl, payload, cls=CustomJSONEncoder)
await redis.execute('SET', f'id:{author.get("id")}:author', ttl, payload, cls=CustomJSONEncoder)
async def update_author_followers_cache(author_id: int, followers, ttl=25 * 60 * 60):
payload = json.dumps(followers)
await redis.execute('SETEX', f'author:{author_id}:followers', ttl, payload)
await redis.execute('SET', f'author:{author_id}:followers', ttl, payload, cls=CustomJSONEncoder)
async def set_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60):
try:
payload = json.dumps(follows)
await redis.execute('SETEX', f'author:{author_id}:follows-topics', ttl, payload)
await redis.execute('SET', f'author:{author_id}:follows-topics', ttl, payload, cls=CustomJSONEncoder)
except Exception as exc:
logger.error(exc)
import traceback
@@ -46,7 +47,7 @@ async def set_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60):
try:
payload = json.dumps(follows)
await redis.execute(
'SETEX', f'author:{author_id}:follows-authors', ttl, payload
'SET', f'author:{author_id}:follows-authors', ttl, payload, cls=CustomJSONEncoder
)
except Exception:
import traceback

9
services/encoders.py Normal file
View File

@@ -0,0 +1,9 @@
import json
from decimal import Decimal
class CustomJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return str(obj)
return super().default(obj)

View File

@@ -4,6 +4,7 @@ import os
from opensearchpy import OpenSearch
from services.encoders import CustomJSONEncoder
from services.logger import root_logger as logger
from services.rediscache import redis
@@ -144,7 +145,7 @@ class SearchService:
# Use Redis as cache with TTL
redis_key = f'search:{text}'
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(results))
await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(results), cls=CustomJSONEncoder)
return []