Skip to content

Commit ee0a709

Browse files
committed
Revert lru_cache for request lock to avoid binding to multiple event loops
1 parent e1bcde0 commit ee0a709

1 file changed

Lines changed: 3 additions & 7 deletions

File tree

‎aiohttp_client_cache/session.py‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import sys
66
import warnings
77
from asyncio import Lock
8+
from collections import defaultdict
89
from contextlib import asynccontextmanager
9-
from functools import lru_cache
1010
from logging import getLogger
1111
from typing import TYPE_CHECKING, cast
1212

@@ -49,11 +49,6 @@ async def __aexit__(self, *excinfo):
4949
from typing_extensions import Self
5050

5151

52-
@lru_cache(maxsize=16384)
53-
def _get_lock(_: int, __: str) -> Lock:
54-
return Lock()
55-
56-
5752
class CacheMixin(MIXIN_BASE):
5853
"""A mixin class for :py:class:`aiohttp.ClientSession` that adds caching support"""
5954

@@ -66,6 +61,7 @@ def __init__(
6661
**kwargs,
6762
):
6863
self.cache = cache or CacheBackend()
64+
self._locks: dict[str, Lock] = defaultdict(Lock)
6965
self._null_lock = nullcontext()
7066

7167
# Pass along any valid kwargs for ClientSession (or custom session superclass)
@@ -93,7 +89,7 @@ async def _request(
9389
if actions.skip_read:
9490
lock: Lock | nullcontext = self._null_lock
9591
else:
96-
lock = _get_lock(id(self), key)
92+
lock = self._locks[key]
9793

9894
async with lock:
9995
response = await self.cache.request(actions)

0 commit comments

Comments
 (0)