Skip to content

Commit

Permalink
Make use of lru_cache for mapper properties, where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Aug 8, 2022
1 parent 78e2f0c commit 28e3899
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Tuple, Optional, Type, Set, Dict, Any, Union, List, Iterable
from functools import lru_cache
import warnings

from optimade.models.entries import EntryResource
Expand Down Expand Up @@ -76,7 +77,8 @@ class BaseResourceMapper:
ENDPOINT: str

@classmethod
def all_aliases(cls) -> Tuple[Tuple[str, str]]:
@lru_cache(maxsize=1)
def all_aliases(cls) -> Iterable[Tuple[str, str]]:
"""Returns all of the associated aliases for this entry type,
including those defined by the server config. The first member
of each tuple is the OPTIMADE-compliant field name, the second
Expand Down Expand Up @@ -108,6 +110,7 @@ def all_aliases(cls) -> Tuple[Tuple[str, str]]:
)

@classproperty
@lru_cache(maxsize=1)
def SUPPORTED_PREFIXES(cls) -> Set[str]:
"""A set of prefixes handled by this entry type.
Expand Down Expand Up @@ -150,6 +153,7 @@ def ENTRY_RESOURCE_ATTRIBUTES(cls) -> Dict[str, Any]:
return retrieve_queryable_properties(cls.ENTRY_RESOURCE_CLASS.schema())

@classproperty
@lru_cache(maxsize=1)
def ENDPOINT(cls) -> str:
"""Returns the expected endpoint for this mapper, corresponding
to the `type` property of the resource class.
Expand All @@ -163,7 +167,8 @@ def ENDPOINT(cls) -> str:
)

@classmethod
def all_length_aliases(cls) -> Tuple[Tuple[str, str]]:
@lru_cache(maxsize=1)
def all_length_aliases(cls) -> Iterable[Tuple[str, str]]:
"""Returns all of the associated length aliases for this class,
including those defined by the server config.
Expand All @@ -178,6 +183,7 @@ def all_length_aliases(cls) -> Tuple[Tuple[str, str]]:
)

@classmethod
@lru_cache(maxsize=128)
def length_alias_for(cls, field: str) -> Optional[str]:
"""Returns the length alias for the particular field,
or `None` if no such alias is found.
Expand All @@ -192,6 +198,7 @@ def length_alias_for(cls, field: str) -> Optional[str]:
return dict(cls.all_length_aliases()).get(field, None)

@classmethod
@lru_cache(maxsize=128)
def get_backend_field(cls, optimade_field: str) -> str:
"""Return the field name configured for the particular
underlying database for the passed OPTIMADE field name, that would
Expand Down Expand Up @@ -226,6 +233,7 @@ def get_backend_field(cls, optimade_field: str) -> str:
return optimade_field

@classmethod
@lru_cache(maxsize=128)
def alias_for(cls, field: str) -> str:
"""Return aliased field name.
Expand All @@ -247,6 +255,7 @@ def alias_for(cls, field: str) -> str:
return cls.get_backend_field(field)

@classmethod
@lru_cache(maxsize=128)
def get_optimade_field(cls, backend_field: str) -> str:
"""Return the corresponding OPTIMADE field name for the underlying database field,
ready to be used to construct the OPTIMADE-compliant JSON response.
Expand Down Expand Up @@ -274,6 +283,7 @@ def get_optimade_field(cls, backend_field: str) -> str:
)

@classmethod
@lru_cache(maxsize=128)
def alias_of(cls, field: str) -> str:
"""Return de-aliased field name, if it exists,
otherwise return the input field name.
Expand All @@ -296,6 +306,7 @@ def alias_of(cls, field: str) -> str:
return cls.get_optimade_field(field)

@classmethod
@lru_cache(maxsize=1)
def get_required_fields(cls) -> set:
"""Get REQUIRED response fields.
Expand Down

0 comments on commit 28e3899

Please sign in to comment.