Ticket #14596: cache-refactor-initialization-and-cull-frequency-test.diff

File cache-refactor-initialization-and-cull-frequency-test.diff, 4.5 KB (added by dauerbaustelle, 14 years ago)

Refactors cache backend initialization and adds cull frequency test.

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

    diff -r d578c187d733 django/core/cache/backends/base.py
    a b  
    2222            timeout = 300
    2323        self.default_timeout = timeout
    2424
     25        max_entries = params.get('max_entries', 300)
     26        try:
     27            self._max_entries = int(max_entries)
     28        except (ValueError, TypeError):
     29            self._max_entries = 300
     30
     31        cull_frequency = params.get('cull_frequency', 3)
     32        try:
     33            self._cull_frequency = int(cull_frequency)
     34        except (ValueError, TypeError):
     35            self._cull_frequency = 3
     36
    2537    def add(self, key, value, timeout=None):
    2638        """
    2739        Set a value in the cache if the key does not already exist. If
  • django/core/cache/backends/db.py

    diff -r d578c187d733 django/core/cache/backends/db.py
    a b  
    2525        self.managed = True
    2626        self.proxy = False
    2727
    28 class CacheClass(BaseCache):
     28class BaseDatabaseCacheClass(BaseCache):
    2929    def __init__(self, table, params):
    3030        BaseCache.__init__(self, params)
    3131        self._table = table
     
    3434            _meta = Options(table)
    3535        self.cache_model_class = CacheEntry
    3636
    37         max_entries = params.get('max_entries', 300)
    38         try:
    39             self._max_entries = int(max_entries)
    40         except (ValueError, TypeError):
    41             self._max_entries = 300
    42         cull_frequency = params.get('cull_frequency', 3)
    43         try:
    44             self._cull_frequency = int(cull_frequency)
    45         except (ValueError, TypeError):
    46             self._cull_frequency = 3
    47 
     37class CacheClass(BaseDatabaseCacheClass):
    4838    def get(self, key, default=None):
    4939        self.validate_key(key)
    5040        db = router.db_for_read(self.cache_model_class)
  • django/core/cache/backends/filebased.py

    diff -r d578c187d733 django/core/cache/backends/filebased.py
    a b  
    1414class CacheClass(BaseCache):
    1515    def __init__(self, dir, params):
    1616        BaseCache.__init__(self, params)
    17 
    18         max_entries = params.get('max_entries', 300)
    19         try:
    20             self._max_entries = int(max_entries)
    21         except (ValueError, TypeError):
    22             self._max_entries = 300
    23 
    24         cull_frequency = params.get('cull_frequency', 3)
    25         try:
    26             self._cull_frequency = int(cull_frequency)
    27         except (ValueError, TypeError):
    28             self._cull_frequency = 3
    29 
    3017        self._dir = dir
    3118        if not os.path.exists(self._dir):
    3219            self._createdir()
  • django/core/cache/backends/locmem.py

    diff -r d578c187d733 django/core/cache/backends/locmem.py
    a b  
    1414        BaseCache.__init__(self, params)
    1515        self._cache = {}
    1616        self._expire_info = {}
    17 
    18         max_entries = params.get('max_entries', 300)
    19         try:
    20             self._max_entries = int(max_entries)
    21         except (ValueError, TypeError):
    22             self._max_entries = 300
    23 
    24         cull_frequency = params.get('cull_frequency', 3)
    25         try:
    26             self._cull_frequency = int(cull_frequency)
    27         except (ValueError, TypeError):
    28             self._cull_frequency = 3
    29 
    3017        self._lock = RWLock()
    3118
    3219    def add(self, key, value, timeout=None):
  • tests/regressiontests/cache/tests.py

    diff -r d578c187d733 tests/regressiontests/cache/tests.py
    a b  
    410410    def test_cull(self):
    411411        self.perform_cull_test(50, 29)
    412412
     413    def test_zero_cull(self):
     414        self.cache = get_cache('db://%s?max_entries=30&cull_frequency=0' % self._table_name)
     415        self.perform_cull_test(50, 18)
     416
    413417class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
    414418    def setUp(self):
    415419        self.cache = get_cache('locmem://?max_entries=30')
     
    417421    def test_cull(self):
    418422        self.perform_cull_test(50, 29)
    419423
     424    def test_zero_cull(self):
     425        self.cache = get_cache('locmem://?max_entries=30&cull_frequency=0')
     426        self.perform_cull_test(50, 19)
     427
    420428# memcached backend isn't guaranteed to be available.
    421429# To check the memcached backend, the test settings file will
    422430# need to contain a CACHE_BACKEND setting that points at
Back to Top