cache: Used lru_cache from functools instead of django.utils.lru_cache.

Django 3.0 removed private Python 2 compatibility APIs
so used lru_cache() directly from functools.

We cast lru_cache to Any to avoid attr-defined error in mypy since we
are adding extra field, 'key_prefix', to this object later.
This commit is contained in:
arpit551
2020-08-14 13:19:33 +05:30
committed by Tim Abbott
parent 2b6989a40f
commit af3a34fbca
2 changed files with 5 additions and 4 deletions

View File

@@ -8,7 +8,7 @@ import re
import sys import sys
import time import time
import traceback import traceback
from functools import wraps from functools import lru_cache, wraps
from typing import ( from typing import (
TYPE_CHECKING, TYPE_CHECKING,
Any, Any,
@@ -29,7 +29,6 @@ from django.core.cache import caches
from django.core.cache.backends.base import BaseCache from django.core.cache.backends.base import BaseCache
from django.db.models import Q from django.db.models import Q
from django.http import HttpRequest from django.http import HttpRequest
from django.utils.lru_cache import lru_cache
from zerver.lib.utils import make_safe_digest, statsd, statsd_key from zerver.lib.utils import make_safe_digest, statsd, statsd_key
@@ -669,7 +668,9 @@ def ignore_unhashable_lru_cache(maxsize: int=128, typed: bool=False) -> DECORATO
# In the development environment, we want every file # In the development environment, we want every file
# change to refresh the source files from disk. # change to refresh the source files from disk.
return user_function return user_function
cache_enabled_user_function = internal_decorator(user_function)
# Casting to Any since we're about to monkey-patch this.
cache_enabled_user_function = cast(Any, internal_decorator(user_function))
def wrapper(*args: Any, **kwargs: Any) -> Any: def wrapper(*args: Any, **kwargs: Any) -> Any:
if not hasattr(cache_enabled_user_function, 'key_prefix'): if not hasattr(cache_enabled_user_function, 'key_prefix'):

View File

@@ -1,13 +1,13 @@
import logging import logging
import operator import operator
import os import os
from functools import lru_cache
from itertools import zip_longest from itertools import zip_longest
from typing import Any, Dict, List from typing import Any, Dict, List
import orjson import orjson
from django.conf import settings from django.conf import settings
from django.utils import translation from django.utils import translation
from django.utils.lru_cache import lru_cache
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _