Skip to content
Snippets Groups Projects
Verified Commit 66fe7c7e authored by Daniel Hornung's avatar Daniel Hornung
Browse files

MAINT: Refactoring cached.py.

parent b753e721
No related branches found
No related tags found
2 merge requests!107ENH: add entity getters and cached functions,!100ENH: add entity getters and cached functions
......@@ -34,17 +34,15 @@ from enum import Enum
# roughly 1GB for typical entity sizes
DEFAULT_SIZE = 33333
# Those dict caches are solely for filling the real cache manually (e.g. to reuse older query
# results)
_DUMB_BY_NAME_CACHE = {}
_DUMB_BY_PATH_CACHE = {}
_DUMB_BY_EID_CACHE = {}
_DUMB_BY_QUERY_CACHE = {}
# This dict cache is solely for filling the real cache manually (e.g. to reuse older query results)
_DUMMY_CACHE = {}
class AccessType(Enum):
""" This module looks for entities based on those kinds of information. """
"""Different access types for cached queries. Needed for filling the cache manually with
:func:`fill_cache` .
"""
QUERY = 1
PATH = 2
EID = 3
......@@ -78,9 +76,15 @@ def cached_get_entity_by(eid: Union[str, int] = None, name: str = None, path: st
if query is not None:
return _cached_access(AccessType.QUERY, query, unique=True)
raise ValueError("Not all arguments may be None.")
def cached_query(query_string) -> Container:
""" a cached version of db.execute_query """
"""A cached version of the :func:`caosdb.execute_query<caosdb.common.models.execute_query>` function.
All additional arguments are at their default values.
"""
return _cached_access(AccessType.QUERY, query_string, unique=False)
......@@ -90,24 +94,20 @@ def _cached_access(kind: AccessType, value: Union[str, int], unique=True):
# Due to the arguments, the cache has kind of separate sections for cached_query and
# cached_get_entity_by with the different AccessTypes. However, there is only one cache size.
# The dumb dict caches are only to allow filling the cache manually
# The dummy dict cache is only for filling the cache manually, it is deleted afterwards.
if value in _DUMMY_CACHE:
return _DUMMY_CACHE[value]
if kind == AccessType.QUERY:
if value in _DUMB_BY_QUERY_CACHE:
return _DUMB_BY_QUERY_CACHE[value]
return execute_query(value)
elif kind == AccessType.NAME:
if value in _DUMB_BY_NAME_CACHE:
return _DUMB_BY_NAME_CACHE[value]
return execute_query(value, unique=unique)
if kind == AccessType.NAME:
return get_entity.get_entity_by_name(value)
elif kind == AccessType.EID:
if value in _DUMB_BY_EID_CACHE:
return _DUMB_BY_EID_CACHE[value]
if kind == AccessType.EID:
return get_entity.get_entity_by_id(value)
elif kind == AccessType.PATH:
if value in _DUMB_BY_PATH_CACHE:
return _DUMB_BY_PATH_CACHE[value]
if kind == AccessType.PATH:
return get_entity.get_entity_by_path(value)
raise ValueError(f"Unknown AccessType: {kind}")
def cache_clear() -> None:
""" Empty the cache that is used by cached_query and cached_get_entity_by """
......@@ -133,26 +133,12 @@ def fill_cache(items: dict, kind: AccessType = AccessType.EID, unique=True) -> N
This allows to fill the cache without actually submitting queries.
"""
# 1. add the given items to the corresponding dumb dict cache
if kind == AccessType.EID:
_DUMB_BY_EID_CACHE.update(items)
elif kind == AccessType.NAME:
_DUMB_BY_NAME_CACHE.update(items)
elif kind == AccessType.PATH:
_DUMB_BY_PATH_CACHE.update(items)
elif kind == AccessType.QUERY:
_DUMB_BY_QUERY_CACHE.update(items)
# 1. add the given items to the corresponding dummy dict cache
_DUMMY_CACHE.update(items)
# 2. call the cache function with each key (this only results in a dict look up)
for key in items.keys():
_cached_access(kind, key, unique=unique)
# 3. empty the dumpy dict cache again
if kind == AccessType.EID:
_DUMB_BY_EID_CACHE.clear()
elif kind == AccessType.NAME:
_DUMB_BY_NAME_CACHE.clear()
elif kind == AccessType.PATH:
_DUMB_BY_PATH_CACHE.clear()
elif kind == AccessType.QUERY:
_DUMB_BY_QUERY_CACHE.clear()
# 3. empty the dummy dict cache again
_DUMMY_CACHE.clear()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment