update-author_cache
This commit is contained in:
@@ -20,6 +20,10 @@ inspector = inspect(engine)
|
||||
configure_mappers()
|
||||
T = TypeVar('T')
|
||||
REGISTRY: Dict[str, type] = {}
|
||||
FILTERED_FIELDS = [
|
||||
'_sa_instance_state',
|
||||
'search_vector'
|
||||
]
|
||||
|
||||
|
||||
# noinspection PyUnusedLocal
|
||||
@@ -42,9 +46,7 @@ class Base(declarative_base()):
|
||||
REGISTRY[cls.__name__] = cls
|
||||
|
||||
def dict(self) -> Dict[str, Any]:
|
||||
column_names = self.__table__.columns.keys()
|
||||
if '_sa_instance_state' in column_names:
|
||||
column_names.remove('_sa_instance_state')
|
||||
column_names = filter(lambda x: x not in FILTERED_FIELDS, self.__table__.columns.keys())
|
||||
try:
|
||||
return {c: getattr(self, c) for c in column_names}
|
||||
except Exception as e:
|
||||
|
@@ -18,8 +18,8 @@ DEFAULT_FOLLOWS = {
|
||||
}
|
||||
|
||||
|
||||
async def update_author_cache(author: Author, ttl=25 * 60 * 60):
|
||||
payload = json.dumps(author.dict())
|
||||
async def update_author_cache(author: dict, ttl=25 * 60 * 60):
|
||||
payload = json.dumps(author)
|
||||
await redis.execute('SETEX', f'user:{author.user}:author', ttl, payload)
|
||||
await redis.execute('SETEX', f'id:{author.id}:author', ttl, payload)
|
||||
|
||||
@@ -48,7 +48,7 @@ def after_shouts_update(mapper, connection, shout: Shout):
|
||||
)
|
||||
authors = get_with_stat(authors_query)
|
||||
for author in authors:
|
||||
asyncio.create_task(update_author_cache(author))
|
||||
asyncio.create_task(update_author_cache(author.dict()))
|
||||
|
||||
|
||||
@event.listens_for(Reaction, 'after_insert')
|
||||
@@ -64,7 +64,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction):
|
||||
authors = get_with_stat(author_query)
|
||||
|
||||
for author in authors:
|
||||
asyncio.create_task(update_author_cache(author))
|
||||
asyncio.create_task(update_author_cache(author.author()))
|
||||
|
||||
shout = connection.execute(select(Shout).where(Shout.id == reaction.shout)).first()
|
||||
if shout:
|
||||
@@ -74,7 +74,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction):
|
||||
@event.listens_for(Author, 'after_insert')
|
||||
@event.listens_for(Author, 'after_update')
|
||||
def after_author_update(mapper, connection, author: Author):
|
||||
asyncio.create_task(update_author_cache(author))
|
||||
asyncio.create_task(update_author_cache(author.dict()))
|
||||
|
||||
|
||||
@event.listens_for(TopicFollower, 'after_insert')
|
||||
@@ -132,8 +132,8 @@ async def handle_author_follower_change(
|
||||
follower_query = select(Author).filter(Author.id == follower_id)
|
||||
follower = get_with_stat(follower_query)
|
||||
if follower and author:
|
||||
_ = asyncio.create_task(update_author_cache(author))
|
||||
_ = asyncio.create_task(update_author_cache(follower))
|
||||
_ = asyncio.create_task(update_author_cache(author.dict()))
|
||||
_ = asyncio.create_task(update_author_cache(follower.dict()))
|
||||
await update_follows_for_user(
|
||||
connection,
|
||||
follower.user,
|
||||
@@ -159,7 +159,7 @@ async def handle_topic_follower_change(
|
||||
follower_query = select(Author).filter(Author.id == follower_id)
|
||||
follower = get_with_stat(follower_query)
|
||||
if follower and topic:
|
||||
_ = asyncio.create_task(update_author_cache(follower))
|
||||
_ = asyncio.create_task(update_author_cache(follower.dict()))
|
||||
await update_follows_for_user(
|
||||
connection,
|
||||
follower.user,
|
||||
|
Reference in New Issue
Block a user