From 0e2c543979dfd06a40c1b34a6fb8eda2e92c8771 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Wed, 2 Nov 2011 17:45:32 +0000 Subject: [PATCH] Fixed #16967 -- Made sure CachedStaticFilesStorage repopulates its cache if there was a miss (for example if the cache server went down). git-svn-id: http://code.djangoproject.com/svn/django/trunk@17067 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/staticfiles/storage.py | 13 ++++++++----- tests/regressiontests/staticfiles_tests/tests.py | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/django/contrib/staticfiles/storage.py b/django/contrib/staticfiles/storage.py index cf0c4a0e3a9..ecb6769973a 100644 --- a/django/contrib/staticfiles/storage.py +++ b/django/contrib/staticfiles/storage.py @@ -90,11 +90,14 @@ class CachedFilesMixin(object): Returns the real URL in DEBUG mode. """ if settings.DEBUG and not force: - return super(CachedFilesMixin, self).url(name) - cache_key = self.cache_key(name) - hashed_name = self.cache.get(cache_key) - if hashed_name is None: - hashed_name = self.hashed_name(name) + hashed_name = name + else: + cache_key = self.cache_key(name) + hashed_name = self.cache.get(cache_key) + if hashed_name is None: + hashed_name = self.hashed_name(name) + # set the cache if there was a miss (e.g. if cache server goes down) + self.cache.set(cache_key, hashed_name) return super(CachedFilesMixin, self).url(hashed_name) def url_converter(self, name): diff --git a/tests/regressiontests/staticfiles_tests/tests.py b/tests/regressiontests/staticfiles_tests/tests.py index f12914f642e..67977e70d1f 100644 --- a/tests/regressiontests/staticfiles_tests/tests.py +++ b/tests/regressiontests/staticfiles_tests/tests.py @@ -352,6 +352,22 @@ class TestCollectionCachedStorage(BaseCollectionTestCase, with storage.staticfiles_storage.open(relpath) as relfile: self.assertTrue("https://" in relfile.read()) + def test_cache_invalidation(self): + name = "cached/styles.css" + hashed_name = "cached/styles.93b1147e8552.css" + # check if the cache is filled correctly as expected + cache_key = storage.staticfiles_storage.cache_key(name) + cached_name = storage.staticfiles_storage.cache.get(cache_key) + self.assertEqual(self.cached_file_path(name), cached_name) + # clearing the cache to make sure we re-set it correctly in the url method + storage.staticfiles_storage.cache.clear() + cached_name = storage.staticfiles_storage.cache.get(cache_key) + self.assertEqual(cached_name, None) + self.assertEqual(self.cached_file_path(name), hashed_name) + cached_name = storage.staticfiles_storage.cache.get(cache_key) + self.assertEqual(cached_name, hashed_name) + + # we set DEBUG to False here since the template tag wouldn't work otherwise TestCollectionCachedStorage = override_settings(**dict(TEST_SETTINGS, STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage',