caching

Utilities for working with notebooks.

set_default_cache_path

set_default_cache_path(cache_path: Path)

Set the path for the temporary cache.


repo_path = nblite.config.get_project_root_and_config()[0]
set_default_cache_path(repo_path / '.tmp_cache')

get_default_cache_path

get_default_cache_path() -> Path|None

Set the path for the temporary cache.


show_doc(this_module.get_default_cache)

get_default_cache

get_default_cache()

Retrieve the default cache.


get_default_cache

get_default_cache()

Retrieve the default cache.


get_cache

get_cache(cache_path: Union[Path,None])

Retrieve a cache instance for the given path. If no path is provided,

the default cache is used. If the cache does not exist, it is created using the specified cache path or the default cache path.


clear_cache_key

clear_cache_key(cache_key, cache: Union[Path,diskcache.Cache,None])

is_in_cache

is_in_cache(key: tuple, cache: Union[Path,diskcache.Cache,None])

memoize

memoize(
   cache: Union[Path,diskcache.Cache,None],
   temp,
   typed,
   expire,
   tag,
   return_cache_key
)

Decorator for memoizing function results to improve performance.

This decorator stores the results of function calls, allowing subsequent calls with the same arguments to retrieve the result from the cache instead of recomputing it. You can specify a cache object or use a temporary cache if none is provided.

Parameters: - cache (Union[Path, diskcache.Cache, None], optional): A cache object or a path to the cache directory. Defaults to a temporary cache if None. - temp (bool, optional): If True, use a temporary cache. Cannot be True if a cache is provided. Defaults to False. - typed (bool, optional): If True, cache function arguments of different types separately. Defaults to True. - expire (int, optional): Cache expiration time in seconds. If None, cache entries do not expire. - tag (str, optional): A tag to associate with cache entries. - return_cache_key (bool, optional): If True, return the cache key along with the result, in the order (cache_key, result). Defaults to False.

Returns: - function: A decorator that applies memoization to the target function.


@memoize(temp=True)
def foo():
    time.sleep(1)
    return "bar"

foo() # Takes 1 second
foo() # Is retrieved from cache and returns immediately
'bar'
@memoize(return_cache_key=True)
async def async_foo():
    time.sleep(1)
    return "bar"

await async_foo() # Takes 1 second
cache_key, result = await async_foo() # Is retrieved from cache and returns immediately
clear_cache_key(cache_key) # Clears the cache key
await async_foo(); # This should again take 1 second