def update_cache(cached, name, key, target, *args): ''' Actual update of cached valeus in name,key, updates to the result of target(args). If value already cached and up to date this does nothing, otherwise if present, not up to date but last_updated less then four minutes ago does nothing as well, as someone surely precached it and is computing the results, already. ''' if cached['present']: delay = datetime.datetime.now() - cached['last_updated'] if delay < RECOMPUTE_PRECACHED_ELEMENT_DELAY and cached['precached']: return if cached['upToDate'] and delay < CACHE_IS_OUTDATED_DELAY: return precache_element(name, key) el = target(*args) cache_element(name, key, pickle.dumps(el)) return el
def update_cache(cached, 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 if cached['present']: delay = datetime.now() - cached['last_updated'] if delay < RECOMPUTE_PRECACHED_ELEMENT_DELAY and cached['precached']: #print '--!!!Udating cache skip precached!' return [False, None] 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]