From 1254d88c8073a8eda0bea870bafcad2414f612e0 Mon Sep 17 00:00:00 2001 From: Alastair Weakley Date: Mon, 29 Jul 2024 11:23:08 +1000 Subject: [PATCH] Fix reversion from rebase --- testproject/home/tests.py | 17 ----------------- wagtailcache/cache.py | 19 ++++++++++--------- 2 files changed, 10 insertions(+), 26 deletions(-) diff --git a/testproject/home/tests.py b/testproject/home/tests.py index 5b1afa2..5ec298f 100644 --- a/testproject/home/tests.py +++ b/testproject/home/tests.py @@ -524,23 +524,6 @@ def test_cache_keyring(self): # Compare Keys self.assertEqual(keyring_item.url, url) - @override_settings(WAGTAIL_CACHE_BACKEND="one_second") - def test_cache_keyring_no_uri_key_duplication(self): - # First get to populate keyring - self.get_miss(self.page_cachedpage.get_url()) - # Wait a short time - time.sleep(0.5) - # Fetch a different page - self.get_miss(self.page_wagtailpage.get_url()) - # Wait until the first page is expired, but not the keyring - time.sleep(0.6) - # Fetch the first page again - self.get_miss(self.page_cachedpage.get_url()) - # Check the keyring does not contain duplicate uri_keys - url = "http://%s%s" % ("testserver", self.page_cachedpage.get_url()) - keyring = self.cache.get("keyring") - self.assertEqual(len(keyring.get(url, [])), 1) - def test_clear_cache(self): # First get should miss cache. self.get_miss(self.page_cachedpage.get_url()) diff --git a/wagtailcache/cache.py b/wagtailcache/cache.py index ca20f76..1456c04 100644 --- a/wagtailcache/cache.py +++ b/wagtailcache/cache.py @@ -2,6 +2,7 @@ Functionality to set, serve from, and clear the cache. """ +import datetime import logging import re from enum import Enum @@ -24,6 +25,7 @@ from django.utils.cache import learn_cache_key from django.utils.cache import patch_response_headers from django.utils.deprecation import MiddlewareMixin +from django.utils.timezone import now from wagtail import hooks from wagtailcache.models import KeyringItem @@ -340,15 +342,14 @@ def process_response( # (of the chopped request, not the real one). cr = _chop_querystring(request) uri = unquote(cr.build_absolute_uri()) - keyring = self._wagcache.get("keyring", {}) - # Get current cache keys belonging to this URI. - # This should be a list of keys. - uri_keys: List[str] = keyring.get(uri, []) - # Append the key to this list if not already present and save. - if cache_key not in uri_keys: - uri_keys.append(cache_key) - keyring[uri] = uri_keys - self._wagcache.set("keyring", keyring) + + expiry = now() + datetime.timedelta(seconds=timeout) + KeyringItem.objects.set( + expiry=expiry, + key=cache_key, + url=uri, + ) + if isinstance(response, SimpleTemplateResponse): def callback(r):