Skip to content

Cache

A caching class that uses cachetools for TTL caching with an LRU eviction policy.

Source code in yeastdnnexplorer/interface/Cache.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Cache:
    """A caching class that uses cachetools for TTL caching with an LRU eviction
    policy."""

    def __init__(self, maxsize: int = 100, ttl: int = 300):
        self.ttl_cache = TTLCache(maxsize=maxsize, ttl=ttl)
        self.logger = logging.getLogger(__name__)

    def get(self, key: str, default: Any = None) -> Any:
        """Get a value from the cache."""
        return self.ttl_cache.get(key, default)

    def set(self, key: str, value: Any) -> None:
        """Set a value in the cache."""
        self.ttl_cache[key] = value

    def list(self) -> list[str]:
        """List all keys in the cache."""
        return list(self.ttl_cache.keys())

    def delete(self, key: str) -> None:
        """Delete a key from the cache."""
        self.ttl_cache.pop(key, None)

delete(key)

Delete a key from the cache.

Source code in yeastdnnexplorer/interface/Cache.py
27
28
29
def delete(self, key: str) -> None:
    """Delete a key from the cache."""
    self.ttl_cache.pop(key, None)

get(key, default=None)

Get a value from the cache.

Source code in yeastdnnexplorer/interface/Cache.py
15
16
17
def get(self, key: str, default: Any = None) -> Any:
    """Get a value from the cache."""
    return self.ttl_cache.get(key, default)

list()

List all keys in the cache.

Source code in yeastdnnexplorer/interface/Cache.py
23
24
25
def list(self) -> list[str]:
    """List all keys in the cache."""
    return list(self.ttl_cache.keys())

set(key, value)

Set a value in the cache.

Source code in yeastdnnexplorer/interface/Cache.py
19
20
21
def set(self, key: str, value: Any) -> None:
    """Set a value in the cache."""
    self.ttl_cache[key] = value