PK!Glrucache_backend/__init__.pyfrom __future__ import absolute_import, unicode_literals """django-lrucache-backend - A smarter local memory cache backend for Django""" from .backend import LRUObjectCache __version__ = "2.0.0" __author__ = "Josh Smeaton " __all__ = ["LRUObjectCache"] PK!(Эbblrucache_backend/backend.py# -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals import time from threading import RLock from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache from lru import LRU # dependency: lru-dict "Thread-safe in-memory LRU object cache backend." # Global in-memory store of cache data. Keyed by name, to provide # multiple named local memory caches. _caches = {} _locks = {} class LRUObjectCache(BaseCache): """ A local memory cache that: 1. Avoids serialization and deserialization 2. Implements an LRU eviction strategy 3. Implements timeouts on add()/get()/has_key() Uses a C based LRU dict for performance. See: https://github.com/amitdev/lru-dict Honours `MAX_ENTRIES` but not `CULL_FREQUENCY`, as the LRU algorithm will evict the least recently used value as required. """ def __init__(self, name, params): BaseCache.__init__(self, params) self._cache = _caches.setdefault(name, LRU(self._max_entries)) self._lock = _locks.setdefault(name, RLock()) def add(self, key, value, timeout=DEFAULT_TIMEOUT, version=None): new_key = self.make_key(key, version=version) with self._lock: if self._has_expired(key, version=version): self._set(new_key, value, timeout) return True return False def get(self, key, default=None, version=None): value, timeout = self._get(key, default, version) return value def _get(self, key, default=None, version=None): key = self.make_key(key, version=version) with self._lock: value, expiration = self._cache.get(key, (default, -1)) if not self._is_expired(expiration): return value, expiration with self._lock: try: del self._cache[key] except KeyError: pass return default, -1 def _set(self, key, value, timeout=DEFAULT_TIMEOUT): timeout = self.get_backend_timeout(timeout) self._cache[key] = (value, timeout) def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None): key = self.make_key(key, version=version) with self._lock: self._set(key, value, timeout) def incr(self, key, delta=1, version=None): with self._lock: # remain locked the entire time so the incr is guaranteed to be correct value, exp = self._get(key, version=version) if value is None: raise ValueError("Key '%s' not found" % key) new_value = value + delta new_key = self.make_key(key, version=version) self._cache[new_key] = (new_value, exp) return new_value def has_key(self, key, version=None): # _has_expired will do the locking if not self._has_expired(key, version=version): return True with self._lock: try: del self._cache[key] except KeyError: pass return False def delete(self, key, version=None): key = self.make_key(key, version=version) with self._lock: try: del self._cache[key] except KeyError: pass def clear(self): with self._lock: self._cache.clear() def _has_expired(self, key, version=None): _, exp = self._get(key, version=version) return self._is_expired(exp) def _is_expired(self, expiration): if expiration is None or expiration > time.time(): return False return True PK!?00/django_lrucache_backend-2.0.0.dist-info/LICENSE MIT License Copyright (c) 2017, Josh Smeaton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H9VWX-django_lrucache_backend-2.0.0.dist-info/WHEEL A н#f."jm)!fb҅~ܴA,mTD}E n0H饹*|D[¬c i=0(q3PK!HwȨ P0django_lrucache_backend-2.0.0.dist-info/METADATAXmo8_6g+o0Iۤ p.%ZZ$8glmPp^yfse.SYM=NEVj$?d]aiLf35lx狟X^߼tb[aF8ς`D)޷5:@P3qqk kdǹ#bބ+um):2o֊zJ*]j *Riε'Lq43~;wsylTPTμT`6_?Po_:<161R 1Xysz~r;1=Z9r8^$f*M[ct8].ޓ~=`8PK!Hi16.django_lrucache_backend-2.0.0.dist-info/RECORD;s@jP,Atfg܂>q&cTㄤfӆ2ȝDO&s4o:;#- {gObDQӳPcR6_!ؤ!XAmKZGCY:LDZ \$ $M4/L93hOTXW9iƸk/OvH=n}X+(`@9FIBB 7"Eۆk+T;/X^܍R:|8VRNT~sPK!Glrucache_backend/__init__.pyPK!(ЭbbSlrucache_backend/backend.pyPK!?00/django_lrucache_backend-2.0.0.dist-info/LICENSEPK!H9VWX-kdjango_lrucache_backend-2.0.0.dist-info/WHEELPK!HwȨ P0 django_lrucache_backend-2.0.0.dist-info/METADATAPK!Hi16.django_lrucache_backend-2.0.0.dist-info/RECORDPK