update-author_cache

This commit is contained in:
Untone 2024-02-25 21:16:34 +03:00
parent 314c54969b
commit f596a9bf2c
4 changed files with 33 additions and 18 deletions

View File

@ -1,12 +1,25 @@
FROM python:alpine FROM python:alpine
# Update package lists and install necessary dependencies
RUN apk update && \
apk add --no-cache build-base icu-data-full curl python3-dev musl-dev postgresql-dev postgresql-client && \
curl -sSL https://install.python-poetry.org | python
# Set working directory
WORKDIR /app WORKDIR /app
# Copy only the pyproject.toml file initially
COPY pyproject.toml /app/
# Install poetry and dependencies
RUN pip install poetry && \
poetry config virtualenvs.create false && \
poetry install --no-root --only main
# Copy the rest of the application
COPY . /app COPY . /app
RUN apk update && apk add --no-cache build-base icu-data-full curl python3-dev musl-dev postgresql-dev postgresql-client # Expose the port
RUN curl -sSL https://install.python-poetry.org | python
ENV PATH="${PATH}:/root/.local/bin"
RUN poetry config virtualenvs.create false && poetry install --only main
EXPOSE 8000 EXPOSE 8000
CMD ["python", "server.py"] CMD ["python", "server.py"]

View File

@ -55,7 +55,7 @@ async def get_author(_, _info, slug='', author_id=None):
q = select(aliased_author).where(aliased_author.id == author_id) q = select(aliased_author).where(aliased_author.id == author_id)
[author] = get_with_stat(q) [author] = get_with_stat(q)
if author: if author:
await update_author_cache(author) await update_author_cache(author.dict())
else: else:
author = json.loads(cache) author = json.loads(cache)
except Exception as exc: except Exception as exc:
@ -79,7 +79,7 @@ async def get_author_by_user_id(user_id: str):
[author] = get_with_stat(q) [author] = get_with_stat(q)
if author: if author:
await update_author_cache(author) await update_author_cache(author.dict())
except Exception as exc: except Exception as exc:
logger.error(exc) logger.error(exc)
return author return author

View File

@ -20,6 +20,10 @@ inspector = inspect(engine)
configure_mappers() configure_mappers()
T = TypeVar('T') T = TypeVar('T')
REGISTRY: Dict[str, type] = {} REGISTRY: Dict[str, type] = {}
FILTERED_FIELDS = [
'_sa_instance_state',
'search_vector'
]
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
@ -42,9 +46,7 @@ class Base(declarative_base()):
REGISTRY[cls.__name__] = cls REGISTRY[cls.__name__] = cls
def dict(self) -> Dict[str, Any]: def dict(self) -> Dict[str, Any]:
column_names = self.__table__.columns.keys() column_names = filter(lambda x: x not in FILTERED_FIELDS, self.__table__.columns.keys())
if '_sa_instance_state' in column_names:
column_names.remove('_sa_instance_state')
try: try:
return {c: getattr(self, c) for c in column_names} return {c: getattr(self, c) for c in column_names}
except Exception as e: except Exception as e:

View File

@ -18,8 +18,8 @@ DEFAULT_FOLLOWS = {
} }
async def update_author_cache(author: Author, ttl=25 * 60 * 60): async def update_author_cache(author: dict, ttl=25 * 60 * 60):
payload = json.dumps(author.dict()) payload = json.dumps(author)
await redis.execute('SETEX', f'user:{author.user}:author', ttl, payload) await redis.execute('SETEX', f'user:{author.user}:author', ttl, payload)
await redis.execute('SETEX', f'id:{author.id}: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) authors = get_with_stat(authors_query)
for author in authors: 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') @event.listens_for(Reaction, 'after_insert')
@ -64,7 +64,7 @@ def after_reaction_insert(mapper, connection, reaction: Reaction):
authors = get_with_stat(author_query) authors = get_with_stat(author_query)
for author in authors: 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() shout = connection.execute(select(Shout).where(Shout.id == reaction.shout)).first()
if shout: 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_insert')
@event.listens_for(Author, 'after_update') @event.listens_for(Author, 'after_update')
def after_author_update(mapper, connection, author: Author): 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') @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_query = select(Author).filter(Author.id == follower_id)
follower = get_with_stat(follower_query) follower = get_with_stat(follower_query)
if follower and author: if follower and author:
_ = asyncio.create_task(update_author_cache(author)) _ = asyncio.create_task(update_author_cache(author.dict()))
_ = asyncio.create_task(update_author_cache(follower)) _ = asyncio.create_task(update_author_cache(follower.dict()))
await update_follows_for_user( await update_follows_for_user(
connection, connection,
follower.user, follower.user,
@ -159,7 +159,7 @@ async def handle_topic_follower_change(
follower_query = select(Author).filter(Author.id == follower_id) follower_query = select(Author).filter(Author.id == follower_id)
follower = get_with_stat(follower_query) follower = get_with_stat(follower_query)
if follower and topic: if follower and topic:
_ = asyncio.create_task(update_author_cache(follower)) _ = asyncio.create_task(update_author_cache(follower.dict()))
await update_follows_for_user( await update_follows_for_user(
connection, connection,
follower.user, follower.user,