Skip to content

Cache

clear_cache_region(ext_name, *modules, cache_name=None)

Clears cached functions in the given cache region.

Parameters:

Name Type Description Default
ext_name

the name of the extension (without the ckanext-)

required
modules

modules containing cached functions

()
cache_name

a name for the cache region, if different from the ext_name

None
Source code in ckantools/cache.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def clear_cache_region(ext_name, *modules, cache_name=None):
    """
    Clears cached functions in the given cache region.

    :param ext_name: the name of the extension (without the ckanext-)
    :param modules: modules containing cached functions
    :param cache_name: a name for the cache region, if different from the ext_name
    """
    cache_name = cache_name or ext_name
    cache_opts = cache_regions.get(cache_name)
    if cache_opts is None:
        # this shouldn't happen, but just in case
        cache_opts = {}
        for k, v in toolkit.config.items():
            if k.startswith(f'ckanext.{ext_name}.cache.'):
                cache_opts[k.split('.')[-1]] = v
    # cache_managers does not usually seem to be populated so just construct a new ref
    cache_manager = CacheManager(**cache_opts)

    cached_functions = []

    for module in modules:

        def _is_cached_func(f):
            is_func = inspect.isfunction(f)
            has_ns = hasattr(f, '_arg_namespace')
            in_rg = getattr(f, '_arg_region', None) == cache_name
            return is_func and has_ns and in_rg

        cached_functions += inspect.getmembers(module, _is_cached_func)

    for _, func in cached_functions:
        # each function has its own namespace that needs to be cleared
        try:
            cache = cache_manager.get_cache(func._arg_namespace)
            cache.clear()
        except Exception as e:
            raise CacheClearError(f'Failed to clear cache for {func.__name__}') from e

configure_cache(ckan_config, ext_name, cache_name=None)

Configures a beaker cache region using settings from the CKAN config.

Use in plugin.py:IConfigurable.configure().

Parameters:

Name Type Description Default
ckan_config

CKAN config from configure()

required
ext_name

the name of the extension (without the ckanext-)

required
cache_name

a name for the cache region, if different from the ext_name

None
Source code in ckantools/cache.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def configure_cache(ckan_config, ext_name, cache_name=None):
    """
    Configures a beaker cache region using settings from the CKAN config.

    Use in plugin.py:IConfigurable.configure().

    :param ckan_config: CKAN config from configure()
    :param ext_name: the name of the extension (without the ckanext-)
    :param cache_name: a name for the cache region, if different from the ext_name
    """
    options = {}
    for k, v in ckan_config.items():
        if k.startswith(f'ckanext.{ext_name}.cache.'):
            options[k.split('.')[-1]] = v
    cache_name = cache_name or ext_name
    cache_regions.update({cache_name: options})