diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
index 57bb63e..9cbd8b0 100644
a
|
b
|
import hashlib
|
4 | 4 | import os |
5 | 5 | import shutil |
6 | 6 | import time |
| 7 | import random |
7 | 8 | try: |
8 | 9 | import cPickle as pickle |
9 | 10 | except ImportError: |
… |
… |
class FileBasedCache(BaseCache):
|
113 | 114 | return |
114 | 115 | |
115 | 116 | try: |
116 | | filelist = sorted(os.listdir(self._dir)) |
| 117 | filelist = os.listdir(self._dir) |
117 | 118 | except (IOError, OSError): |
118 | 119 | return |
119 | 120 | |
120 | 121 | if self._cull_frequency == 0: |
121 | | doomed = filelist |
122 | | else: |
123 | | doomed = [os.path.join(self._dir, k) for (i, k) in enumerate(filelist) if i % self._cull_frequency == 0] |
| 122 | self.clear() |
| 123 | return |
| 124 | |
| 125 | ranno = random.randint(0, self._cull_frequency) |
| 126 | doomed = [ |
| 127 | os.path.join(self._dir, k) |
| 128 | for (i, k) in enumerate(filelist) |
| 129 | if i % self._cull_frequency == ranno] |
124 | 130 | |
125 | 131 | for topdir in doomed: |
126 | 132 | try: |
127 | | for root, _, files in os.walk(topdir): |
128 | | for f in files: |
129 | | self._delete(os.path.join(root, f)) |
| 133 | shutil.rmtree(topdir) |
130 | 134 | except (IOError, OSError): |
131 | 135 | pass |
132 | 136 | |
diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
index c5349d5..fd9d7c8 100644
a
|
b
|
class BaseCacheTests(object):
|
414 | 414 | number of entries will vary between backends""" |
415 | 415 | # Create initial cache key entries. This will overflow the cache, causing a cull |
416 | 416 | for i in range(1, initial_count): |
417 | | self.cache.set('cull%d' % i, 'value', 1000) |
| 417 | self.cache.set('cull-%d' % i, 'value', 1000) |
418 | 418 | count = 0 |
419 | 419 | # Count how many keys are left in the cache. |
420 | 420 | for i in range(1, initial_count): |
421 | | if self.cache.has_key('cull%d' % i): |
| 421 | if self.cache.has_key('cull-%d' % i): |
422 | 422 | count = count + 1 |
423 | 423 | self.assertEqual(count, final_count) |
424 | 424 | |
… |
… |
class FileBasedCacheTests(unittest.TestCase, BaseCacheTests):
|
880 | 880 | self.assertTrue(not os.path.exists(os.path.dirname(os.path.dirname(keypath)))) |
881 | 881 | |
882 | 882 | def test_cull(self): |
883 | | self.perform_cull_test(50, 29) |
| 883 | self.perform_cull_test(35, 24) |
884 | 884 | |
885 | 885 | def test_old_initialization(self): |
886 | 886 | self.cache = get_cache('file://%s?max_entries=30' % self.dirname) |
887 | | self.perform_cull_test(50, 29) |
| 887 | self.perform_cull_test(35, 24) |
888 | 888 | |
889 | 889 | class CustomCacheKeyValidationTests(unittest.TestCase): |
890 | 890 | """ |