def cache_results(function):
def save_to_permacache():<EOL><INDENT>"""<STR_LIT>"""<EOL>update_from_permacache()<EOL>try:<EOL><INDENT>with open(filename, '<STR_LIT:wb>') as fp:<EOL><INDENT>pickle.dump(cache, fp, pickle.HIGHEST_PROTOCOL)<EOL><DEDENT><DEDENT>except IOError:<EOL><INDENT>pass  <EOL><DEDENT><DEDENT>def update_from_permacache():<EOL><INDENT>"""<STR_LIT>"""<EOL>try:<EOL><INDENT>with open(filename, '<STR_LIT:rb>') as fp:<EOL><INDENT>permacache = pickle.load(fp)<EOL><DEDENT><DEDENT>except Exception:  <EOL><INDENT>return  <EOL><DEDENT>for key, value in permacache.items():<EOL><INDENT>if key not in cache or value[<NUM_LIT:0>] > cache[key][<NUM_LIT:0>]:<EOL><INDENT>cache[key] = value<EOL><DEDENT><DEDENT><DEDENT>cache = {}<EOL>cache_expire_time = <NUM_LIT><EOL>try:<EOL><INDENT>filename = os.path.join(gettempdir(), '<STR_LIT>')<EOL>update_from_permacache()<EOL><DEDENT>except NotImplementedError:<EOL><INDENT>filename = None<EOL><DEDENT>@wraps(function)<EOL>def wrapped(obj, package_name, package_version, **extra_data):<EOL><INDENT>"""<STR_LIT>"""<EOL>now = time.time()<EOL>key = (package_name, package_version)<EOL>if not obj.bypass_cache and key in cache:  <EOL><INDENT>cache_time, retval = cache[key]<EOL>if now - cache_time < cache_expire_time:<EOL><INDENT>return retval<EOL><DEDENT><DEDENT>retval = function(obj, package_name, package_version, **extra_data)<EOL>cache[key] = now, retval<EOL>if filename:<EOL><INDENT>save_to_permacache()<EOL><DEDENT>return retval<EOL><DEDENT>return wrapped<EOL>
Return decorated function that caches the results.