cache¶
Caching of web service responses.
Queries to identifiers.org, OLS, ChEBI and UniChem are cached on disk so that
repeated lookups of the same term do not hit the network again. Caching is on
by default and controlled by pymetadata.CACHE_USE and pymetadata.CACHE_PATH,
which are read at query time.
A cached response is refreshed once it is older than the cache duration of its
service, see CACHE_DURATION_ONTOLOGY and CACHE_DURATION_REGISTRY. If the
refresh fails, because the service is unreachable or answers with an error, the
outdated content is used instead of failing the query and a warning is logged,
see read_json_cache_fallback. Working offline therefore keeps working with
whatever was cached before.
DataclassJSONEncoder
¶
Bases: JSONEncoder
JSON encoder which serializes dataclasses via their __dict__.
cache_age
¶
Get the age of a cache file in hours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path
|
path of the cache file |
required |
Returns:
| Type | Description |
|---|---|
float | None
|
The age in hours, or None if the file does not exist. |
read_json_cache
¶
Read a JSON cache file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path
|
path of the cache file |
required |
max_age
|
float | None
|
maximum age of the content in hours; older content is treated as if it were not cached. Any age is accepted if None |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The cached content. |
Raises:
| Type | Description |
|---|---|
IOError
|
if the cache file does not exist or is older than |
read_json_cache_fallback
¶
Read a cache file of any age, after the query which should refresh it failed.
The library prefers outdated content over no content, so that an unreachable service does not fail a query which was answered before. The age of the content is not checked and a warning names the reason, so that the fallback is visible in the log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_path
|
Path
|
path of the cache file |
required |
reason
|
str
|
why the content could not be refreshed, e.g., the error of the failed query |
required |
Returns:
| Type | Description |
|---|---|
dict | None
|
The cached content, or None if nothing is cached. |
write_json_cache
¶
Write a JSON cache file.
Missing parent directories are created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
data to serialize |
required |
cache_path
|
Path
|
path of the cache file |
required |
json_encoder
|
type[JSONEncoder] | None
|
encoder for objects json cannot serialize, e.g.
|
None
|