Code

Ticket #21012: locmem.diff

File locmem.diff, 1.3 KB (added by apollo13, 5 months ago)
Line 
1diff --git a/django/core/cache/backends/locmem.py b/django/core/cache/backends/locmem.py
2index cab690b..f68546e 100644
3--- a/django/core/cache/backends/locmem.py
4+++ b/django/core/cache/backends/locmem.py
5@@ -1,6 +1,7 @@
6 "Thread-safe in-memory cache backend."
7 
8 import time
9+import threading
10 try:
11     from django.utils.six.moves import cPickle as pickle
12 except ImportError:
13@@ -12,17 +13,19 @@ from django.utils.synch import RWLock
14 
15 # Global in-memory store of cache data. Keyed by name, to provide
16 # multiple named local memory caches.
17-_caches = {}
18-_expire_info = {}
19-_locks = {}
20+_cache_info = {}
21+_cache_info_lock = threading.Lock()
22 
23 
24 class LocMemCache(BaseCache):
25     def __init__(self, name, params):
26         BaseCache.__init__(self, params)
27-        self._cache = _caches.setdefault(name, {})
28-        self._expire_info = _expire_info.setdefault(name, {})
29-        self._lock = _locks.setdefault(name, RWLock())
30+        if name in _cache_info:
31+            cache_info = _cache_info[name]
32+        else:
33+            with _cache_info_lock:
34+                cache_info = _cache_info.setdefault(name, ({}, {}, RWLock()))
35+        self._cache, self._expire_info, self._lock = cache_info
36 
37     def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None):
38         key = self.make_key(key, version=version)