Skip to content

Commit 2365130

Browse files
jonatronvsalvino
andauthored
Disable keyring by default, add setting to enable (#83)
Co-authored-by: Vince Salvino <[email protected]>
1 parent 255bc95 commit 2365130

File tree

7 files changed

+69
-32
lines changed

7 files changed

+69
-32
lines changed

ci/compare-codecov.ps1

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ $ApiBase = "https://dev.azure.com/$org/$project"
3939

4040
# ---- GET CODE COVERAGE FROM RECENT BUILD -------------------------------------
4141

42+
$mainBuildUrl = "$ApiBase/_apis/build/builds?branchName=refs/heads/main&api-version=5.1"
43+
Write-Host "mainBuildUrl: $mainBuildUrl"
4244

4345
# Get list of all recent builds.
4446
$mainBuildJson = (
45-
Invoke-WebRequest "$ApiBase/_apis/build/builds?branchName=refs/heads/main&api-version=5.1"
47+
Invoke-WebRequest "$mainBuildUrl"
4648
).Content | ConvertFrom-Json
4749

4850
# Get the latest matching build ID from the list of builds.
@@ -53,9 +55,17 @@ foreach ($build in $mainBuildJson.value) {
5355
}
5456
}
5557

58+
$mainCoverageUrl = "$ApiBase/_apis/test/codecoverage?buildId=$mainLatestId&flags=7&api-version=7.1"
59+
Write-Host "mainCoverageUrl: $mainCoverageUrl"
60+
61+
if($mainLatestId -eq $null) {
62+
Write-Host "No main coverage, build may have been cleaned up"
63+
exit 0
64+
}
65+
5666
# Retrieve code coverage for this build ID.
5767
$mainCoverageJson = (
58-
Invoke-WebRequest "$ApiBase/_apis/test/codecoverage?buildId=$mainLatestId&flags=7&api-version=7.1-preview.1"
68+
Invoke-WebRequest $mainCoverageUrl
5969
).Content | ConvertFrom-Json
6070
foreach ($cov in $mainCoverageJson.coverageData.coverageStats) {
6171
if ($cov.label -eq "Lines") {

docs/getting_started/django_settings.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,14 @@ own unique page in the cache, set this value to ``None`` or ``[]``.
100100
If you feel as though the spammers have won, and want the nuclear option, you
101101
can set this to ``[r".*"]`` which will ignore all querystrings. This is surely
102102
a terrible idea, but it can be done.
103+
104+
WAGTAIL_CACHE_KEYRING
105+
---------------------
106+
107+
.. versionadded:: 3.0
108+
109+
Set to ``True`` to enable the keyring, which allows you to purge specific URLs. Defaults to ``False``.
110+
111+
.. note::
112+
113+
Enabling the keyring will reduce the performance of the cache. Only enable this if you need to purge specific URLs before they are set to expire.

docs/getting_started/usage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ publishing a page. To accomplish this, use a Wagtail hook as so
5252
Purge specific URLs
5353
-------------------
5454

55+
Requires ``WAGTAIL_CACHE_KEYRING=True``
56+
5557
Sometimes you only want to delete specific pages in the cache automatically
5658
after publishing a page. To achieve this, use a Wagtail hook as follows
5759

testproject/home/tests.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ def test_admin_clearcache(self):
538538

539539
# ---- PURGE SPECIFIC URLS & CLEAR ALL--------------------------------------
540540

541+
@override_settings(WAGTAIL_CACHE_KEYRING=True)
541542
def test_cache_keyring(self):
542543
# Check if keyring is not present
543544
self.assertEqual(self.cache.get("keyring"), None)
@@ -549,7 +550,9 @@ def test_cache_keyring(self):
549550
# Compare Keys
550551
self.assertEqual(key, url)
551552

552-
@override_settings(WAGTAIL_CACHE_BACKEND="one_second")
553+
@override_settings(
554+
WAGTAIL_CACHE_BACKEND="one_second", WAGTAIL_CACHE_KEYRING=True
555+
)
553556
def test_cache_keyring_no_uri_key_duplication(self):
554557
# First get to populate keyring
555558
self.get_miss(self.page_cachedpage.get_url())
@@ -576,6 +579,7 @@ def test_clear_cache(self):
576579
# Now the page should miss cache.
577580
self.get_miss(self.page_cachedpage.get_url())
578581

582+
@override_settings(WAGTAIL_CACHE_KEYRING=True)
579583
def test_clear_cache_url(self):
580584
u1 = self.page_cachedpage.get_url()
581585
u2 = self.page_cachedpage.get_url() + "?action=pytest"

wagtailcache/cache.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,18 @@ def process_response(
343343
# (of the chopped request, not the real one).
344344
cr = _chop_querystring(request)
345345
uri = unquote(cr.build_absolute_uri())
346-
keyring = self._wagcache.get("keyring", {})
347-
# Get current cache keys belonging to this URI.
348-
# This should be a list of keys.
349-
uri_keys: List[str] = keyring.get(uri, [])
350-
# Append the key to this list if not already present and save.
351-
if cache_key not in uri_keys:
352-
uri_keys.append(cache_key)
353-
keyring[uri] = uri_keys
354-
self._wagcache.set("keyring", keyring)
346+
347+
if wagtailcache_settings.WAGTAIL_CACHE_KEYRING:
348+
keyring = self._wagcache.get("keyring", {})
349+
# Get current cache keys belonging to this URI.
350+
# This should be a list of keys.
351+
uri_keys: List[str] = keyring.get(uri, [])
352+
# Append the key to this list if not already present and save.
353+
if cache_key not in uri_keys:
354+
uri_keys.append(cache_key)
355+
keyring[uri] = uri_keys
356+
self._wagcache.set("keyring", keyring)
357+
355358
if isinstance(response, SimpleTemplateResponse):
356359

357360
def callback(r):
@@ -382,7 +385,11 @@ def clear_cache(urls: List[str] = []) -> None:
382385
return
383386

384387
_wagcache = caches[wagtailcache_settings.WAGTAIL_CACHE_BACKEND]
385-
if urls and "keyring" in _wagcache:
388+
if (
389+
urls
390+
and wagtailcache_settings.WAGTAIL_CACHE_KEYRING
391+
and "keyring" in _wagcache
392+
):
386393
keyring = _wagcache.get("keyring")
387394
# Check the provided URL matches a key in our keyring.
388395
matched_urls = []

wagtailcache/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class _DefaultSettings:
3434
r"^trk_.*$", # Listrak
3535
r"^utm_.*$", # Google Analytics
3636
]
37+
WAGTAIL_CACHE_KEYRING = False
3738

3839
def __getattribute__(self, attr: Text):
3940
# First load from Django settings.

wagtailcache/templates/wagtailcache/index.html

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,27 @@ <h2>{% trans "Status" %}</h2>
3333
{% trans "Clear cache" %}
3434
</a>
3535
</p>
36-
<br>
37-
<h2>{% trans "Contents" %}</h2>
38-
<p>{% trans "Number of URLs in the cache:" %} <b>{{ keyring|length }}</b></p>
39-
<p>
40-
{% trans "Below is each URL and the cache key for each response of that URL that is currently in the cache." %}
41-
{% trans "Note that 301/302 redirects and 404s may also be cached." %}
42-
</p>
43-
<ul class="wagcache-pagelist">
44-
{% for url, entries in keyring.items %}
45-
<li>
46-
{{ url }}
47-
<ul>
48-
{% for entry in entries %}
49-
<li>{{ entry }}</li>
50-
{% endfor %}
51-
</ul>
52-
</li>
53-
{% endfor %}
54-
</ul>
36+
{% if 'WAGTAIL_CACHE_KEYRING'|get_wagtailcache_setting %}
37+
<br>
38+
<h2>{% trans "Contents" %}</h2>
39+
<p>{% trans "Number of URLs in the cache:" %} <b>{{ keyring|length }}</b></p>
40+
<p>
41+
{% trans "Below is each URL and the cache key for each response of that URL that is currently in the cache." %}
42+
{% trans "Note that 301/302 redirects and 404s may also be cached." %}
43+
</p>
44+
<ul class="wagcache-pagelist">
45+
{% for url, entries in keyring.items %}
46+
<li>
47+
{{ url }}
48+
<ul>
49+
{% for entry in entries %}
50+
<li>{{ entry }}</li>
51+
{% endfor %}
52+
</ul>
53+
</li>
54+
{% endfor %}
55+
</ul>
56+
{% endif %}
5557
{% else %}
5658
<p>
5759
<b>{% trans "DISABLED" %}</b>

0 commit comments

Comments
 (0)