Table Of Contents

Previous topic

9.8. Requests TLS Session

Next topic

10. Testing

9.9. MemoryCache

The MemoryCache implements a global key-value in memory cache which is not persisted to disk when SPS restarts. The memory cache is common for all plugins, which means that the keys may conflict between plugins. The cache should be used with a suitable prefix applied to the keys, such as oneidentity:starling which contains the vendor and product separated by a colon (:). The time to live of a cache entry may be specified on a per usage basis or per service, the value 0 means the entry never expires.

The cache is set up to handle plugins data specifically, which means these two use cases:

  • storing user data indefinitely, such as person - device associations,

  • storing external service data for a limited time, such as time limited access tokens for REST APIs.

In both cases the pattern is to get the data, then if the data existed,use it, otherwise create the data and set it in the cache with or without a time to live (TTL), i.e. expire time. The set operation always overwrites data and there is no delete operation.

In case the cache gets full, the least frequently used entries get evicted first. The eviction algorithm is the Redis allkeys-lfu policy, with values lfu-log-factor = 0 and lfu-decay-time = 720. The decay time is half a day as people typically work in day cycles, which means that people that have not logged in for days will be evicted first. Half a day is chosen instead of a full day since a session will typically have more then one access to the cache.

9.9.1. Configuration example


9.9.2. Setting and getting values

from safeguard.sessions.plugin import PluginConfiguration
from safeguard.sessions.plugin.memory_cache import MemoryCache

class Plugin:
    def __init__(self, configuration):
        self.__config = PluginConfiguration(configuration)
        self.__mem_cache = MemoryCache.from_config(self.__config)

        cached_value = self.__mem_cache.get('foo')

        if not cached_value:
            # create the cached_value and write
            cached_value = {'bar': 42'}
            self.__mem_cache.set('foo', cached_value)

        # use the cached_value
class safeguard.sessions.plugin.memory_cache.MemoryCache(memory_cache, prefix, ttl)

The MemoryCache represents access to the memory cache running on SPS.

Do not instantiate this class via its constructor, use the from_config() method instead.

classmethod from_config(plugin_configuration, section='memory-cache', prefix='', ttl=0)

The from_config() method can be used to initialize the memory cache service.

  • plugin_configuration (PluginConfiguration) – plugin configuration object

  • section (str) – name of the section where the memory cache parameters are stored

  • prefix (str) – unique prefix for this cache to avoid conflict with other users, should be a colon (:) separated string, for example oneidentity:starling

  • ttl (int) – time to live in seconds, 0 meaning the entry never expires


object representing the service

Return type


set(key, value, ttl=None)

The set() method writes a key-value to the cache, overwriting any previous value. Note: do not write the None value directly as the get() method returns None for a cache miss.

  • key (str) – will be used as key, prefix with the prefix set in the configuration

  • value – any Python data that can be encoded to JSON

  • ttl (int) – time to live in seconds, 0 meaning the entry never expires. None means use the configured default


The get() method reads a key-value from the cache, returning None if the key does not exist.


key – will be used as key, prefix with the prefix set in the configuration


JSON decoded value