Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16967 -- Made sure CachedStaticFilesStorage repopulates its ca…

…che 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
  • Loading branch information...
commit 0e2c543979dfd06a40c1b34a6fb8eda2e92c8771 1 parent 44a4037
Jannis Leidel authored November 02, 2011
13  django/contrib/staticfiles/storage.py
@@ -90,11 +90,14 @@ def url(self, name, force=False):
90 90
         Returns the real URL in DEBUG mode.
91 91
         """
92 92
         if settings.DEBUG and not force:
93  
-            return super(CachedFilesMixin, self).url(name)
94  
-        cache_key = self.cache_key(name)
95  
-        hashed_name = self.cache.get(cache_key)
96  
-        if hashed_name is None:
97  
-            hashed_name = self.hashed_name(name)
  93
+            hashed_name = name
  94
+        else:
  95
+            cache_key = self.cache_key(name)
  96
+            hashed_name = self.cache.get(cache_key)
  97
+            if hashed_name is None:
  98
+                hashed_name = self.hashed_name(name)
  99
+                # set the cache if there was a miss (e.g. if cache server goes down)
  100
+                self.cache.set(cache_key, hashed_name)
98 101
         return super(CachedFilesMixin, self).url(hashed_name)
99 102
 
100 103
     def url_converter(self, name):
16  tests/regressiontests/staticfiles_tests/tests.py
@@ -352,6 +352,22 @@ def test_template_tag_url(self):
352 352
         with storage.staticfiles_storage.open(relpath) as relfile:
353 353
             self.assertTrue("https://" in relfile.read())
354 354
 
  355
+    def test_cache_invalidation(self):
  356
+        name = "cached/styles.css"
  357
+        hashed_name = "cached/styles.93b1147e8552.css"
  358
+        # check if the cache is filled correctly as expected
  359
+        cache_key = storage.staticfiles_storage.cache_key(name)
  360
+        cached_name = storage.staticfiles_storage.cache.get(cache_key)
  361
+        self.assertEqual(self.cached_file_path(name), cached_name)
  362
+        # clearing the cache to make sure we re-set it correctly in the url method
  363
+        storage.staticfiles_storage.cache.clear()
  364
+        cached_name = storage.staticfiles_storage.cache.get(cache_key)
  365
+        self.assertEqual(cached_name, None)
  366
+        self.assertEqual(self.cached_file_path(name), hashed_name)
  367
+        cached_name = storage.staticfiles_storage.cache.get(cache_key)
  368
+        self.assertEqual(cached_name, hashed_name)
  369
+
  370
+
355 371
 # we set DEBUG to False here since the template tag wouldn't work otherwise
356 372
 TestCollectionCachedStorage = override_settings(**dict(TEST_SETTINGS,
357 373
     STATICFILES_STORAGE='django.contrib.staticfiles.storage.CachedStaticFilesStorage',

0 notes on commit 0e2c543

Please sign in to comment.
Something went wrong with that request. Please try again.