custom-encoder-fix-2

This commit is contained in:
Untone 2024-03-06 22:00:37 +03:00
parent 2b89ab7c78
commit 7a5cbf7438
3 changed files with 14 additions and 14 deletions

View File

@ -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')

View File

@ -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

View File

@ -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 []