Ticket #21012: locmem.diff

File locmem.diff, 1.3 KB (added by apollo13, 18 months ago)
  • django/core/cache/backends/locmem.py

    diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
    index cab690b..f68546e 100644
    a b  
    11"Thread-safe in-memory cache backend."
    22
    33import time
     4import threading
    45try:
    56    from django.utils.six.moves import cPickle as pickle
    67except ImportError:
    from django.utils.synch import RWLock 
    1213
    1314# Global in-memory store of cache data. Keyed by name, to provide
    1415# multiple named local memory caches.
    15 _caches = {}
    16 _expire_info = {}
    17 _locks = {}
     16_cache_info = {}
     17_cache_info_lock = threading.Lock()
    1818
    1919
    2020class LocMemCache(BaseCache):
    2121    def __init__(self, name, params):
    2222        BaseCache.__init__(self, params)
    23         self._cache = _caches.setdefault(name, {})
    24         self._expire_info = _expire_info.setdefault(name, {})
    25         self._lock = _locks.setdefault(name, RWLock())
     23        if name in _cache_info:
     24            cache_info = _cache_info[name]
     25        else:
     26            with _cache_info_lock:
     27                cache_info = _cache_info.setdefault(name, ({}, {}, RWLock()))
     28        self._cache, self._expire_info, self._lock = cache_info
    2629
    2730    def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
    2831        key = self.make_key(key, version=version)
Back to Top