Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

decorator: Remove cachify in favor of lru_cache.

Zixuan James Li9d448e73d2c3
diff --git a/zerver/decorator.py b/zerver/decorator.py
index eb13f08153f..934cb45bcbb 100644
--- a/zerver/decorator.py
+++ b/zerver/decorator.py
@@ -4,7 +4,7 @@ import logging
import urllib
from functools import wraps
from io import BytesIO
-from typing import Callable, Dict, Optional, Sequence, Set, Tuple, TypeVar, Union, cast, overload
+from typing import Callable, Dict, Optional, Sequence, Set, TypeVar, Union, cast, overload
import django_otp
import orjson
@@ -70,20 +70,6 @@ webhook_anomalous_payloads_logger = logging.getLogger("zulip.zerver.webhooks.ano
FuncT = TypeVar("FuncT", bound=Callable[..., object])
-def cachify(method: FuncT) -> FuncT:
- dct: Dict[Tuple[object, ...], object] = {}
-
- def cache_wrapper(*args: object) -> object:
- tup = tuple(args)
- if tup in dct:
- return dct[tup]
- result = method(*args)
- dct[tup] = result
- return result
-
- return cast(FuncT, cache_wrapper) # https://github.com/python/mypy/issues/1927
-
-
def update_user_activity(
request: HttpRequest, user_profile: UserProfile, query: Optional[str]
) -> None:
diff --git a/zerver/lib/export.py b/zerver/lib/export.py
index d0ca0916590..a49bbce9483 100644
--- a/zerver/lib/export.py
+++ b/zerver/lib/export.py
@@ -13,6 +13,7 @@ import os
import shutil
import subprocess
import tempfile
+from functools import lru_cache
from typing import Any, Callable, Dict, List, Optional, Set, Tuple
import orjson
@@ -28,7 +29,6 @@ from typing_extensions import TypedDict
import zerver.lib.upload
from analytics.models import RealmCount, StreamCount, UserCount
from scripts.lib.zulip_tools import overwrite_symlink
-from zerver.decorator import cachify
from zerver.lib.avatar_hash import user_avatar_path_from_ids
from zerver.lib.pysa import mark_sanitized
from zerver.lib.upload import get_bucket
@@ -2107,7 +2107,7 @@ def chunkify(lst: List[int], chunk_size: int) -> List[List[int]]:
def export_messages_single_user(
user_profile: UserProfile, *, output_dir: Path, reaction_message_ids: Set[int]
) -> None:
- @cachify
+ @lru_cache(maxsize=None)
def get_recipient(recipient_id: int) -> str:
recipient = Recipient.objects.get(id=recipient_id)
diff --git a/zerver/tests/test_decorators.py b/zerver/tests/test_decorators.py
index 02c3b13ca41..e4abea91b98 100644
--- a/zerver/tests/test_decorators.py
+++ b/zerver/tests/test_decorators.py
@@ -3,6 +3,7 @@ import os
import re
import uuid
from collections import defaultdict
+from functools import lru_cache
from typing import Any, Callable, Dict, List, Optional, Sequence, Tuple
from unittest import mock, skipUnless
@@ -18,7 +19,6 @@ from zerver.decorator import (
authenticated_json_view,
authenticated_rest_api_view,
authenticated_uploads_api_view,
- cachify,
internal_notify_view,
is_local_addr,
rate_limit,
@@ -1948,7 +1948,7 @@ class RestAPITest(ZulipTestCase):
class CacheTestCase(ZulipTestCase):
def test_cachify_basics(self) -> None:
- @cachify
+ @lru_cache(maxsize=None)
def add(w: Any, x: Any, y: Any, z: Any) -> Any:
return w + x + y + z
@@ -1962,7 +1962,7 @@ class CacheTestCase(ZulipTestCase):
result_log: List[str] = []
work_log: List[str] = []
- @cachify
+ @lru_cache(maxsize=None)
def greet(first_name: str, last_name: str) -> str:
msg = f"{greeting} {first_name} {last_name}"
work_log.append(msg)
diff --git a/zerver/tornado/event_queue.py b/zerver/tornado/event_queue.py
index e191ff88048..d725a7d39eb 100644
--- a/zerver/tornado/event_queue.py
+++ b/zerver/tornado/event_queue.py
@@ -11,6 +11,7 @@ import time
import traceback
from collections import deque
from dataclasses import asdict
+from functools import lru_cache
from typing import (
AbstractSet,
Any,
@@ -38,7 +39,6 @@ from django.utils.translation import gettext as _
from typing_extensions import TypedDict
from version import API_FEATURE_LEVEL, ZULIP_MERGE_BASE, ZULIP_VERSION
-from zerver.decorator import cachify
from zerver.lib.exceptions import JsonableError
from zerver.lib.message import MessageDict
from zerver.lib.narrow import build_narrow_filter
@@ -938,7 +938,7 @@ def process_message_event(
message_type: str = wide_dict["type"]
sending_client: str = wide_dict["client"]
- @cachify
+ @lru_cache(maxsize=None)
def get_client_payload(apply_markdown: bool, client_gravatar: bool) -> Dict[str, Any]:
return MessageDict.finalize_payload(
wide_dict,