Skip to content

Base Cache Strategy

To implement a custom cache strategy, you need to create a new class that extends the CacheStrategy class and implement the abstract methods.

See a naive implementation of a memcached cache strategy below:

import pickle

from pymemcache.client import base

import ckanext.charts.config as config
from ckanext.charts.cache import CacheStrategy
from ckanext.charts import types

class MemcachedCache(CacheStrategy):
    """Cache data to Memcached"""

    def __init__(self):
        self.client = base.Client(('localhost', 11211))

    def get_data(self, key: str) -> types.ChartData | None:
        """Return data from cache if exists"""
        try:
            raw_data = self.client.get(key)

            if not raw_data:
                return None

            return pickle.loads(raw_data)
        except Exception:
            log.exception(f"Failed to get data for key: {key}")
            return None

    def set_data(self, key: str, data: types.ChartData):
        """Serialize data and save to Memcached"""
        cache_ttl = config.get_memcached_cache_ttl()
        payload = pickle.dumps(data)

        try:
            self.client.set(key, payload, expire=cache_ttl)
        except Exception:
            log.exception(f"Failed to save data to Memcached for key: {key}")

    def invalidate(self, key: str):
        """Invalidate cache by key"""
        try:
            self.client.delete(key)
        except Exception:
            log.exception(f"Failed to invalidate cache for key: {key}")

Bases: ABC

Cache strategy interface.

Defines the abstracts methods for cache strategies.

get_data(key) abstractmethod

Return data and settings from cache if exists.

PARAMETER DESCRIPTION
key

The cache key to retrieve the data.

TYPE: str

RETURNS DESCRIPTION
ChartData | None

ChartData or None if not found.

invalidate(key) abstractmethod

Invalidate cache by key.

PARAMETER DESCRIPTION
key

The cache key to invalidate.

TYPE: str

set_data(key, data) abstractmethod

Store data and settings to cache.

PARAMETER DESCRIPTION
key

The cache key to store the data.

TYPE: str

data

The ChartData to be stored.

TYPE: ChartData