embedding_cache.py 1.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

import hashlib


class EmbeddingCache:
    def __init__(self):
        # Initialize an empty dictionary to store key-value pairs
        self.cache = {}

12
13
    @classmethod
    def generate_hash_key(cls, *args):
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
        """
        Generate a hashable key based on the provided arguments.

        Args:
            *args: A variable number of arguments to generate the key.

        Returns:
            A string representing the hashable key.
        """
        key = hashlib.sha256()
        for arg in args:
            key.update(str(arg).encode("utf-8"))
        return key.hexdigest()

    def has_key(self, key):
        """
        Check if a key exists in the cache.

        Args:
            key: The key to check.

        Returns:
            True if the key exists in the cache, False otherwise.
        """
        return key in self.cache

    def set(self, key, value):
        """
        Store a key-value pair in the cache.

        Args:
            key: The key to store the value under.
            value: The value to store, expected to be a tuple.
        """
        self.cache[key] = value

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

        Args:
            key: The key to look up.

        Returns:
            The value (tuple) associated with the key, or None if the key is not found.
        """
        return self.cache.get(key)