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:
@@ -103,6 +103,7 @@ class TestGetDutyMessageText:
|
||||
"""Tests for get_duty_message_text."""
|
||||
|
||||
def test_no_current_duty_returns_no_duty(self, session):
|
||||
svc.duty_pin_cache.invalidate_pattern(("duty_message_text",))
|
||||
with patch(
|
||||
"duty_teller.services.group_duty_pin_service.get_current_duty",
|
||||
return_value=None,
|
||||
@@ -113,6 +114,7 @@ class TestGetDutyMessageText:
|
||||
assert result == "No duty"
|
||||
|
||||
def test_with_current_duty_returns_formatted(self, session, duty, user):
|
||||
svc.duty_pin_cache.invalidate_pattern(("duty_message_text",))
|
||||
with patch(
|
||||
"duty_teller.services.group_duty_pin_service.get_current_duty",
|
||||
return_value=(duty, user),
|
||||
@@ -130,6 +132,7 @@ class TestGetNextShiftEndUtc:
|
||||
"""Tests for get_next_shift_end_utc."""
|
||||
|
||||
def test_no_next_shift_returns_none(self, session):
|
||||
svc.duty_pin_cache.invalidate(("next_shift_end",))
|
||||
with patch(
|
||||
"duty_teller.services.group_duty_pin_service.get_next_shift_end",
|
||||
return_value=None,
|
||||
@@ -138,6 +141,7 @@ class TestGetNextShiftEndUtc:
|
||||
assert result is None
|
||||
|
||||
def test_has_next_shift_returns_naive_utc(self, session):
|
||||
svc.duty_pin_cache.invalidate(("next_shift_end",))
|
||||
naive = datetime(2025, 2, 21, 6, 0, 0)
|
||||
with patch(
|
||||
"duty_teller.services.group_duty_pin_service.get_next_shift_end",
|
||||
|
||||
Reference in New Issue
Block a user