forked from c3nav/c3nav
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing LocalCacheProxy to skip pickling stuff
- Loading branch information
1 parent
957ce0c
commit 3eeda53
Showing
3 changed files
with
69 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from collections import OrderedDict | ||
|
||
from django.core.cache import cache | ||
|
||
|
||
class NoneFromCache: | ||
pass | ||
|
||
|
||
class LocalCacheProxy: | ||
# django cache, buffered using a LRU cache | ||
# only usable for stuff that never changes, obviously | ||
def __init__(self, maxsize=128): | ||
self._maxsize = maxsize | ||
self._items = OrderedDict() | ||
|
||
def get(self, key, default=None): | ||
print('get') | ||
try: | ||
# first check out cache | ||
result = self._items[key] | ||
except KeyError: | ||
# not in our cache | ||
result = cache.get(key, default=NoneFromCache) | ||
if result is not NoneFromCache: | ||
self._items[key] = result | ||
self._prune() | ||
else: | ||
result = default | ||
else: | ||
self._items.move_to_end(key, last=True) | ||
return result | ||
|
||
def _prune(self): | ||
# remove old items | ||
while len(self._items) > self._maxsize: | ||
self._items.pop(next(iter(self._items.keys()))) | ||
|
||
def set(self, key, value, expire): | ||
cache.set(key, value, expire) | ||
self._items[key] = value | ||
self._prune() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters