Skip to content

Commit dac751b

Browse files
authored
Fix the caching of featured projects (#7054)
* Fix the caching of featured projects Currently the `if` block actually evaluates the queryset, causing it never to actually cache the query that we want it to cache. * Add homepage cache tests * Don’t test logged in queries because they vary too much. * Clear cache on both sides
1 parent 35b5ca2 commit dac751b

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

readthedocs/rtd_tests/tests/test_views.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.urls import reverse
88
from django.utils import timezone
99
from django_dynamic_fixture import get, new
10+
from django.core.cache import cache
1011

1112
from readthedocs.builds.constants import EXTERNAL, LATEST
1213
from readthedocs.builds.models import Build, Version
@@ -359,3 +360,22 @@ def test_generated_csv_data(self):
359360
self.assertEqual(len(body), 23)
360361
self.assertEqual(body[0][1], 'advertising')
361362
self.assertEqual(body[-1][1], 'hello world')
363+
364+
365+
class TestHomepageCache(TestCase):
366+
367+
def setUp(self):
368+
cache.clear()
369+
370+
def tearDown(self):
371+
cache.clear()
372+
373+
def test_homepage_queries(self):
374+
with self.assertNumQueries(1):
375+
r = self.client.get('/')
376+
self.assertEqual(r.status_code, 200)
377+
378+
# Cache
379+
with self.assertNumQueries(0):
380+
r = self.client.get('/')
381+
self.assertEqual(r.status_code, 200)

readthedocs/templates/homepage.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ <h3>Multiple versions</h3>
114114
{% include "core/widesearchbar.html" %}
115115
</section>
116116

117-
{% if featured_list %}
118117
{% get_current_language as language %}
119118
{% cache 600 homepage_featured_list language %}
119+
{% if featured_list %}
120120
<!-- BEGIN projects list -->
121121
<section>
122122
<h3>{% trans "Featured Projects" %}</h3>
@@ -129,8 +129,8 @@ <h3>{% trans "Featured Projects" %}</h3>
129129
</div>
130130
</section>
131131
<!-- END projects list -->
132-
{% endcache %}
133132
{% endif %}
133+
{% endcache %}
134134

135135
<!-- Funding and Contributing -->
136136
<section>

0 commit comments

Comments
 (0)