Browse Source

refactor: replace memoize with django utils (#67694)

Let's remove internal implementation in favor of `functools`

Ref: https://github.com/getsentry/team-processing/issues/135
Yagiz Nizipli 11 months ago
parent
commit
8b8218cb46

+ 2 - 2
src/sentry/api/endpoints/organization_details.py

@@ -9,6 +9,7 @@ from django.db import models, router, transaction
 from django.db.models.query_utils import DeferredAttribute
 from django.urls import reverse
 from django.utils import timezone as django_timezone
+from django.utils.functional import cached_property
 from rest_framework import serializers, status
 
 from bitfield.types import BitHandler
@@ -72,7 +73,6 @@ from sentry.services.organization.provisioning import (
     organization_provisioning_service,
 )
 from sentry.utils.audit import create_audit_entry
-from sentry.utils.cache import memoize
 
 ERR_DEFAULT_ORG = "You cannot remove the default organization."
 ERR_NO_USER = "This request requires an authenticated user."
@@ -231,7 +231,7 @@ class OrganizationSerializer(BaseOrganizationSerializer):
     relayPiiConfig = serializers.CharField(required=False, allow_blank=True, allow_null=True)
     apdexThreshold = serializers.IntegerField(min_value=1, required=False)
 
-    @memoize
+    @cached_property
     def _has_legacy_rate_limits(self):
         org = self.context["organization"]
         return OrganizationOption.objects.filter(

+ 2 - 2
src/sentry/auth/system.py

@@ -9,9 +9,9 @@ from django.conf import settings
 from django.contrib.auth.models import AnonymousUser
 from django.http.request import HttpRequest
 from django.utils.crypto import constant_time_compare
+from django.utils.functional import cached_property
 
 from sentry import options
-from sentry.utils.cache import memoize
 
 INTERNAL_NETWORKS = [
     ipaddress.ip_network(str(net), strict=False) for net in settings.INTERNAL_SYSTEM_IPS
@@ -76,7 +76,7 @@ class SystemToken:
     def is_expired(self) -> bool:
         return False
 
-    @memoize
+    @cached_property
     def user(self) -> AnonymousUser:
         user = AnonymousUser()
         user.is_active = True

+ 2 - 2
src/sentry/db/models/fields/node.py

@@ -8,11 +8,11 @@ from typing import Any
 from uuid import uuid4
 
 from django.db.models.signals import post_delete
+from django.utils.functional import cached_property
 
 from sentry import nodestore
 from sentry.db.models.utils import Creator
 from sentry.utils import json
-from sentry.utils.cache import memoize
 from sentry.utils.canonical import CANONICAL_TYPES, CanonicalKeyDict
 from sentry.utils.strings import decompress
 
@@ -98,7 +98,7 @@ class NodeData(MutableMapping):
     def copy(self):
         return self.data.copy()
 
-    @memoize
+    @cached_property
     def data(self):
         """
         Get the current data object, fetching from nodestore if necessary.

+ 4 - 4
src/sentry/eventstore/models.py

@@ -13,6 +13,7 @@ import sentry_sdk
 from dateutil.parser import parse as parse_date
 from django.conf import settings
 from django.utils.encoding import force_str
+from django.utils.functional import cached_property
 
 from sentry import eventtypes
 from sentry.db.models import NodeData
@@ -24,7 +25,6 @@ from sentry.models.event import EventDict
 from sentry.snuba.events import Columns
 from sentry.spans.grouping.api import load_span_grouping_config
 from sentry.utils import json
-from sentry.utils.cache import memoize
 from sentry.utils.canonical import CanonicalKeyView
 from sentry.utils.safe import get_path, trim
 from sentry.utils.strings import truncatechars
@@ -292,7 +292,7 @@ class BaseEvent(metaclass=abc.ABCMeta):
     def get_interfaces(self) -> Mapping[str, Interface]:
         return cast(Mapping[str, Interface], CanonicalKeyView(get_interfaces(self.data)))
 
-    @memoize
+    @cached_property
     def interfaces(self) -> Mapping[str, Interface]:
         return self.get_interfaces()
 
@@ -546,7 +546,7 @@ class BaseEvent(metaclass=abc.ABCMeta):
 
         return data
 
-    @memoize
+    @cached_property
     def search_message(self) -> str:
         """
         The internal search_message attribute is only used for search purposes.
@@ -777,7 +777,7 @@ class GroupEvent(BaseEvent):
             return cast(str, self._snuba_data[column])
         return None
 
-    @memoize
+    @cached_property
     def search_message(self) -> str:
         message = super().search_message
         # Include values from the occurrence in our search message as well, so that occurrences work

+ 2 - 2
src/sentry/integrations/slack/requests/action.py

@@ -2,12 +2,12 @@ from __future__ import annotations
 
 from typing import Any
 
+from django.utils.functional import cached_property
 from rest_framework import status
 
 from sentry.integrations.slack.requests.base import SlackRequest, SlackRequestError
 from sentry.models.group import Group
 from sentry.utils import json
-from sentry.utils.cache import memoize
 from sentry.utils.json import JSONData
 
 
@@ -24,7 +24,7 @@ class SlackActionRequest(SlackRequest):
     def type(self) -> str:
         return str(self.data.get("type"))
 
-    @memoize
+    @cached_property
     def callback_data(self) -> JSONData:
         """
         We store certain data in ``callback_id`` as JSON. It's a bit hacky, but

+ 4 - 3
src/sentry/interfaces/security.py

@@ -1,7 +1,8 @@
+from django.utils.functional import cached_property
+
 from sentry.interfaces.base import Interface
 from sentry.security import csp
 from sentry.utils import json
-from sentry.utils.cache import memoize
 from sentry.web.helpers import render_to_string
 
 __all__ = ("Csp", "Hpkp", "ExpectCT", "ExpectStaple")
@@ -179,11 +180,11 @@ class Csp(SecurityReport):
             "sentry/partial/interfaces/csp_email.html", {"data": self.get_api_context()}
         )
 
-    @memoize
+    @cached_property
     def normalized_blocked_uri(self):
         return csp.normalize_value(self.blocked_uri)
 
-    @memoize
+    @cached_property
     def local_script_violation_type(self):
         """
         If this is a locally-sourced script-src error, gives the type.

+ 2 - 2
src/sentry/mediators/alert_rule_actions/creator.py

@@ -1,4 +1,5 @@
 from django.db import router
+from django.utils.functional import cached_property
 
 from sentry.coreapi import APIError
 from sentry.mediators.external_requests.alert_rule_action_requester import (
@@ -9,7 +10,6 @@ from sentry.mediators.mediator import Mediator
 from sentry.mediators.param import Param
 from sentry.models.integrations.sentry_app_component import SentryAppComponent
 from sentry.models.integrations.sentry_app_installation import SentryAppInstallation
-from sentry.utils.cache import memoize
 
 
 class AlertRuleActionCreator(Mediator):
@@ -39,6 +39,6 @@ class AlertRuleActionCreator(Mediator):
             fields=self.fields,
         )
 
-    @memoize
+    @cached_property
     def sentry_app(self):
         return self.install.sentry_app

+ 2 - 2
src/sentry/mediators/external_issues/issue_link_creator.py

@@ -1,4 +1,5 @@
 from django.db import router
+from django.utils.functional import cached_property
 
 from sentry.coreapi import APIUnauthorized
 from sentry.mediators.external_issues.creator import Creator
@@ -9,7 +10,6 @@ from sentry.models.group import Group
 from sentry.models.platformexternalissue import PlatformExternalIssue
 from sentry.services.hybrid_cloud.app import RpcSentryAppInstallation
 from sentry.services.hybrid_cloud.user import RpcUser
-from sentry.utils.cache import memoize
 
 
 class IssueLinkCreator(Mediator):
@@ -50,6 +50,6 @@ class IssueLinkCreator(Mediator):
             identifier=self.response["identifier"],
         )
 
-    @memoize
+    @cached_property
     def sentry_app(self):
         return self.install.sentry_app

+ 3 - 3
src/sentry/mediators/external_requests/alert_rule_action_requester.py

@@ -4,6 +4,7 @@ from urllib.parse import urlparse, urlunparse
 from uuid import uuid4
 
 from django.db import router
+from django.utils.functional import cached_property
 from requests import RequestException
 from requests.models import Response
 
@@ -12,7 +13,6 @@ from sentry.mediators.mediator import Mediator
 from sentry.mediators.param import Param
 from sentry.models.integrations.sentry_app_installation import SentryAppInstallation
 from sentry.utils import json
-from sentry.utils.cache import memoize
 
 logger = logging.getLogger("sentry.mediators.external-requests")
 
@@ -98,7 +98,7 @@ class AlertRuleActionRequester(Mediator):
             "Sentry-App-Signature": self.sentry_app.build_signature(self.body),
         }
 
-    @memoize
+    @cached_property
     def body(self):
         return json.dumps(
             {
@@ -107,6 +107,6 @@ class AlertRuleActionRequester(Mediator):
             }
         )
 
-    @memoize
+    @cached_property
     def sentry_app(self):
         return self.install.sentry_app

+ 3 - 3
src/sentry/mediators/external_requests/issue_link_requester.py

@@ -6,6 +6,7 @@ from urllib.parse import urlparse
 from uuid import uuid4
 
 from django.db import router
+from django.utils.functional import cached_property
 
 from sentry.coreapi import APIError
 from sentry.http import safe_urlread
@@ -16,7 +17,6 @@ from sentry.models.group import Group
 from sentry.services.hybrid_cloud.app import RpcSentryAppInstallation
 from sentry.services.hybrid_cloud.user import RpcUser
 from sentry.utils import json
-from sentry.utils.cache import memoize
 
 logger = logging.getLogger("sentry.mediators.external-requests")
 
@@ -112,7 +112,7 @@ class IssueLinkRequester(Mediator):
             "Sentry-App-Signature": self.sentry_app.build_signature(self.body),
         }
 
-    @memoize
+    @cached_property
     def body(self):
         body: dict[str, Any] = {"fields": {}}
         for name, value in self.fields.items():
@@ -126,6 +126,6 @@ class IssueLinkRequester(Mediator):
         body["actor"] = {"type": "user", "id": self.user.id, "name": self.user.name}
         return json.dumps(body)
 
-    @memoize
+    @cached_property
     def sentry_app(self):
         return self.install.sentry_app

Some files were not shown because too many files changed in this diff