releso.util.caching.RelesoSporCache
- class releso.util.caching.RelesoSporCache(db_path: str, example_data: dict)
Bases:
objectA simple SQLite cache for storing SPOR results.
This cache is designed for use in Releso’s SPOR steps, allowing for efficient storage and retrieval of key-value pairs where keys are strings and values are JSON-serializable dictionaries. The cache is backed by an SQLite database, which is created if it does not already exist. It is designed to be used both in single as well as multi environment setups. In addition, it can allow caching between different training runs. As long as the key and value pair is consistent, the cache can be reused across different runs.
It is advisable to only cache the input and output if the most computationally expensive part of the SPOR step and not cache the objective and reward since these are usually not expensive to compute and can change between runs.
Ensure that the key that you use a string that uniquely identifies the input parameters of the SPOR step. You can use the
make_cache_key()method to create a consistent key based on the input parameters. The function allows list and numpy array as inputs. The input will be flattened and rounded to a specified number of decimal places to ensure that the key is unique and consistent across runs and floating point arithmetic does not produce additional superfluous keys.Example usage:
from releso.util.caching import RelesoSporCache def main(args, logger, func_data): # first call of function if func_data is None: # the data_keys and values are not the input parameters of the # function, but rather the data that you want to cache. func_data["cache"] = RelesoSporCache( db_path="spor_cache.db", example_data={"data_key1": "value1", "data_key2": "value2"}, ) if args.reset: # don't touch the cache, only reset the other func_data entries input_params = args.json_object["info"]["geometry_information"] key = RelesoSporCache.make_cache_key(input_params) cached_value = func_data["cache"].get(key) if cached_value is not None: # Use cached value pass else: # Perform expensive computation result = expensive_computation(input_params) # Store result in cache # The data you send here needs to be serializable by JSON. func_data["cache"].set( key, {"data_key1": result[0], "data_key2": result[1]}, )
- Params:
db_path (str): Path to the SQLite database file. This file will be created if it does not exist. The cache will store key-value pairs where keys are strings and values are JSON-serializable dictionaries.
- __init__(db_path: str, example_data: dict)
Methods
get(key)Retrieve a value from the cache.
make_cache_key(key_array[, rounding])Create a cache key based on the provided arguments.
set(key, value)Store a value in the cache.
- get(key: str) list[dict] | None
Retrieve a value from the cache.
- Parameters:
key (str) – The key to retrieve the value for.
- Returns:
The cached value as a list of dictionaries, or None if not found.
- Return type:
list[dict] | None
- static make_cache_key(key_array: ndarray | list[list[float]] | list[float], rounding: int = 6) str
Create a cache key based on the provided arguments.
This function was initially generated by AI.
- Parameters:
key_array (np.ndarray | list[list[float]]) – Array or list of control point y-coordinates to be used as a key for caching.
rounding (int) – Number of decimal places to round the values in the key. Default is 6.
- Returns:
A JSON-serialized string that serves as a unique key for caching.
- Return type:
str
- set(key: str, value: dict)
Store a value in the cache.
- Parameters:
key (str) – The key to store the value under.
value (dict) – The value to store, must be a dictionary with keys matching the example data keys.