Skip to content

Commit 64ec030

Browse files
berinhardewdurbin
andauthored
Improves response time of sponsors page (#1948)
* Enable django's caching on sponsors list page * Use inner join to also fetch for package and reduce query execution time * Clean-up sponsors list cache after any sponsorship approval * Cache sponsor page for only one day * enable django caching via DB Co-authored-by: Ee Durbin <[email protected]> Co-authored-by: Ee Durbin <[email protected]>
1 parent b4efb1e commit 64ec030

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

pydotorg/settings/heroku.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
DATABASE_CONN_MAX_AGE = 600
1212
DATABASES['default']['CONN_MAX_AGE'] = DATABASE_CONN_MAX_AGE
1313

14+
## Django Caching
15+
16+
CACHES = {
17+
'default': {
18+
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
19+
'LOCATION': 'django_cache_table',
20+
}
21+
}
22+
1423
HAYSTACK_SEARCHBOX_SSL_URL = config(
1524
'SEARCHBOX_SSL_URL'
1625
)

sponsors/notifications.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.core.mail import EmailMessage
2+
from django.core.cache import cache
23
from django.template.loader import render_to_string
34
from django.conf import settings
45
from django.contrib.admin.models import LogEntry, CHANGE, ADDITION
@@ -209,3 +210,9 @@ def notify(self, notification, sponsorship, contact_types, request, **kwargs):
209210
action_flag=CHANGE,
210211
change_message=msg
211212
)
213+
214+
215+
class RefreshSponsorshipsCache:
216+
def notify(self, *args, **kwargs):
217+
# clean up cached used by "sponsors/partials/sponsors-list.html"
218+
cache.delete("CACHED_SPONSORS_LIST")

sponsors/templatetags/sponsors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from collections import OrderedDict
22
from django import template
3+
from django.conf import settings
4+
from django.core.cache import cache
35

46
from ..models import Sponsorship, SponsorshipPackage, TieredQuantityConfiguration
57
from sponsors.models.enums import PublisherChoices, LogoPlacementChoices
@@ -23,7 +25,7 @@ def full_sponsorship(sponsorship, display_fee=False):
2325
def list_sponsors(logo_place, publisher=PublisherChoices.FOUNDATION.value):
2426
sponsorships = Sponsorship.objects.enabled().with_logo_placement(
2527
logo_place=logo_place, publisher=publisher
26-
).order_by('package').select_related('sponsor')
28+
).order_by('package').select_related('sponsor', 'package')
2729
packages = SponsorshipPackage.objects.all()
2830

2931
context = {
@@ -50,6 +52,7 @@ def list_sponsors(logo_place, publisher=PublisherChoices.FOUNDATION.value):
5052
'packages': SponsorshipPackage.objects.all(),
5153
'sponsorships_by_package': sponsorships_by_package,
5254
})
55+
5356
return context
5457

5558

sponsors/tests/test_use_cases.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,13 @@ def test_execute_and_update_database_object(self):
187187

188188
def test_build_use_case_with_default_notificationss(self):
189189
uc = use_cases.ExecuteContractUseCase.build()
190-
self.assertEqual(len(uc.notifications), 1)
190+
self.assertEqual(len(uc.notifications), 2)
191191
self.assertIsInstance(
192192
uc.notifications[0], ExecutedContractLogger
193193
)
194+
self.assertIsInstance(
195+
uc.notifications[1], RefreshSponsorshipsCache,
196+
)
194197

195198

196199
class ExecuteExistingContractUseCaseTests(TestCase):
@@ -219,10 +222,13 @@ def test_execute_and_update_database_object(self):
219222

220223
def test_build_use_case_with_default_notificationss(self):
221224
uc = use_cases.ExecuteExistingContractUseCase.build()
222-
self.assertEqual(len(uc.notifications), 1)
225+
self.assertEqual(len(uc.notifications), 2)
223226
self.assertIsInstance(
224227
uc.notifications[0], ExecutedExistingContractLogger
225228
)
229+
self.assertIsInstance(
230+
uc.notifications[1], RefreshSponsorshipsCache,
231+
)
226232

227233

228234
class NullifyContractUseCaseTests(TestCase):
@@ -239,10 +245,13 @@ def test_nullify_and_update_database_object(self):
239245

240246
def test_build_use_case_with_default_notificationss(self):
241247
uc = use_cases.NullifyContractUseCase.build()
242-
self.assertEqual(len(uc.notifications), 1)
248+
self.assertEqual(len(uc.notifications), 2)
243249
self.assertIsInstance(
244250
uc.notifications[0], NullifiedContractLogger
245251
)
252+
self.assertIsInstance(
253+
uc.notifications[1], RefreshSponsorshipsCache,
254+
)
246255

247256

248257
class SendSponsorshipNotificationUseCaseTests(TestCase):

sponsors/use_cases.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def execute(self, contract, **kwargs):
9494
class ExecuteExistingContractUseCase(BaseUseCaseWithNotifications):
9595
notifications = [
9696
notifications.ExecutedExistingContractLogger(),
97+
notifications.RefreshSponsorshipsCache(),
9798
]
9899
force_execute = True
99100

@@ -109,13 +110,15 @@ def execute(self, contract, contract_file, **kwargs):
109110
class ExecuteContractUseCase(ExecuteExistingContractUseCase):
110111
notifications = [
111112
notifications.ExecutedContractLogger(),
113+
notifications.RefreshSponsorshipsCache(),
112114
]
113115
force_execute = False
114116

115117

116118
class NullifyContractUseCase(BaseUseCaseWithNotifications):
117119
notifications = [
118120
notifications.NullifiedContractLogger(),
121+
notifications.RefreshSponsorshipsCache(),
119122
]
120123

121124
def execute(self, contract, **kwargs):

templates/sponsors/partials/sponsors-list.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
{% load thumbnail %}
2+
{% load cache %}
3+
4+
{% comment %}cache for 1 day{% endcomment %}
5+
{% cache 86400 CACHED_SPONSORS_LIST %}
26

37
{% if logo_place == "download" %}
48
<h2 class="widget-title">Sponsors</h2>
@@ -51,3 +55,4 @@ <h1 style="font-size: {% if forloop.first %}350%{% else %}300%{% endif %}">{{ pa
5155
{% endif %}
5256
{% endfor %}
5357
{% endif %}
58+
{% endcache CACHED_SPONSORS_LIST %}

0 commit comments

Comments
 (0)