Ticket #15825: merged_patch.diff

File merged_patch.diff, 4.1 KB (added by desh, 4 years ago)

Patch for both #15825 and #15806 (as they address same functions)

  • django/core/cache/backends/filebased.py

    diff --git a/django/core/cache/backends/filebased.py b/django/core/cache/backends/filebased.py
    index 57bb63e..9cbd8b0 100644
    a b import hashlib 
    44import os
    55import shutil
    66import time
     7import random
    78try:
    89    import cPickle as pickle
    910except ImportError:
    class FileBasedCache(BaseCache): 
    113114            return
    114115
    115116        try:
    116             filelist = sorted(os.listdir(self._dir))
     117            filelist = os.listdir(self._dir)
    117118        except (IOError, OSError):
    118119            return
    119120
    120121        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]
    124130
    125131        for topdir in doomed:
    126132            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)
    130134            except (IOError, OSError):
    131135                pass
    132136
  • tests/regressiontests/cache/tests.py

    diff --git a/tests/regressiontests/cache/tests.py b/tests/regressiontests/cache/tests.py
    index c5349d5..a0acb75 100644
    a b class BaseCacheTests(object): 
    408408        self.assertEqual(self.cache.get('key3'), 'sausage')
    409409        self.assertEqual(self.cache.get('key4'), 'lobster bisque')
    410410
    411     def perform_cull_test(self, initial_count, final_count):
     411    def perform_cull_test(self, initial_count, final_count, cache=None):
    412412        """This is implemented as a utility method, because only some of the backends
    413413        implement culling. The culling algorithm also varies slightly, so the final
    414414        number of entries will vary between backends"""
     415        if cache is None:
     416            cache = self.cache
     417
    415418        # Create initial cache key entries. This will overflow the cache, causing a cull
    416419        for i in range(1, initial_count):
    417             self.cache.set('cull%d' % i, 'value', 1000)
     420            cache.set('cull-%d' % i, 'value', 1000)
    418421        count = 0
    419422        # Count how many keys are left in the cache.
    420423        for i in range(1, initial_count):
    421             if self.cache.has_key('cull%d' % i):
     424            if cache.has_key('cull-%d' % i):
    422425                count = count + 1
    423426        self.assertEqual(count, final_count)
    424427
    class FileBasedCacheTests(unittest.TestCase, BaseCacheTests): 
    848851    def setUp(self):
    849852        self.dirname = tempfile.mkdtemp()
    850853        self.cache = get_cache(self.backend_name, LOCATION=self.dirname, OPTIONS={'MAX_ENTRIES': 30})
     854        self.full_cull_cache = get_cache(self.backend_name,
     855                LOCATION=self.dirname,
     856                OPTIONS={'MAX_ENTRIES': 30, 'CULL_FREQUENCY': 0})
    851857        self.prefix_cache = get_cache(self.backend_name, LOCATION=self.dirname, KEY_PREFIX='cacheprefix')
    852858        self.v2_cache = get_cache(self.backend_name, LOCATION=self.dirname, VERSION=2)
    853859        self.custom_key_cache = get_cache(self.backend_name, LOCATION=self.dirname, KEY_FUNCTION=custom_key_func)
    class FileBasedCacheTests(unittest.TestCase, BaseCacheTests): 
    879885        self.assertTrue(not os.path.exists(os.path.dirname(keypath)))
    880886        self.assertTrue(not os.path.exists(os.path.dirname(os.path.dirname(keypath))))
    881887
     888    def test_full_cull(self):
     889        self.perform_cull_test(32, 1, self.full_cull_cache)
     890
    882891    def test_cull(self):
    883         self.perform_cull_test(50, 29)
     892        self.perform_cull_test(35, 24)
    884893
    885894    def test_old_initialization(self):
    886895        self.cache = get_cache('file://%s?max_entries=30' % self.dirname)
    887         self.perform_cull_test(50, 29)
     896        self.perform_cull_test(35, 24)
    888897
    889898class CustomCacheKeyValidationTests(unittest.TestCase):
    890899    """
Back to Top