This commit is contained in:
parent
6c0d96e7ac
commit
d4c16658bd
15
cache/cache.py
vendored
15
cache/cache.py
vendored
|
@ -60,7 +60,6 @@ CACHE_KEYS = {
|
||||||
"TOPIC_FOLLOWERS": "topic:followers:{}",
|
"TOPIC_FOLLOWERS": "topic:followers:{}",
|
||||||
"TOPIC_SHOUTS": "topic_shouts_{}",
|
"TOPIC_SHOUTS": "topic_shouts_{}",
|
||||||
"AUTHOR_ID": "author:id:{}",
|
"AUTHOR_ID": "author:id:{}",
|
||||||
"AUTHOR_USER": "author:user:{}",
|
|
||||||
"SHOUTS": "shouts:{}",
|
"SHOUTS": "shouts:{}",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -300,19 +299,19 @@ async def get_cached_follower_topics(author_id: int):
|
||||||
return topics
|
return topics
|
||||||
|
|
||||||
|
|
||||||
# Get author by user ID from cache
|
# Get author by author_id from cache
|
||||||
async def get_cached_author_by_id(user_id: str, get_with_stat):
|
async def get_cached_author_by_id(author_id: int, get_with_stat):
|
||||||
"""
|
"""
|
||||||
Retrieve author information by user_id, checking the cache first, then the database.
|
Retrieve author information by author_id, checking the cache first, then the database.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
user_id (str): The user identifier for which to retrieve the author.
|
author_id (int): The author identifier for which to retrieve the author.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: Dictionary with author data or None if not found.
|
dict: Dictionary with author data or None if not found.
|
||||||
"""
|
"""
|
||||||
# Attempt to find author ID by user_id in Redis cache
|
# Attempt to find author ID by author_id in Redis cache
|
||||||
author_id = await redis.execute("GET", f"author:user:{author_id}")
|
author_id = await redis.execute("GET", f"author:id:{author_id}")
|
||||||
if author_id:
|
if author_id:
|
||||||
# If ID is found, get full author data by ID
|
# If ID is found, get full author data by ID
|
||||||
author_data = await redis.execute("GET", f"author:id:{author_id}")
|
author_data = await redis.execute("GET", f"author:id:{author_id}")
|
||||||
|
@ -406,7 +405,7 @@ async def invalidate_shouts_cache(cache_keys: List[str]):
|
||||||
logger.debug(f"Invalidated related key: {related_key}")
|
logger.debug(f"Invalidated related key: {related_key}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error invalidating cache key {key}: {e}")
|
logger.error(f"Error invalidating cache key {cache_key}: {e}")
|
||||||
|
|
||||||
|
|
||||||
async def cache_topic_shouts(topic_id: int, shouts: List[dict]):
|
async def cache_topic_shouts(topic_id: int, shouts: List[dict]):
|
||||||
|
|
|
@ -252,11 +252,11 @@ async def invalidate_authors_cache(author_id=None):
|
||||||
f"author:follows-shouts:{author_id}",
|
f"author:follows-shouts:{author_id}",
|
||||||
]
|
]
|
||||||
|
|
||||||
# Получаем user_id автора, если есть
|
# Получаем author_id автора, если есть
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
author = session.query(Author).filter(Author.id == author_id).first()
|
author = session.query(Author).filter(Author.id == author_id).first()
|
||||||
if author and Author.id:
|
if author and Author.id:
|
||||||
specific_keys.append(f"author:user:{Author.id.strip()}")
|
specific_keys.append(f"author:id:{Author.id}")
|
||||||
|
|
||||||
# Удаляем конкретные ключи
|
# Удаляем конкретные ключи
|
||||||
for key in specific_keys:
|
for key in specific_keys:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user