Skip to content

Commit 5b8bb25

Browse files
authored
minor refactor if depths (#182)
* depth 1 * depth 2
1 parent 15012cf commit 5b8bb25

1 file changed

Lines changed: 35 additions & 38 deletions

File tree

cachier/core.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -283,51 +283,48 @@ def func_wrapper(*args, **kwds):
283283
key, entry = core.get_entry(tuple(), kwargs)
284284
if overwrite_cache:
285285
return _calc_entry(core, key, func, args, kwds)
286-
if entry is not None:
287-
_print("Entry found.")
288-
if _allow_none or entry.get("value", None) is not None:
289-
_print("Cached result found.")
290-
local_stale_after = (
291-
stale_after or _default_params["stale_after"]
292-
) # noqa: E501
293-
local_next_time = next_time or _default_params["next_time"] # noqa: E501
294-
now = datetime.datetime.now()
295-
if now - entry["time"] > local_stale_after:
296-
_print("But it is stale... :(")
297-
if entry["being_calculated"]:
298-
if local_next_time:
299-
_print("Returning stale.")
300-
return entry["value"] # return stale val
301-
_print("Already calc. Waiting on change.")
302-
try:
303-
return core.wait_on_entry_calc(key)
304-
except RecalculationNeeded:
305-
return _calc_entry(core, key, func, args, kwds)
306-
if local_next_time:
307-
_print("Async calc and return stale")
308-
try:
309-
core.mark_entry_being_calculated(key)
310-
_get_executor().submit(
311-
_function_thread,
312-
core,
313-
key,
314-
func,
315-
args,
316-
kwds,
317-
)
318-
finally:
319-
core.mark_entry_not_calculated(key)
320-
return entry["value"]
321-
_print("Calling decorated function and waiting")
322-
return _calc_entry(core, key, func, args, kwds)
286+
if entry is None:
287+
_print("No entry found. No current calc. Calling like a boss.")
288+
return _calc_entry(core, key, func, args, kwds)
289+
_print("Entry found.")
290+
if _allow_none or entry.get("value", None) is not None:
291+
_print("Cached result found.")
292+
local_stale_after = (
293+
stale_after or _default_params["stale_after"]
294+
) # noqa: E501
295+
local_next_time = next_time or _default_params["next_time"] # noqa: E501
296+
now = datetime.datetime.now()
297+
if now - entry["time"] <= local_stale_after:
323298
_print("And it is fresh!")
324299
return entry["value"]
300+
_print("But it is stale... :(")
325301
if entry["being_calculated"]:
326-
_print("No value but being calculated. Waiting.")
302+
if local_next_time:
303+
_print("Returning stale.")
304+
return entry["value"] # return stale val
305+
_print("Already calc. Waiting on change.")
327306
try:
328307
return core.wait_on_entry_calc(key)
329308
except RecalculationNeeded:
330309
return _calc_entry(core, key, func, args, kwds)
310+
if local_next_time:
311+
_print("Async calc and return stale")
312+
try:
313+
core.mark_entry_being_calculated(key)
314+
_get_executor().submit(
315+
_function_thread, core, key, func, args, kwds
316+
)
317+
finally:
318+
core.mark_entry_not_calculated(key)
319+
return entry["value"]
320+
_print("Calling decorated function and waiting")
321+
return _calc_entry(core, key, func, args, kwds)
322+
if entry["being_calculated"]:
323+
_print("No value but being calculated. Waiting.")
324+
try:
325+
return core.wait_on_entry_calc(key)
326+
except RecalculationNeeded:
327+
return _calc_entry(core, key, func, args, kwds)
331328
_print("No entry found. No current calc. Calling like a boss.")
332329
return _calc_entry(core, key, func, args, kwds)
333330

0 commit comments

Comments
 (0)