From 391cef6c59d1de52461fcf71d6ea9dae1ba756be Mon Sep 17 00:00:00 2001 From: Shay Palachy Date: Mon, 29 Aug 2016 00:47:31 +0300 Subject: [PATCH] logic fix --- cachier/core.py | 20 ++++++++++---------- tests/test_cachier.py | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/cachier/core.py b/cachier/core.py index 3934b6a..be8148a 100644 --- a/cachier/core.py +++ b/cachier/core.py @@ -342,16 +342,16 @@ def _function_thread(core, key, func, args, kwds): ) -def cachier(stale_after=None, next_time=True, pickle_reload=True, +def cachier(stale_after=None, next_time=False, pickle_reload=True, mongetter=None): """A persistent, stale-free memoization decorator. - When using a MongoDB-backed caching, the positional and keyword arguments - to the wrapped function must be hashable (i.e. Python's immutable built-in - objects, not mutable containers). Also, notice that since objects which - are instances of user-defined classes are hashable but all compare unequal - (their hash value is their id), equal objects across different sessions - will not yield identical keys. + The positional and keyword arguments to the wrapped function must be + hashable (i.e. Python's immutable built-in objects, not mutable + containers). Also, notice that since objects which are instances of + user-defined classes are hashable but all compare unequal (their hash + value is their id), equal objects across different sessions will not yield + identical keys. Arguments --------- @@ -363,7 +363,7 @@ def cachier(stale_after=None, next_time=True, pickle_reload=True, next_time (optional) : bool If set to True, a stale result will be returned when finding one, not waiting for the calculation of the fresh result to return. Defaults to - True. + False. pickle_reload (optional) : bool If set to True, in-memory cache will be reloaded on each cache read, enabling different threads to share cache. Should be set to False for @@ -400,11 +400,11 @@ def cachier(stale_after=None, next_time=True, pickle_reload=True, if now - entry['time'] > stale_after: # print('But it is stale... :(') if entry['being_calculated']: + if next_time: + return entry['value'] # return stale val # print('Already calc. Waiting on change.') return core.wait_on_entry_calc(key) if next_time: - if entry['being_calculated']: - return entry['value'] # return stale val # trigger async calculation and return stale core.mark_entry_being_calculated(key) _get_executor().submit( diff --git a/tests/test_cachier.py b/tests/test_cachier.py index 107af0e..f99309d 100644 --- a/tests/test_cachier.py +++ b/tests/test_cachier.py @@ -19,7 +19,7 @@ def _mongo_getter(): # Pickle core tests -@cachier() +@cachier(next_time=True) def test_int_pickling(int_1, int_2): """Add the two given ints.""" return int_1 + int_2 @@ -69,7 +69,7 @@ def stale_after_seconds(arg_1, arg_2): # Mongo core tests -@cachier(mongetter=_mongo_getter) +@cachier(mongetter=_mongo_getter, next_time=True) def test_mongo_caching(arg_1, arg_2): """Some function.""" return 'arg_1:{}, arg_2:{}'.format(arg_1, arg_2)