From 7a5cbf7438183369fb81e2fb70a4d01e5c2fb260 Mon Sep 17 00:00:00 2001 From: Untone Date: Wed, 6 Mar 2024 22:00:37 +0300 Subject: [PATCH] custom-encoder-fix-2 --- resolvers/author.py | 8 ++++---- services/cache.py | 18 +++++++++--------- services/search.py | 2 +- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/resolvers/author.py b/resolvers/author.py index 0f6f556f..44e1aec4 100644 --- a/resolvers/author.py +++ b/resolvers/author.py @@ -162,14 +162,14 @@ async def get_author_follows(_, _info, slug='', user=None, author_id=None): ) if not cached: prepared = [author.dict() for author in authors] - await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared), cls=CustomJSONEncoder) + await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared, cls=CustomJSONEncoder)) rkey = f'author:{author_id}:follows-topics' cached = await redis.execute('GET', rkey) topics = json.loads(cached) if cached else author_follows_topics(author_id) if not cached: prepared = [topic.dict() for topic in topics] - await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared), cls=CustomJSONEncoder) + await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared, cls=CustomJSONEncoder)) return { 'topics': topics, 'authors': authors, @@ -198,7 +198,7 @@ async def get_author_follows_topics(_, _info, slug='', user=None, author_id=None topics = json.loads(cached) if cached else author_follows_topics(author_id) if not cached: prepared = [topic.dict() for topic in topics] - await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared), cls=CustomJSONEncoder) + await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared, cls=CustomJSONEncoder)) return topics else: raise ValueError('Author not found') @@ -223,7 +223,7 @@ async def get_author_follows_authors(_, _info, slug='', user=None, author_id=Non ) if not cached: prepared = [author.dict() for author in authors] - await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared), cls=CustomJSONEncoder) + await redis.execute('SET', rkey, 24 * 60 * 60, json.dumps(prepared, cls=CustomJSONEncoder)) return authors else: raise ValueError('Author not found') diff --git a/services/cache.py b/services/cache.py index ec806138..e536cee9 100644 --- a/services/cache.py +++ b/services/cache.py @@ -21,20 +21,20 @@ DEFAULT_FOLLOWS = { async def set_author_cache(author: dict, ttl=25 * 60 * 60): - payload = json.dumps(author) - 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) + payload = json.dumps(author, cls=CustomJSONEncoder) + await redis.execute('SET', f'user:{author.get("user")}:author', ttl, payload) + await redis.execute('SET', f'id:{author.get("id")}:author', ttl, payload) async def update_author_followers_cache(author_id: int, followers, ttl=25 * 60 * 60): - payload = json.dumps(followers) - await redis.execute('SET', f'author:{author_id}:followers', ttl, payload, cls=CustomJSONEncoder) + payload = json.dumps(followers, cls=CustomJSONEncoder) + await redis.execute('SET', f'author:{author_id}:followers', ttl, payload) async def set_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60): try: - payload = json.dumps(follows) - await redis.execute('SET', f'author:{author_id}:follows-topics', ttl, payload, cls=CustomJSONEncoder) + payload = json.dumps(follows, cls=CustomJSONEncoder) + await redis.execute('SET', f'author:{author_id}:follows-topics', ttl, payload) except Exception as exc: logger.error(exc) import traceback @@ -45,9 +45,9 @@ async def set_follows_topics_cache(follows, author_id: int, ttl=25 * 60 * 60): async def set_follows_authors_cache(follows, author_id: int, ttl=25 * 60 * 60): try: - payload = json.dumps(follows) + payload = json.dumps(follows, cls=CustomJSONEncoder) await redis.execute( - 'SET', f'author:{author_id}:follows-authors', ttl, payload, cls=CustomJSONEncoder + 'SET', f'author:{author_id}:follows-authors', ttl, payload ) except Exception: import traceback diff --git a/services/search.py b/services/search.py index b5c81169..10893421 100644 --- a/services/search.py +++ b/services/search.py @@ -145,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), cls=CustomJSONEncoder) + await redis.execute('SETEX', redis_key, REDIS_TTL, json.dumps(results, cls=CustomJSONEncoder)) return []