Singleton TimeCache

Introduction

This class is a singleton class that stores data to memory. The data is stored with a time to live (TTL) value.

This class is more of a dynamic singleton class. When the same parameter is passed to the constructor, the same instance is returned. If the parameter is different, a new instance is created. Custom key value pairs can be passed to the constructor to create a new instance. Custom key value pairs must be hashable.

This class is functionally the same as the instance TimeCache with the exception that it is a singleton class.

New in version 0.52.0.

Examples

This example creates an instance of the TimeCache class with a TTL of 2 seconds.

import time
from ooodev.utils.cache.singleton import TimeCache

cache = TimeCache(seconds=2, cleanup_interval=1)  # 60 seconds
cache["key"] = "value"
assert "key" in cache # True
assert cache["key"] == "value"

time.sleep(1)
assert "key" in cache # True

time.sleep(3)
assert "key" not in cache # True

This example demonstrates the use of custom key value pairs to create a new singleton instance of the TimeCache class.

from ooodev.utils.cache.singleton import TimeCache

cache = TimeCache(seconds=300, cleanup_interval=60, custom1="custom1", custom2="custom2")

cache["key1"] = "value1"
cache["key2"] = "value2"
cache["key3"] = "value3"

print(cache["key1"]) # prints "value1"
print(cache["key2"]) # prints "value2"
print(cache["key3"]) # prints "value3"

cache2 = TimeCache()
assert cache not is cache2 # True

cache3 = TimeCache(seconds=300, cleanup_interval=60, custom1="custom1", custom2="custom2")
assert cache is cache3 # True
print(cache3["key1"]) # prints "value1"

Class

class ooodev.utils.cache.singleton.TimeCache(*, seconds=300.0, cleanup_interval=60.0, **kwargs)[source]

Time based singleton Cache.

Cached items expire after a specified time. If cleanup_interval is set, then the cache is cleaned up at regular intervals; Otherwise, the cache is only cleaned up when an item is accessed.

Each time an element is accessed, the timestamp is updated. If the element has expired, it is removed from the cache.

When an item expires, the event cache_items_expired is triggered. This event is called on a separate thread. for this reason it is important to make sure that the event handler is thread safe. See Also Singleton TimeCache

__init__(*, seconds=300.0, cleanup_interval=60.0, **kwargs)[source]

Time based Cache.

Parameters:
  • seconds (float, optional) – Cache expiration time in seconds. Defaults to 300.0.

  • cleanup_interval (float, optional) – Cache cleanup interval in seconds. If set to 0 then the cleanup is disabled. Defaults to 60.0.

  • kwargs (Any) – Additional keyword arguments. The arguments are used to create a unique instance of the singleton class.

Return type:

None