Skip to content

Commit

Permalink
Merge pull request #1 from numberly/480-fix-concurrent-cache-access
Browse files Browse the repository at this point in the history
fix aio-libs#480, concurrent cache access with ttl
  • Loading branch information
ahivert authored May 3, 2023
2 parents da1bd95 + da2935e commit feab808
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 1 addition & 2 deletions async_lru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,8 @@ def _task_done_callback(
) -> None:
self.__tasks.remove(task)

if self.__ttl is not None:
if self.__ttl is not None and (cache_item := self.__cache.get(key)):
loop = asyncio.get_running_loop()
cache_item = self.__cache[key]
cache_item.later_call = loop.call_later(
self.__ttl, self.__cache.pop, key, None
)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_ttl.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,12 @@ async def coro(val: int) -> int:
# cache is not cleared after ttl expires because invalidate also should clear
# the invalidation by timeout
check_lru(coro, hits=0, misses=2, cache=1, tasks=0, maxsize=None)


async def test_ttl_concurrent() -> None:
@alru_cache(maxsize=1, ttl=1)
async def coro(val: int) -> int:
return val

results = await asyncio.gather(*[coro(i) for i in range(2)])
assert results == list(range(2))

0 comments on commit feab808

Please sign in to comment.