Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-pylib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-pylib
Commits
66fe7c7e
Verified
Commit
66fe7c7e
authored
1 year ago
by
Daniel Hornung
Browse files
Options
Downloads
Patches
Plain Diff
MAINT: Refactoring cached.py.
parent
b753e721
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!107
ENH: add entity getters and cached functions
,
!100
ENH: add entity getters and cached functions
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/caosdb/cached.py
+26
-40
26 additions, 40 deletions
src/caosdb/cached.py
with
26 additions
and
40 deletions
src/caosdb/cached.py
+
26
−
40
View file @
66fe7c7e
...
...
@@ -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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment