def retrieve_update_cache(name, key, target, *args): ''' Retrieves the result of target(args)(= value) from (name, key) cached element. If element present and UpToDate it returns [value, True]. If element present and Precached it returns [None, False] because it is currently computed. If element is not present it computes its value, updates the cache and returns [value, True]. ''' #print '--Getting ', name, ' ', key cached = get_cached_element(name, str(key)) if cached['present']: if cached['upToDate'] and not FORCE_CACHE_IS_EXPIRED: delay = datetime.now() - cached['last_updated'] if delay < CACHE_IS_OUTDATED_DELAY: return [deserialize(cached['value']), True] val = update_cache(cached, name, str(key), target, *args) if val[0]: return [val[1], True] else: return [None, False]
def update_cache(name, key, target, *args): ''' Actual update of cached value of (name, key). Updates to the result of target(args). If value present in cache, not up to date but last_updated less than a threshold it does nothing, as someone surely precached it and is computing the results already. If not present in cache it precaches it, computes its value and stores it in cache returning its value. ''' #print '--Updating cache: ', name,' ',key cached = get_cached_element(name, key) if cached['present']: delay = datetime.now() - cached['last_updated'] if delay < RECOMPUTE_PRECACHED_ELEMENT_DELAY and cached['precached']: #print '--!!!Udating cache skip precached!' raise PreCacheError precache_element(name, key) el = target(*args) cache_element(name, key, serialize(el)) #print '--Updating cache: ', name,' ',key, ' returning! ', str(el)[0:10] return [True, el]
def retrieve_update_cache(name, key, target, *args): ''' Retrieves the result of target(args) from name,key cache element. If element not present, returns [None, False] while spawning a process which will compute the result and cache it. If value Present but expired returs [value, False], and [value, True] otherwise ''' # if not name and not key and not target: # #magic key to fake a false result # return [None, False] cached = get_cached_element(name, str(key)) if cached['present']: if cached['upToDate']: delay = datetime.datetime.now() - cached['last_updated'] if delay < CACHE_IS_OUTDATED_DELAY: return [pickle.loads(cached['value']), True] val = update_cache(cached, name, str(key), target, *args) if val: return [val, True] else: return [None, False]