diff --git a/optimade/server/mappers/entries.py b/optimade/server/mappers/entries.py index fc1953696..0082384c9 100644 --- a/optimade/server/mappers/entries.py +++ b/optimade/server/mappers/entries.py @@ -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",) @@ -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. @@ -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 @@ -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. @@ -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. @@ -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.