diff options
| author | 2020-10-04 09:25:52 +0300 | |
|---|---|---|
| committer | 2020-10-04 09:25:52 +0300 | |
| commit | 90103c58697889fdd352cd021faba6be2ad3a7d7 (patch) | |
| tree | 7de505fb22ac301048f125e6103e1b8313238603 | |
| parent | Make AsyncCache key tuple instead string (diff) | |
Move AsyncCache max_size argument to __init__ from decorator
| -rw-r--r-- | bot/utils/cache.py | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/bot/utils/cache.py b/bot/utils/cache.py index 8a180b4fa..68ce15607 100644 --- a/bot/utils/cache.py +++ b/bot/utils/cache.py @@ -12,10 +12,11 @@ class AsyncCache: An offset may be optionally provided to be applied to the coroutine's arguments when creating the cache key. """ - def __init__(self): + def __init__(self, max_size: int = 128): self._cache = OrderedDict() + self._max_size = max_size - def __call__(self, max_size: int = 128, arg_offset: int = 0) -> Callable: + def __call__(self, arg_offset: int = 0) -> Callable: """Decorator for async cache.""" def decorator(function: Callable) -> Callable: @@ -27,7 +28,7 @@ class AsyncCache: key = args[arg_offset:] if key not in self._cache: - if len(self._cache) > max_size: + if len(self._cache) > self._max_size: self._cache.popitem(last=False) self._cache[key] = await function(*args) |