Skip to content

Commit 1ab93f3

Browse files
Merge bf498df into master
2 parents b149587 + bf498df commit 1ab93f3

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

filememo/_constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.2.0'
1+
__version__ = '0.2.1'

filememo/_deco.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def _outdated_exc(created: dt.datetime, max_age: Optional[dt.timedelta]):
4242
return (_utc() - created) > max_age
4343

4444

45+
def _outdated_result(created: dt.datetime, max_age: dt.timedelta):
46+
assert max_age is not None
47+
if max_age == dt.timedelta.max:
48+
return False
49+
return (_utc() - created) > max_age
50+
51+
4552
def memoize(function: Callable = None,
4653
dir_path: Union[Path, str] = None,
4754
max_age: dt.timedelta = dt.timedelta.max,
@@ -55,44 +62,44 @@ def memoize(function: Callable = None,
5562
version=version,
5663
exceptions_max_age=exceptions_max_age)
5764

58-
# method_str = _file_and_method(method)
59-
6065
if max_age is None:
6166
raise ValueError('max_age must not be None')
6267

6368
@functools.wraps(function)
6469
def f(*args, **kwargs):
6570
key = (args, kwargs)
6671

67-
# try get existing result from the cache
68-
# try:
69-
# we will use max_age on both reading and writing
70-
71-
# print(f.data.dirpath)
72+
# TRYING TO RETURN FROM CACHE
7273

73-
record = f.data._get_record(key, max_age=_max_to_none(max_age))
74+
# we will use max_age on both reading and writing
75+
record = f.data._get_record(key)
7476
if record is not None:
7577

7678
exception, result = record.data
77-
if not exception:
78-
return result
7979

80-
assert exception is not None
81-
if not _outdated_exc(record.created, exceptions_max_age):
82-
raise FunctionException(exception)
80+
if exception:
81+
if not _outdated_exc(record.created, exceptions_max_age):
82+
raise FunctionException(exception)
83+
else:
84+
assert exception is None
85+
# we don't need to delete anything on reading
86+
if not _outdated_result(record.created, max_age):
87+
return result
8388

8489
# we did not return result and did not raise exception.
8590
# We will just restart the function
8691

92+
# COMPUTING NEW RESULT AND SAVING TO CACHE
93+
8794
# get new result and store it to the cache
8895
exception: Optional[BaseException] = None
8996
result = None
9097
try:
9198
result = function(*args, **kwargs)
92-
except BaseException as error:
93-
if not exceptions_max_age:
99+
except BaseException as exc:
100+
if exceptions_max_age is None:
94101
raise FunctionException(exception)
95-
exception = error
102+
exception = exc
96103

97104
assert exception is None or exceptions_max_age is not None
98105

@@ -105,8 +112,8 @@ def f(*args, **kwargs):
105112

106113
if exception:
107114
raise FunctionException(exception)
108-
109-
return result
115+
else:
116+
return result
110117

111118
# dir_path is the parent path. Within this directory, we will create
112119
# a subdirectory that uniquely matches the function we are decorating.

0 commit comments

Comments
 (0)