Commits

Anonymous committed 0e692a1

fix FileSystemCache.clear() confusing cache key vs. cache filename

using a new internal method _list_dir() now to get a list of cache filenames
(absolute filenames). this method will also be used later to filter out
the temp files we will need when updating the cache using a tempfile and
a atomic rename.

added a test for FileSystemCache.clear()

  • Participants
  • Parent commits 59be84c

Comments (0)

Files changed (2)

tests/contrib/test_cache.py

     shutil.rmtree(tmp_dir)
     assert len(cache_files) <= THRESHOLD
 
+
+def test_filesystemcache_clear():
+    """
+    test if FileSystemCache.clear works
+    """
+    tmp_dir = tempfile.mkdtemp()
+    cache = FileSystemCache(cache_dir=tmp_dir)
+    cache.set('foo', 'bar')
+    cache_files = os.listdir(tmp_dir)
+    assert len(cache_files) == 1
+    cache.clear()
+    cache_files = os.listdir(tmp_dir)
+    assert len(cache_files) == 0
+    shutil.rmtree(tmp_dir)
+

werkzeug/contrib/cache.py

         if not os.path.exists(self._path):
             os.makedirs(self._path)
 
+    def _list_dir(self):
+        """return a list of (fully qualified) cache filenames
+        """
+        return [os.path.join(self._path, fn) for fn in os.listdir(self._path)]
+
     def _prune(self):
-        entries = os.listdir(self._path)
+        entries = self._list_dir()
         if len(entries) > self._threshold:
             now = time()
             for idx, fname in enumerate(entries):
                 remove = False
-                fname = os.path.join(self._path, fname)
                 f = None
                 try:
                     try:
                     except (IOError, OSError):
                         pass
 
+    def clear(self):
+        for fname in self._list_dir():
+            try:
+                os.remove(fname)
+            except (IOError, OSError):
+                pass
+
     def _get_filename(self, key):
         hash = md5(key).hexdigest()
         return os.path.join(self._path, hash)
         except (IOError, OSError):
             pass
 
-    def clear(self):
-        for key in os.listdir(self._path):
-            self.delete(key)