mirror of
https://github.com/wassname/cachier.git
synced 2026-09-09 11:19:08 +08:00
Added RecalculationNeeded Exception
This commit is contained in:
+9
-5
@@ -30,7 +30,7 @@ except ImportError: # we're in python 2.x
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from .pickle_core import _PickleCore
|
||||
from .mongo_core import _MongoCore
|
||||
from .mongo_core import _MongoCore, RecalculationNeeded
|
||||
|
||||
|
||||
|
||||
@@ -82,8 +82,6 @@ def _calc_entry(core, key, func, args, kwds):
|
||||
finally:
|
||||
core.mark_entry_not_calculated(key)
|
||||
|
||||
|
||||
|
||||
def cachier(stale_after=None, next_time=False, pickle_reload=True,
|
||||
mongetter=None):
|
||||
"""A persistent, stale-free memoization decorator.
|
||||
@@ -156,7 +154,10 @@ def cachier(stale_after=None, next_time=False, pickle_reload=True,
|
||||
_print('Returning stale.')
|
||||
return entry['value'] # return stale val
|
||||
_print('Already calc. Waiting on change.')
|
||||
return core.wait_on_entry_calc(key)
|
||||
try:
|
||||
return core.wait_on_entry_calc(key)
|
||||
except RecalculationNeeded:
|
||||
return _calc_entry(core, key, func, args, kwds)
|
||||
if next_time:
|
||||
_print('Async calc and return stale')
|
||||
try:
|
||||
@@ -173,7 +174,10 @@ def cachier(stale_after=None, next_time=False, pickle_reload=True,
|
||||
return entry['value']
|
||||
if entry['being_calculated']:
|
||||
_print('No value but being calculated. Waiting.')
|
||||
return core.wait_on_entry_calc(key)
|
||||
try:
|
||||
return core.wait_on_entry_calc(key)
|
||||
except RecalculationNeeded:
|
||||
return _calc_entry(core, key, func, args, kwds)
|
||||
_print('No entry found. No current calc. Calling like a boss.')
|
||||
return _calc_entry(core, key, func, args, kwds)
|
||||
|
||||
|
||||
@@ -28,6 +28,8 @@ from .base_core import _BaseCore
|
||||
|
||||
MONGO_SLEEP_DURATION_IN_SEC = 1
|
||||
|
||||
class RecalculationNeeded(Exception):
|
||||
pass
|
||||
|
||||
class _MongoCore(_BaseCore):
|
||||
|
||||
@@ -126,6 +128,8 @@ class _MongoCore(_BaseCore):
|
||||
while True:
|
||||
time.sleep(MONGO_SLEEP_DURATION_IN_SEC)
|
||||
key, entry = self.get_entry_by_key(key)
|
||||
if entry is None:
|
||||
raise RecalculationNeeded()
|
||||
if entry is not None and not entry['being_calculated']:
|
||||
return entry['value']
|
||||
|
||||
|
||||
Reference in New Issue
Block a user