Memoize can now store the cache in memory
This commit is contained in:
parent
92f7eff846
commit
fd6ed0d813
@ -30,7 +30,7 @@ from xdg.BaseDirectory import save_cache_path
|
|||||||
CACHEDIR = save_cache_path('opensuse-repo-checker')
|
CACHEDIR = save_cache_path('opensuse-repo-checker')
|
||||||
|
|
||||||
|
|
||||||
def memoize(ttl=None):
|
def memoize(ttl=None, session=False):
|
||||||
"""Decorator function to implement a persistent cache.
|
"""Decorator function to implement a persistent cache.
|
||||||
|
|
||||||
>>> @memoize()
|
>>> @memoize()
|
||||||
@ -91,6 +91,9 @@ def memoize(ttl=None):
|
|||||||
NCLEAN = 1024 # Number of slots to remove when limit reached
|
NCLEAN = 1024 # Number of slots to remove when limit reached
|
||||||
TIMEOUT = 60*60*2 # Time to live for every cache slot (seconds)
|
TIMEOUT = 60*60*2 # Time to live for every cache slot (seconds)
|
||||||
|
|
||||||
|
# The session cache is only used when 'session' is True
|
||||||
|
session_cache = {}
|
||||||
|
|
||||||
def _memoize(fn):
|
def _memoize(fn):
|
||||||
# Implement a POSIX lock / unlock extension for shelves. Inspired
|
# Implement a POSIX lock / unlock extension for shelves. Inspired
|
||||||
# on ActiveState Code recipe #576591
|
# on ActiveState Code recipe #576591
|
||||||
@ -104,15 +107,19 @@ def memoize(ttl=None):
|
|||||||
lckfile.close()
|
lckfile.close()
|
||||||
|
|
||||||
def _open_cache(cache_name):
|
def _open_cache(cache_name):
|
||||||
lckfile = _lock(cache_name)
|
cache = session_cache
|
||||||
cache = shelve.open(cache_name, protocol=-1)
|
if not session:
|
||||||
# Store a reference to the lckfile to avoid to be closed by gc
|
lckfile = _lock(cache_name)
|
||||||
cache.lckfile = lckfile
|
cache = shelve.open(cache_name, protocol=-1)
|
||||||
|
# Store a reference to the lckfile to avoid to be
|
||||||
|
# closed by gc
|
||||||
|
cache.lckfile = lckfile
|
||||||
return cache
|
return cache
|
||||||
|
|
||||||
def _close_cache(cache):
|
def _close_cache(cache):
|
||||||
cache.close()
|
if not session:
|
||||||
_unlock(cache.lckfile)
|
cache.close()
|
||||||
|
_unlock(cache.lckfile)
|
||||||
|
|
||||||
def _clean_cache(cache):
|
def _clean_cache(cache):
|
||||||
len_cache = len(cache)
|
len_cache = len(cache)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user