Skip to content

Commit

Permalink
Allow disabling all caches (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kavigupta authored Jan 23, 2025
1 parent 94ae0b3 commit aaca77c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions permacache/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from .cache_miss_error import CacheMissError, error_on_miss_global
from .dict_function import drop_if, drop_if_equal
from .hash import migrated_attrs, stable_hash, stringify
from .no_cache import no_cache_global
from .swap_unpickler import renamed_symbol_unpickler, swap_unpickler_context_manager
4 changes: 4 additions & 0 deletions permacache/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from appdirs import user_cache_dir

from permacache.no_cache import no_cache_global
from permacache.out_file_cache import (
add_file_cache_info,
do_copy_files,
Expand Down Expand Up @@ -38,6 +39,9 @@ def error_on_miss(self):
return error_on_miss(self)

def __call__(self, *args, **kwargs):
if no_cache_global.no_cache:
return self._run_underlying(*args, **kwargs)

key = self.key_function(args, kwargs, parallel=self.parallel)
if isinstance(key, parallel_output):
return self.call_parallel(key.values, args, kwargs)
Expand Down
18 changes: 18 additions & 0 deletions permacache/no_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class no_cache_global:
"""
context manager that sets the error on miss attribute globally, and the
resets it when the context is exited
"""

no_cache = False

def __init__(self):
self.old = None

def __enter__(self):
self.old = no_cache_global.no_cache
no_cache_global.no_cache = True

def __exit__(self, *args):
assert self.old is not None
no_cache_global.no_cache = self.old
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="permacache",
version="3.10.0",
version="3.11.0",
author="Kavi Gupta",
author_email="[email protected]",
description="Permanant cache.",
Expand Down
12 changes: 11 additions & 1 deletion tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import tempfile
import unittest

from permacache import cache
from permacache import cache, no_cache_global
from permacache.swap_unpickler import (
renamed_symbol_unpickler,
swap_unpickler_context_manager,
Expand Down Expand Up @@ -44,6 +44,16 @@ def test_basic(self):
self.assertEqual(self.f(3, 2, 1, 0, -1), (3, 2, 1, (0, -1)))
self.assertEqual(fn.counter, 3)

def test_basic_with_disabled_cache(self):
self.assertEqual(fn.counter, 0)
self.assertEqual(self.f(1, 2, 3), (1, 2, 3, ()))
self.assertEqual(fn.counter, 1)
self.assertEqual(self.f(1, 2, 3), (1, 2, 3, ()))
self.assertEqual(fn.counter, 1)
with no_cache_global():
self.assertEqual(self.f(1, 2, 3), (1, 2, 3, ()))
self.assertEqual(fn.counter, 2)


def g(x):
g.counter += 1
Expand Down

0 comments on commit aaca77c

Please sign in to comment.