Cache

The Cache class is a component in the llamarch library that provides a simple interface to work with caches, in particular with Redis.

Cache

A simple cache class using Redis as the backend.

Parameters:
  • host (str, default: 'localhost' ) –

    The hostname of the Redis server (default is "localhost").

  • port (int, default: 6379 ) –

    The port number on which the Redis server is running (default is 6379).

  • db (int, default: 0 ) –

    The Redis database number to use (default is 0).

Attributes:
  • client (Redis) –

    The Redis client instance used to interact with the Redis server.

Source code in llamarch/common/cache.py
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 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
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
class Cache:
    """
    A simple cache class using Redis as the backend.

    Parameters
    ----------
    host : str, optional
        The hostname of the Redis server (default is "localhost").
    port : int, optional
        The port number on which the Redis server is running (default is 6379).
    db : int, optional
        The Redis database number to use (default is 0).

    Attributes
    ----------
    client : redis.Redis
        The Redis client instance used to interact with the Redis server.
    """

    def __init__(self, host="localhost", port=6379, db=0):
        self.client = redis.Redis(host=host, port=port, db=db)

    def set(self, key: str, value: str, expiration: int = None):
        """
        Set a key-value pair in the cache with an optional expiration.

        Parameters
        ----------
        key : str
            The key under which the value will be stored.
        value : str
            The value to store in the cache.
        expiration : int, optional
            The time-to-live (TTL) for the key in seconds. If not provided, the key will persist indefinitely.

        Returns
        -------
        bool
            Returns True if the operation is successful, False otherwise.
        """
        return self.client.set(key, value, ex=expiration)

    def get(self, key: str):
        """
        Retrieve the value associated with a key from the cache.

        Parameters
        ----------
        key : str
            The key to retrieve the value for.

        Returns
        -------
        str or None
            The value associated with the key, or None if the key does not exist.
        """
        value = self.client.get(key)
        return value.decode('utf-8') if value else None

    def delete(self, key: str) -> bool:
        """
        Delete a key-value pair from the cache.

        Parameters
        ----------
        key : str
            The key to delete.

        Returns
        -------
        bool
            Returns True if the key was deleted, False if the key does not exist.
        """
        return self.client.delete(key) > 0

    def exists(self, key: str) -> bool:
        """
        Check if a key exists in the cache.

        Parameters
        ----------
        key : str
            The key to check for existence.

        Returns
        -------
        bool
            Returns True if the key exists, False otherwise.
        """
        return self.client.exists(key) == 1

    def get_all_values(self) -> list:
        """
        Retrieve all values stored in the cache.

        Returns
        -------
        list of str
            A list of all values currently stored in the cache as strings.
        """
        keys = self.client.keys('*')  # Get all keys
        return [self.client.get(key).decode('utf-8') for key in keys if self.client.exists(key)]

set(key, value, expiration=None)

Set a key-value pair in the cache with an optional expiration.

Parameters:
  • key (str) –

    The key under which the value will be stored.

  • value (str) –

    The value to store in the cache.

  • expiration (int, default: None ) –

    The time-to-live (TTL) for the key in seconds. If not provided, the key will persist indefinitely.

Returns:
  • bool

    Returns True if the operation is successful, False otherwise.

Source code in llamarch/common/cache.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def set(self, key: str, value: str, expiration: int = None):
    """
    Set a key-value pair in the cache with an optional expiration.

    Parameters
    ----------
    key : str
        The key under which the value will be stored.
    value : str
        The value to store in the cache.
    expiration : int, optional
        The time-to-live (TTL) for the key in seconds. If not provided, the key will persist indefinitely.

    Returns
    -------
    bool
        Returns True if the operation is successful, False otherwise.
    """
    return self.client.set(key, value, ex=expiration)

get(key)

Retrieve the value associated with a key from the cache.

Parameters:
  • key (str) –

    The key to retrieve the value for.

Returns:
  • str or None

    The value associated with the key, or None if the key does not exist.

Source code in llamarch/common/cache.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def get(self, key: str):
    """
    Retrieve the value associated with a key from the cache.

    Parameters
    ----------
    key : str
        The key to retrieve the value for.

    Returns
    -------
    str or None
        The value associated with the key, or None if the key does not exist.
    """
    value = self.client.get(key)
    return value.decode('utf-8') if value else None

delete(key)

Delete a key-value pair from the cache.

Parameters:
  • key (str) –

    The key to delete.

Returns:
  • bool

    Returns True if the key was deleted, False if the key does not exist.

Source code in llamarch/common/cache.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def delete(self, key: str) -> bool:
    """
    Delete a key-value pair from the cache.

    Parameters
    ----------
    key : str
        The key to delete.

    Returns
    -------
    bool
        Returns True if the key was deleted, False if the key does not exist.
    """
    return self.client.delete(key) > 0

exists(key)

Check if a key exists in the cache.

Parameters:
  • key (str) –

    The key to check for existence.

Returns:
  • bool

    Returns True if the key exists, False otherwise.

Source code in llamarch/common/cache.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def exists(self, key: str) -> bool:
    """
    Check if a key exists in the cache.

    Parameters
    ----------
    key : str
        The key to check for existence.

    Returns
    -------
    bool
        Returns True if the key exists, False otherwise.
    """
    return self.client.exists(key) == 1

get_all_values()

Retrieve all values stored in the cache.

Returns:
  • list of str

    A list of all values currently stored in the cache as strings.

Source code in llamarch/common/cache.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
def get_all_values(self) -> list:
    """
    Retrieve all values stored in the cache.

    Returns
    -------
    list of str
        A list of all values currently stored in the cache as strings.
    """
    keys = self.client.keys('*')  # Get all keys
    return [self.client.get(key).decode('utf-8') for key in keys if self.client.exists(key)]