def__enter__(self):""" Record the original cache state and turn it to the target state. """self.previous_state=is_caching_enabled()ifself.on:enable_caching()else:disable_caching()def__exit__(self,exc_type,exc_val,exc_tb):""" Restore the original cache state. """ifself.previous_state:enable_caching()else:disable_caching()
[docs]defdataset_cache_control(on):""" A more easy-to-use decorator for functions that need to control the cache state temporarily. """defdataset_cache_decorator(func):@wraps(func)defwrapped_function(*args,**kwargs):withDatasetCacheControl(on=on):returnfunc(*args,**kwargs)returnwrapped_functionreturndataset_cache_decorator