update-author_cache
This commit is contained in:
parent
314c54969b
commit
f596a9bf2c
23
Dockerfile
23
Dockerfile
|
@ -1,12 +1,25 @@
|
|||
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
|
||||
|
||||
# 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
|
||||
|
||||
RUN apk update && apk add --no-cache build-base icu-data-full curl python3-dev musl-dev postgresql-dev postgresql-client
|
||||
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 the port
|
||||
EXPOSE 8000
|
||||
|
||||
CMD ["python", "server.py"]
|
||||
|
|
|
@ -55,7 +55,7 @@ async def get_author(_, _info, slug='', author_id=None):
|
|||
q = select(aliased_author).where(aliased_author.id == author_id)
|
||||
[author] = get_with_stat(q)
|
||||
if author:
|
||||
await update_author_cache(author)
|
||||
await update_author_cache(author.dict())
|
||||
else:
|
||||
author = json.loads(cache)
|
||||
except Exception as exc:
|
||||
|
@ -79,7 +79,7 @@ async def get_author_by_user_id(user_id: str):
|
|||
|
||||
[author] = get_with_stat(q)
|
||||
if author:
|
||||
await update_author_cache(author)
|
||||
await update_author_cache(author.dict())
|
||||
except Exception as exc:
|
||||
logger.error(exc)
|
||||
return author
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user