Skip to content

base

BaseCache

BaseCache is an abstract base class that defines the interface for a cache.

Source code in inference/core/cache/base.py
  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class BaseCache:
    """
    BaseCache is an abstract base class that defines the interface for a cache.
    """

    def get(self, key: str):
        """
        Gets the value associated with the given key.

        Args:
            key (str): The key to retrieve the value.

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

    def set(self, key: str, value: str, expire: float = None):
        """
        Sets a value for a given key with an optional expire time.

        Args:
            key (str): The key to store the value.
            value (str): The value to store.
            expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

    def zadd(self, key: str, value: str, score: float, expire: float = None):
        """
        Adds a member with the specified score to the sorted set stored at key.

        Args:
            key (str): The key of the sorted set.
            value (str): The value to add to the sorted set.
            score (float): The score associated with the value.
            expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

    def zrangebyscore(
        self,
        key: str,
        min: Optional[float] = -1,
        max: Optional[float] = float("inf"),
        withscores: bool = False,
    ):
        """
        Retrieves a range of members from a sorted set.

        Args:
            key (str): The key of the sorted set.
            start (int, optional): The starting index of the range. Defaults to -1.
            stop (int, optional): The ending index of the range. Defaults to float("inf").
            withscores (bool, optional): Whether to return the scores along with the values. Defaults to False.

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

    def zremrangebyscore(
        self,
        key: str,
        start: Optional[int] = -1,
        stop: Optional[int] = float("inf"),
    ):
        """
        Removes all members in a sorted set within the given scores.

        Args:
            key (str): The key of the sorted set.
            start (int, optional): The minimum score of the range. Defaults to -1.
            stop (int, optional): The maximum score of the range. Defaults to float("inf").

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

    def acquire_lock(self, key: str, expire: float = None) -> Any:
        raise NotImplementedError()

    @contextmanager
    def lock(self, key: str, expire: float = None) -> Any:
        logger.debug(f"Acquiring lock at cache key: {key}")
        l = self.acquire_lock(key, expire=expire)
        try:
            yield l
        finally:
            logger.debug(f"Releasing lock at cache key: {key}")
            l.release()

    def set_numpy(self, key: str, value: Any, expire: float = None):
        """
        Caches a numpy array.

        Args:
            key (str): The key to store the value.
            value (Any): The value to store.
            expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

    def get_numpy(self, key: str) -> Any:
        """
        Retrieves a numpy array from the cache.

        Args:
            key (str): The key of the value to retrieve.

        Raises:
            NotImplementedError: This method must be implemented by subclasses.
        """
        raise NotImplementedError()

get(key)

Gets the value associated with the given key.

Parameters:

Name Type Description Default
key str

The key to retrieve the value.

required

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
12
13
14
15
16
17
18
19
20
21
22
def get(self, key: str):
    """
    Gets the value associated with the given key.

    Args:
        key (str): The key to retrieve the value.

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()

get_numpy(key)

Retrieves a numpy array from the cache.

Parameters:

Name Type Description Default
key str

The key of the value to retrieve.

required

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
120
121
122
123
124
125
126
127
128
129
130
def get_numpy(self, key: str) -> Any:
    """
    Retrieves a numpy array from the cache.

    Args:
        key (str): The key of the value to retrieve.

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()

set(key, value, expire=None)

Sets a value for a given key with an optional expire time.

Parameters:

Name Type Description Default
key str

The key to store the value.

required
value str

The value to store.

required
expire float

The time, in seconds, after which the key will expire. Defaults to None.

None

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
24
25
26
27
28
29
30
31
32
33
34
35
36
def set(self, key: str, value: str, expire: float = None):
    """
    Sets a value for a given key with an optional expire time.

    Args:
        key (str): The key to store the value.
        value (str): The value to store.
        expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()

set_numpy(key, value, expire=None)

Caches a numpy array.

Parameters:

Name Type Description Default
key str

The key to store the value.

required
value Any

The value to store.

required
expire float

The time, in seconds, after which the key will expire. Defaults to None.

None

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
106
107
108
109
110
111
112
113
114
115
116
117
118
def set_numpy(self, key: str, value: Any, expire: float = None):
    """
    Caches a numpy array.

    Args:
        key (str): The key to store the value.
        value (Any): The value to store.
        expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()

zadd(key, value, score, expire=None)

Adds a member with the specified score to the sorted set stored at key.

Parameters:

Name Type Description Default
key str

The key of the sorted set.

required
value str

The value to add to the sorted set.

required
score float

The score associated with the value.

required
expire float

The time, in seconds, after which the key will expire. Defaults to None.

None

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def zadd(self, key: str, value: str, score: float, expire: float = None):
    """
    Adds a member with the specified score to the sorted set stored at key.

    Args:
        key (str): The key of the sorted set.
        value (str): The value to add to the sorted set.
        score (float): The score associated with the value.
        expire (float, optional): The time, in seconds, after which the key will expire. Defaults to None.

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()

zrangebyscore(key, min=-1, max=float('inf'), withscores=False)

Retrieves a range of members from a sorted set.

Parameters:

Name Type Description Default
key str

The key of the sorted set.

required
start int

The starting index of the range. Defaults to -1.

required
stop int

The ending index of the range. Defaults to float("inf").

required
withscores bool

Whether to return the scores along with the values. Defaults to False.

False

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def zrangebyscore(
    self,
    key: str,
    min: Optional[float] = -1,
    max: Optional[float] = float("inf"),
    withscores: bool = False,
):
    """
    Retrieves a range of members from a sorted set.

    Args:
        key (str): The key of the sorted set.
        start (int, optional): The starting index of the range. Defaults to -1.
        stop (int, optional): The ending index of the range. Defaults to float("inf").
        withscores (bool, optional): Whether to return the scores along with the values. Defaults to False.

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()

zremrangebyscore(key, start=-1, stop=float('inf'))

Removes all members in a sorted set within the given scores.

Parameters:

Name Type Description Default
key str

The key of the sorted set.

required
start int

The minimum score of the range. Defaults to -1.

-1
stop int

The maximum score of the range. Defaults to float("inf").

float('inf')

Raises:

Type Description
NotImplementedError

This method must be implemented by subclasses.

Source code in inference/core/cache/base.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def zremrangebyscore(
    self,
    key: str,
    start: Optional[int] = -1,
    stop: Optional[int] = float("inf"),
):
    """
    Removes all members in a sorted set within the given scores.

    Args:
        key (str): The key of the sorted set.
        start (int, optional): The minimum score of the range. Defaults to -1.
        stop (int, optional): The maximum score of the range. Defaults to float("inf").

    Raises:
        NotImplementedError: This method must be implemented by subclasses.
    """
    raise NotImplementedError()