def lru_cache(maxsize=<NUM_LIT:100>):
<EOL>def decorating_function(user_function):<EOL><INDENT>cache = dict()<EOL>make_key = _make_key<EOL>cache_get = cache.get           <EOL>_len = len                      <EOL>lock = RLock()                  <EOL>root = []                       <EOL>root[:] = [root, root, None, None]      <EOL>nonlocal_root = [root]                  <EOL>PREV, NEXT, KEY, RESULT = <NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>    <EOL>if maxsize == <NUM_LIT:0>:<EOL><INDENT>def wrapper(*args, **kwds):<EOL><INDENT>result = user_function(*args, **kwds)<EOL>return result<EOL><DEDENT><DEDENT>elif maxsize is None:<EOL><INDENT>def wrapper(*args, **kwds):<EOL><INDENT>key = make_key(args, kwds)<EOL>result = cache_get(key, root)   <EOL>if result is not root:<EOL><INDENT>return result<EOL><DEDENT>result = user_function(*args, **kwds)<EOL>cache[key] = result<EOL>return result<EOL><DEDENT><DEDENT>else:<EOL><INDENT>def wrapper(*args, **kwds):<EOL><INDENT>key = make_key(args, kwds)<EOL>with lock:<EOL><INDENT>link = cache_get(key)<EOL>if link is not None:<EOL><INDENT>root, = nonlocal_root<EOL>link_prev, link_next, key, result = link<EOL>link_prev[NEXT] = link_next<EOL>link_next[PREV] = link_prev<EOL>last = root[PREV]<EOL>last[NEXT] = root[PREV] = link<EOL>link[PREV] = last<EOL>link[NEXT] = root<EOL>return result<EOL><DEDENT><DEDENT>result = user_function(*args, **kwds)<EOL>with lock:<EOL><INDENT>root, = nonlocal_root<EOL>if key in cache:<EOL><INDENT>pass<EOL><DEDENT>elif _len(cache) >= maxsize:<EOL><INDENT>oldroot = root<EOL>oldroot[KEY] = key<EOL>oldroot[RESULT] = result<EOL>root = nonlocal_root[<NUM_LIT:0>] = oldroot[NEXT]<EOL>oldkey = root[KEY]<EOL>oldvalue = root[RESULT]<EOL>root[KEY] = root[RESULT] = None<EOL>del cache[oldkey]<EOL>cache[key] = oldroot<EOL><DEDENT>else:<EOL><INDENT>last = root[PREV]<EOL>link = [last, root, key, result]<EOL>last[NEXT] = root[PREV] = cache[key] = link<EOL><DEDENT><DEDENT>return result<EOL><DEDENT><DEDENT>def cache_clear():<EOL><INDENT>"""<STR_LIT>"""<EOL>with lock:<EOL><INDENT>cache.clear()<EOL>root = nonlocal_root[<NUM_LIT:0>]<EOL>root[:] = [root, root, None, None]<EOL><DEDENT><DEDENT>wrapper.__wrapped__ = user_function<EOL>wrapper.cache_clear = cache_clear<EOL>return update_wrapper(wrapper, user_function)<EOL><DEDENT>return decorating_function<EOL>
Least-recently-used cache decorator.

    If *maxsize* is set to None, the LRU features are disabled and the cache
    can grow without bound.

    Arguments to the cached function must be hashable.

    View the cache statistics named tuple (hits, misses, maxsize, currsize) with
    f.cache_info().  Clear the cache and statistics with f.cache_clear().
    Access the underlying function with f.__wrapped__.

    See:  http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used