aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Numerlor <[email protected]>2020-07-18 16:16:49 +0200
committerGravatar Numerlor <[email protected]>2020-07-18 16:16:49 +0200
commitc3bda11a10e3706d7e457f727e57e6a92f604d1e (patch)
treec7ced9b4523cf962fc5613464cbef85692b9cff0
parentCreate a package for the Doc cog. (diff)
Move async_cache into a separate module
-rw-r--r--bot/cogs/doc/cache.py32
-rw-r--r--bot/cogs/doc/cog.py33
2 files changed, 34 insertions, 31 deletions
diff --git a/bot/cogs/doc/cache.py b/bot/cogs/doc/cache.py
new file mode 100644
index 000000000..9da2a1dab
--- /dev/null
+++ b/bot/cogs/doc/cache.py
@@ -0,0 +1,32 @@
+import functools
+from collections import OrderedDict
+from typing import Any, Callable
+
+
+def async_cache(max_size: int = 128, arg_offset: int = 0) -> Callable:
+ """
+ LRU cache implementation for coroutines.
+
+ Once the cache exceeds the maximum size, keys are deleted in FIFO order.
+
+ An offset may be optionally provided to be applied to the coroutine's arguments when creating the cache key.
+ """
+ # Assign the cache to the function itself so we can clear it from outside.
+ async_cache.cache = OrderedDict()
+
+ def decorator(function: Callable) -> Callable:
+ """Define the async_cache decorator."""
+ @functools.wraps(function)
+ async def wrapper(*args) -> Any:
+ """Decorator wrapper for the caching logic."""
+ key = ':'.join(args[arg_offset:])
+
+ value = async_cache.cache.get(key)
+ if value is None:
+ if len(async_cache.cache) > max_size:
+ async_cache.cache.popitem(last=False)
+
+ async_cache.cache[key] = await function(*args)
+ return async_cache.cache[key]
+ return wrapper
+ return decorator
diff --git a/bot/cogs/doc/cog.py b/bot/cogs/doc/cog.py
index 463e4ebc6..2627951e8 100644
--- a/bot/cogs/doc/cog.py
+++ b/bot/cogs/doc/cog.py
@@ -7,7 +7,7 @@ import textwrap
from collections import OrderedDict
from contextlib import suppress
from types import SimpleNamespace
-from typing import Any, Callable, Dict, List, NamedTuple, Optional, Tuple, Union
+from typing import Callable, Dict, List, NamedTuple, Optional, Tuple, Union
from urllib.parse import urljoin
import discord
@@ -25,7 +25,7 @@ from bot.converters import PackageName, ValidURL
from bot.decorators import with_role
from bot.pagination import LinePaginator
from bot.utils.messages import wait_for_deletion
-
+from .cache import async_cache
log = logging.getLogger(__name__)
logging.getLogger('urllib3').setLevel(logging.WARNING)
@@ -76,35 +76,6 @@ class DocItem(NamedTuple):
group: str
-def async_cache(max_size: int = 128, arg_offset: int = 0) -> Callable:
- """
- LRU cache implementation for coroutines.
-
- Once the cache exceeds the maximum size, keys are deleted in FIFO order.
-
- An offset may be optionally provided to be applied to the coroutine's arguments when creating the cache key.
- """
- # Assign the cache to the function itself so we can clear it from outside.
- async_cache.cache = OrderedDict()
-
- def decorator(function: Callable) -> Callable:
- """Define the async_cache decorator."""
- @functools.wraps(function)
- async def wrapper(*args) -> Any:
- """Decorator wrapper for the caching logic."""
- key = ':'.join(args[arg_offset:])
-
- value = async_cache.cache.get(key)
- if value is None:
- if len(async_cache.cache) > max_size:
- async_cache.cache.popitem(last=False)
-
- async_cache.cache[key] = await function(*args)
- return async_cache.cache[key]
- return wrapper
- return decorator
-
-
class DocMarkdownConverter(MarkdownConverter):
"""Subclass markdownify's MarkdownCoverter to provide custom conversion methods."""