Skip to content

Commit

Permalink
add RESULTCACHING_DISABLE option to turn off the framework (load/save)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschrimpf committed Jun 21, 2019
1 parent 03dc707 commit 033fd10
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ By default, results will be stored in `~/.result_caching`, this can be
changed through the environment variable `RESULTCACHING_HOME`.

`cache` will only hold results in memory and not write them to disk.

## Environment variables
| Variable | description |
|-----------------------|----------------------------------------------------------------------------------|
| RESULTCACHING_HOME | directory to cache results (benchmark ceilings) in, `~/.result_caching` by default |
| RESULTCACHING_DISABLE | `'1'` to disable loading and saving of results, functions will be called directly |
11 changes: 8 additions & 3 deletions result_caching/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def get_function_identifier(function, call_args):
return function_identifier


def is_enabled():
return os.getenv('RESULTCACHING_DISABLE', '0') != '1'


class _Storage(object):
def __init__(self, identifier_ignore=()):
"""
Expand All @@ -41,13 +45,14 @@ def __call__(self, function):
def wrapper(*args, **kwargs):
call_args = self.getcallargs(function, *args, **kwargs)
function_identifier = self.get_function_identifier(function, call_args)
if self.is_stored(function_identifier):
if is_enabled() and self.is_stored(function_identifier):
self._logger.debug("Loading from storage: {}".format(function_identifier))
return self.load(function_identifier)
self._logger.debug("Running function: {}".format(function_identifier))
result = function(*args, **kwargs)
self._logger.debug("Saving to storage: {}".format(function_identifier))
self.save(result, function_identifier)
if is_enabled():
self._logger.debug("Saving to storage: {}".format(function_identifier))
self.save(result, function_identifier)
return result

return wrapper
Expand Down
19 changes: 19 additions & 0 deletions tests/test___init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,25 @@ def __repr__(self):
# second call returns same thing and doesn't actually call function again
assert c.f(1) == 1

def test_disable_store(self):
with tempfile.TemporaryDirectory() as storage_dir:
os.environ['RESULTCACHING_HOME'] = storage_dir
os.environ['RESULTCACHING_DISABLE'] = '1'

function_calls = 0

@store()
def func(x):
nonlocal function_calls
function_calls += 1
return x

assert func(1) == 1
assert function_calls == 1
assert not os.listdir(storage_dir)
assert func(1) == 1
assert function_calls == 2


class TestCache:
def test(self):
Expand Down

0 comments on commit 033fd10

Please sign in to comment.