Skip to content

Commit 3176422

Browse files
committed
fix: centralize cache handling with reusable cache_set helper
- add `cache_set` function in `stats_helpers.utils` - replace direct `cache.set` calls with `cache_set` across stats helpers - update imports to reflect the new utility module structure
1 parent 92f300d commit 3176422

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

backend/donations/views/dashboard/admin_dashboard.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
from django.utils.timezone import now
88
from django.utils.translation import gettext_lazy as _
99

10-
from .stats_helper_chart import donors_for_month
11-
from .stats_helper_metrics import (
10+
from donations.views.dashboard.stats_helpers.chart import donors_for_month
11+
from donations.views.dashboard.stats_helpers.metrics import (
1212
all_active_ngos,
1313
all_redirections,
1414
current_year_redirections,
1515
ngos_active_in_current_year,
1616
ngos_with_ngo_hub,
1717
)
18-
from .stats_helpers_yearly import get_stats_for_year
18+
from donations.views.dashboard.stats_helpers.yearly import get_stats_for_year
1919

2020
from .helpers import (
2121
generate_donations_per_month_chart,

backend/donations/views/dashboard/stats_helpers/__init__.py

Whitespace-only changes.

backend/donations/views/dashboard/stats_helper_chart.py renamed to backend/donations/views/dashboard/stats_helpers/chart.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django_q.tasks import async_task
77

88
from donations.models import Donor
9-
9+
from donations.views.dashboard.stats_helpers.utils import cache_set
1010

1111
STATS_FOR_MONTH_CACHE_PREFIX = "STATS_FOR_MONTH_"
1212

@@ -52,8 +52,6 @@ def donors_for_month(month: int, year: int = None) -> Dict[str, int]:
5252
month,
5353
year,
5454
cache_key,
55-
hook="_donors_for_month_callback",
56-
task_name="donors_for_month_task",
5755
)
5856

5957
return cached_stats or default_stat
@@ -85,6 +83,6 @@ def _update_stats_for_month(month: int, year: int, cache_key: str) -> Dict[str,
8583
"month": month,
8684
}
8785

88-
cache.set(cache_key, stat)
86+
cache_set(cache_key, stat)
8987

9088
return stat

backend/donations/views/dashboard/stats_helper_metrics.py renamed to backend/donations/views/dashboard/stats_helpers/metrics.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django_q.tasks import async_task
55

66
from donations.models import Donor, Ngo
7+
from donations.views.dashboard.stats_helpers.utils import cache_set
78

89

910
def _cache_key_for_metric(metric_name: str) -> str:
@@ -50,7 +51,7 @@ def current_year_redirections():
5051
"timestamp": now(),
5152
}
5253

53-
cache.set(_cache_key_for_metric("current_year_redirections"), result)
54+
cache_set(_cache_key_for_metric("current_year_redirections"), result)
5455

5556
return result
5657

@@ -62,7 +63,7 @@ def all_redirections():
6263
"timestamp": now(),
6364
}
6465

65-
cache.set(_cache_key_for_metric("all_redirections"), result)
66+
cache_set(_cache_key_for_metric("all_redirections"), result)
6667

6768
return result
6869

@@ -74,7 +75,7 @@ def all_active_ngos():
7475
"timestamp": now(),
7576
}
7677

77-
cache.set(_cache_key_for_metric("all_active_ngos"), result)
78+
cache_set(_cache_key_for_metric("all_active_ngos"), result)
7879

7980
return result
8081

@@ -86,7 +87,7 @@ def ngos_active_in_current_year():
8687
"timestamp": now(),
8788
}
8889

89-
cache.set(_cache_key_for_metric("ngos_active_in_current_year"), result)
90+
cache_set(_cache_key_for_metric("ngos_active_in_current_year"), result)
9091

9192
return result
9293

@@ -98,6 +99,6 @@ def ngos_with_ngo_hub():
9899
"timestamp": now(),
99100
}
100101

101-
cache.set(_cache_key_for_metric("ngos_with_ngo_hub"), result)
102+
cache_set(_cache_key_for_metric("ngos_with_ngo_hub"), result)
102103

103104
return result
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.core.cache import cache
2+
3+
4+
def cache_set(key: str, value: dict) -> None:
5+
"""
6+
Sets a value in the cache with a specified key.
7+
The timeout is set to None, meaning the cache will not expire.
8+
"""
9+
cache.set(key, value, timeout=None)

backend/donations/views/dashboard/stats_helpers_yearly.py renamed to backend/donations/views/dashboard/stats_helpers/yearly.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django_q.tasks import async_task
88

99
from donations.models import Donor, Ngo
10-
10+
from donations.views.dashboard.stats_helpers.utils import cache_set
1111

1212
STATS_FOR_YEAR_CACHE_PREFIX = "STATS_FOR_YEAR_"
1313

@@ -92,6 +92,6 @@ def _update_stats_for_year(year: int, cache_key: str, current_time: datetime) ->
9292
"timestamp": current_time,
9393
}
9494

95-
cache.set(cache_key, statistic, timeout=settings.TIMEOUT_CACHE_NORMAL)
95+
cache_set(cache_key, statistic, timeout=settings.TIMEOUT_CACHE_NORMAL)
9696

9797
return statistic

0 commit comments

Comments
 (0)