TheoChem-VU / TCutility

Utility functions/classes for the TheoCheM programs
https://theochem-vu.github.io/TCutility/
MIT License
6 stars 0 forks source link

Store cached results locally #220

Closed YHordijk closed 7 months ago

YHordijk commented 7 months ago

Currently we have a number of useful functions in tcutility.cache, but it would be good to be able to store it locally. I propose we add the tcutility.cache.cache_file decorator. This will cache the results of a function and store it in a .json file.

Example:

@cache_file('test.json')
def test(a, b, c):
    return a + b * c

test(1, 2, 3)  # this will run the function
test(1, 2, 3)  # this will retrieve it
test(1, 4, 3)  # this will run the function
test(1, 2, 3)  # this will retrieve it
test(1, 4, 3)  # this will retrieve it

This script will store results in test.json. When a function call was already made with the same function, args and kwargs it will read it from the test.json file.