aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/cogs/doc.py32
-rw-r--r--bot/utils/cache.py32
2 files changed, 34 insertions, 30 deletions
diff --git a/bot/cogs/doc.py b/bot/cogs/doc.py
index 204cffb37..ff60fc80a 100644
--- a/bot/cogs/doc.py
+++ b/bot/cogs/doc.py
@@ -6,7 +6,7 @@ import textwrap
from collections import OrderedDict
from contextlib import suppress
from types import SimpleNamespace
-from typing import Any, Callable, Optional, Tuple
+from typing import Optional, Tuple
import discord
from bs4 import BeautifulSoup
@@ -23,6 +23,7 @@ from bot.constants import MODERATION_ROLES, RedirectOutput
from bot.converters import ValidPythonIdentifier, ValidURL
from bot.decorators import with_role
from bot.pagination import LinePaginator
+from bot.utils.cache import async_cache
log = logging.getLogger(__name__)
@@ -66,35 +67,6 @@ FAILED_REQUEST_RETRY_AMOUNT = 3
NOT_FOUND_DELETE_DELAY = RedirectOutput.delete_delay
-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."""
diff --git a/bot/utils/cache.py b/bot/utils/cache.py
new file mode 100644
index 000000000..338924df8
--- /dev/null
+++ b/bot/utils/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(str(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