Spaces:
Running
Running
File size: 2,702 Bytes
8196089 | 1 2 3 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 | # SPDX-License-Identifier: Apache-2.0
"""Bounded in-process payload cache. It is not a durable or private memory store."""
from __future__ import annotations
from collections import OrderedDict
from threading import RLock
from typing import Any
class EmbeddingCache:
"""LRU with explicit tensor-payload accounting, not total-process accounting.
Callers must key by corpus/model/content identity. This cache is only used
with the exact public corpus; it provides no private-data ACL or revocation.
Returned tensors are used read-only by the owning retrieval coordinator.
"""
def __init__(self, max_bytes: int = 256 * 1024 * 1024) -> None:
if type(max_bytes) is not int or not 0 <= max_bytes <= 512 * 1024 * 1024:
raise ValueError("cache budget must be an integer from zero to 512 MiB")
self._max_bytes = max_bytes
self._entries: OrderedDict[tuple[str, ...], tuple[Any, int]] = OrderedDict()
self._bytes = self._hits = self._misses = self._evictions = 0
self._lock = RLock()
@property
def max_bytes(self) -> int:
return self._max_bytes
def get(self, key: tuple[str, ...]):
with self._lock:
entry = self._entries.pop(key, None)
if entry is None:
self._misses += 1
return None
self._entries[key] = entry
self._hits += 1
return entry[0]
def put(self, key: tuple[str, ...], value: Any, *, byte_size: int) -> bool:
if (not isinstance(key, tuple) or not key or len(key) > 8
or any(not isinstance(part, str) or not part or len(part) > 1024 for part in key)
or type(byte_size) is not int or byte_size <= 0 or value is None):
raise ValueError("invalid cache entry")
with self._lock:
old = self._entries.pop(key, None)
if old is not None:
self._bytes -= old[1]
if byte_size > self.max_bytes:
return False
while self._bytes + byte_size > self.max_bytes:
_, (_, size) = self._entries.popitem(last=False)
self._bytes -= size
self._evictions += 1
self._entries[key] = (value, byte_size)
self._bytes += byte_size
return True
def stats(self) -> dict[str, int | str]:
with self._lock:
return {"scope": "PUBLIC_DOCUMENT_TENSOR_PAYLOAD_ONLY",
"max_bytes": self.max_bytes, "payload_bytes": self._bytes,
"entries": len(self._entries), "hits": self._hits,
"misses": self._misses, "evictions": self._evictions}
|