feat: implement caching for duty-related data and enhance performance
- Added a TTLCache class for in-memory caching of duty-related data, improving performance by reducing database queries. - Integrated caching into the group duty pin functionality, allowing for efficient retrieval of message text and next shift end times. - Introduced new methods to invalidate caches when relevant data changes, ensuring data consistency. - Created a new Alembic migration to add indexes on the duties table for improved query performance. - Updated tests to cover the new caching behavior and ensure proper functionality.
This commit is contained in:
44
alembic/versions/008_duties_indexes.py
Normal file
44
alembic/versions/008_duties_indexes.py
Normal file
@@ -0,0 +1,44 @@
|
||||
"""Add indexes on duties table for performance.
|
||||
|
||||
Revision ID: 008
|
||||
Revises: 007
|
||||
Create Date: 2025-02-25
|
||||
|
||||
Indexes for get_current_duty, get_next_shift_end, get_duties, get_duties_for_user.
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision: str = "008"
|
||||
down_revision: Union[str, None] = "007"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_index(
|
||||
"ix_duties_event_type_start_at",
|
||||
"duties",
|
||||
["event_type", "start_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_duties_event_type_end_at",
|
||||
"duties",
|
||||
["event_type", "end_at"],
|
||||
unique=False,
|
||||
)
|
||||
op.create_index(
|
||||
"ix_duties_user_id_start_at",
|
||||
"duties",
|
||||
["user_id", "start_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
op.drop_index("ix_duties_user_id_start_at", table_name="duties")
|
||||
op.drop_index("ix_duties_event_type_end_at", table_name="duties")
|
||||
op.drop_index("ix_duties_event_type_start_at", table_name="duties")
|
||||
Reference in New Issue
Block a user