Created
May 28, 2015 00:52
-
-
Save anonymous/1339158b49b2089b3886 to your computer and use it in GitHub Desktop.
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
def wrapper(*args, **kwds): | |
# size limited caching that tracks accesses by recency | |
key = make_key(args, kwds, typed) if kwds or typed else args | |
with lock: | |
cache_result = cache_get(key, (None, None)) | |
last_modified, link = cache_result | |
if link is not None and cache_is_valid(last_modified, ttl): | |
# record recent use of the key by moving it to the front of the list | |
root, = nonlocal_root | |
link_prev, link_next, key, result = link | |
link_prev[NEXT] = link_next | |
link_next[PREV] = link_prev | |
last = root[PREV] | |
last[NEXT] = root[PREV] = link | |
link[PREV] = last | |
link[NEXT] = root | |
stats[HITS] += 1 | |
return result | |
result = user_function(*args, **kwds) | |
with lock: | |
root, = nonlocal_root | |
if key in cache: | |
# getting here means that this same key was added to the | |
# cache while the lock was released. since the link | |
# update is already done, we need only return the | |
# computed result and update the count of misses. | |
pass | |
elif _len(cache) >= maxsize: | |
# use the old root to store the new key and result | |
oldroot = root | |
oldroot[KEY] = key | |
oldroot[RESULT] = result | |
# empty the oldest link and make it the new root | |
root = nonlocal_root[0] = oldroot[NEXT] | |
oldkey = root[KEY] | |
oldvalue = root[RESULT] | |
root[KEY] = root[RESULT] = None | |
# now update the cache dictionary for the new links | |
del cache[oldkey] | |
cache[key] = (datetime.now(), oldroot) | |
else: | |
# put result in a new link at the front of the list | |
last = root[PREV] | |
link = [last, root, key, result] | |
cache[key] = (datetime.now(), link) | |
last[NEXT] = root[PREV] = link | |
stats[MISSES] += 1 | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment