Skip to content

Commit 565a9ea

Browse files
committed
Add condition wrapper without cache_info.
1 parent f8a6d48 commit 565a9ea

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/cachetools/_decorators.py

+34
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,38 @@ def cache_clear():
127127
return wrapper
128128

129129

130+
def _cached_cond(func, cache, key, cond):
131+
pending = set()
132+
133+
def wrapper(*args, **kwargs):
134+
k = key(*args, **kwargs)
135+
with cond:
136+
cond.wait_for(lambda: k not in pending)
137+
try:
138+
return cache[k]
139+
except KeyError:
140+
pending.add(k)
141+
try:
142+
v = func(*args, **kwargs)
143+
with cond:
144+
try:
145+
cache[k] = v
146+
except ValueError:
147+
pass # value too large
148+
return v
149+
finally:
150+
with cond:
151+
pending.remove(k)
152+
cond.notify_all()
153+
154+
def cache_clear():
155+
with cond:
156+
cache.clear()
157+
158+
wrapper.cache_clear = cache_clear
159+
return wrapper
160+
161+
130162
def _cached_locked(func, cache, key, lock):
131163
def wrapper(*args, **kwargs):
132164
k = key(*args, **kwargs)
@@ -192,6 +224,8 @@ def _cached_wrapper(func, cache, key, lock, info):
192224
wrapper = _uncached(func)
193225
elif lock is None:
194226
wrapper = _cached_unlocked(func, cache, key)
227+
elif hasattr(lock, "wait_for") and hasattr(lock, "notify_all"):
228+
wrapper = _cached_cond(func, cache, key, lock)
195229
else:
196230
wrapper = _cached_locked(func, cache, key, lock)
197231
wrapper.cache_info = None

0 commit comments

Comments
 (0)