Skip to content
Snippets Groups Projects
Commit fffca594 authored by Dominic Kempf's avatar Dominic Kempf
Browse files

Define a cache context that allows to temporarily add global tags

parent a82b2b6b
No related branches found
No related tags found
No related merge requests found
from __future__ import absolute_import
from dune.perftool.generation.cache import (generator_factory,
from dune.perftool.generation.cache import (cache_context,
generator_factory,
retrieve_cache_items,
delete_cache_items,
)
......
......@@ -7,9 +7,35 @@ a complex requirement structure. This includes:
from __future__ import absolute_import
from pytools import memoize
# have one cache the module level. It is easier than handing around an instance of it.
_cache = {}
# Implement a context manager that allows to temporarily define tags globally.
_cache_context_stack = []
class _CacheContext(object):
def __init__(self, tags):
self.tags = tags
def __enter__(self):
_cache_context_stack.append(self.tags)
def __exit__(self, exc_type, exc_value, traceback):
_cache_context_stack.pop()
def cache_context(*tags):
return _CacheContext(tags)
def _get_context_tags():
result = tuple()
for items in _cache_context_stack:
result = result + items
return result
def _freeze(data):
""" A function that deterministically generates an
......@@ -105,6 +131,13 @@ class _RegisteredFunction(object):
):
self.func = func
self.cache_key_generator = cache_key_generator
# Append the current context item tags to the given ones
if 'item_tags' in kwargs:
kwargs['item_tags'] = tuple(kwargs['item_tags']) + _get_context_tags()
else:
kwargs['item_tags'] = _get_context_tags()
self.itemtype = _construct_cache_item_type("CacheItemType", **kwargs)
def __call__(self, *args, **kwargs):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment