Skip to content

Commit

Permalink
Increase mapper lru_cache size to allow for multiple mapper type to b…
Browse files Browse the repository at this point in the history
…e cached simultaneously
  • Loading branch information
ml-evs committed Dec 20, 2022
1 parent 5478fb8 commit 5584cab
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

from optimade.models.entries import EntryResource

# A number that approximately tracks the number of types with mappers
# so that the global caches can be set to the correct size.
# See https://github.com/Materials-Consortia/optimade-python-tools/issues/1434
# for the details.
NUM_ENTRY_TYPES = 4

__all__ = ("BaseResourceMapper",)


Expand All @@ -27,8 +33,7 @@ def __get__(self, _, owner):


class BaseResourceMapper:
"""
Generic Resource Mapper that defines and performs the mapping
"""Generic Resource Mapper that defines and performs the mapping
between objects in the database and the resource objects defined by
the specification.
Expand Down Expand Up @@ -70,8 +75,13 @@ class BaseResourceMapper:
RELATIONSHIP_ENTRY_TYPES: Set[str] = {"references", "structures"}
TOP_LEVEL_NON_ATTRIBUTES_FIELDS: Set[str] = {"id", "type", "relationships", "links"}

def __init__(self):
raise RuntimeError(
f"{self.__class__.__name__!r} should not be instantiated directly, instead use its functionality via the `@classmethods`"
)

@classmethod
@lru_cache(maxsize=1)
@lru_cache(maxsize=NUM_ENTRY_TYPES)
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
Expand Down Expand Up @@ -147,7 +157,7 @@ def ENTRY_RESOURCE_ATTRIBUTES(cls) -> Dict[str, Any]:
return retrieve_queryable_properties(cls.ENTRY_RESOURCE_CLASS.schema())

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

@classmethod
@lru_cache(maxsize=1)
@lru_cache(maxsize=NUM_ENTRY_TYPES)
def all_length_aliases(cls) -> Tuple[Tuple[str, str], ...]:
"""Returns all of the associated length aliases for this class,
including those defined by the server config.
Expand Down Expand Up @@ -300,7 +310,7 @@ def alias_of(cls, field: str) -> str:
return cls.get_optimade_field(field)

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

0 comments on commit 5584cab

Please sign in to comment.