Skip to content
Snippets Groups Projects

ENH: add entity getters and cached functions

Merged Henrik tom Wörden requested to merge f-entity-getters into dev
All threads resolved!
Files
3
+ 17
9
@@ -2,8 +2,9 @@
#
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2023 Henrik tom Wörden <h.tomwoerden@indiscale.com>
# Copyright (C) 2023 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2023 Henrik tom Wörden <h.tomwoerden@indiscale.com>
# Copyright (C) 2023 Daniel Hornung <d.hornung@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
@@ -23,14 +24,14 @@
This module provides some cached versions of functions that retrieve Entities from a remote server.
"""
from typing import Union
from enum import Enum
from functools import lru_cache
from typing import Union
from .utils import get_entity
from .common.models import execute_query, Entity, Container
from enum import Enum
# roughly 1GB for typical entity sizes
DEFAULT_SIZE = 33333
@@ -40,7 +41,7 @@ _DUMMY_CACHE = {}
class AccessType(Enum):
"""Different access types for cached queries. Needed for filling the cache manually with
:func:`fill_cache` .
:func:`cache_fill` .
"""
QUERY = 1
@@ -84,7 +85,7 @@ If a query phrase is given, the result must be unique. If this is not what you
def cached_query(query_string) -> Container:
"""A cached version of the :func:`caosdb.execute_query<caosdb.common.models.execute_query>` function.
"""A cached version of :func:`caosdb.execute_query<caosdb.common.models.execute_query>`.
All additional arguments are at their default values.
@@ -120,21 +121,28 @@ def cache_clear() -> None:
def cache_info():
"""Empty the cache that is used by `cached_query` and `cached_get_entity_by`."""
"""Return info about the cache that is used by `cached_query` and `cached_get_entity_by`.
Returns
-------
out: named tuple
See the standard library :func:`functools.lru_cache` for details."""
return _cached_access.cache_info()
def cache_initialize(maxsize=DEFAULT_SIZE) -> None:
"""Create a new cache with the given size for `cached_query` and `cached_get_entity_by`.
The old cache is removed with all its content.
This implies a call of :func:`cache_clear`, the old cache is emptied.
"""
cache_clear()
global _cached_access
_cached_access = lru_cache(maxsize=maxsize)(_cached_access.__wrapped__)
def fill_cache(items: dict, kind: AccessType = AccessType.EID, unique: bool = True) -> None:
def cache_fill(items: dict, kind: AccessType = AccessType.EID, unique: bool = True) -> None:
"""Add entries to the cache manually.
This allows to fill the cache without actually submitting queries. Note that this does not
Loading