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:
@@ -229,6 +229,22 @@ def get_or_create_user_by_full_name(session: Session, full_name: str) -> User:
|
||||
return user
|
||||
|
||||
|
||||
def get_users_by_full_names(session: Session, full_names: list[str]) -> dict[str, User]:
|
||||
"""Get users by full_name. Returns dict full_name -> User. Does not create missing.
|
||||
|
||||
Args:
|
||||
session: DB session.
|
||||
full_names: List of full names to look up.
|
||||
|
||||
Returns:
|
||||
Dict mapping full_name to User for found users.
|
||||
"""
|
||||
if not full_names:
|
||||
return {}
|
||||
users = session.query(User).filter(User.full_name.in_(full_names)).all()
|
||||
return {u.full_name: u for u in users}
|
||||
|
||||
|
||||
def update_user_display_name(
|
||||
session: Session,
|
||||
telegram_user_id: int,
|
||||
@@ -268,6 +284,8 @@ def delete_duties_in_range(
|
||||
user_id: int,
|
||||
from_date: str,
|
||||
to_date: str,
|
||||
*,
|
||||
commit: bool = True,
|
||||
) -> int:
|
||||
"""Delete all duties of the user that overlap the given date range.
|
||||
|
||||
@@ -276,6 +294,7 @@ def delete_duties_in_range(
|
||||
user_id: User id.
|
||||
from_date: Start date YYYY-MM-DD.
|
||||
to_date: End date YYYY-MM-DD.
|
||||
commit: If True, commit immediately. If False, caller commits (for batch import).
|
||||
|
||||
Returns:
|
||||
Number of duties deleted.
|
||||
@@ -288,7 +307,8 @@ def delete_duties_in_range(
|
||||
)
|
||||
count = q.count()
|
||||
q.delete(synchronize_session=False)
|
||||
session.commit()
|
||||
if commit:
|
||||
session.commit()
|
||||
return count
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user