MCPcopy Create free account
hub / github.com/BerriAI/litellm / UserApiKeyCache

Class UserApiKeyCache

litellm/proxy/common_utils/user_api_key_cache.py:50–477  ·  view source on GitHub ↗

DualCache for UserAPIKeyAuth-like payloads. Stores a Redis-safe JSON payload in memory and, with ``enable_redis_auth_cache``, in Redis, to avoid "memory returns BaseModel, Redis returns dict" format drift. When ``model_type`` is provided: - writes are serialized via ``CacheCod

Source from the content-addressed store, hash-verified

48
49
50class UserApiKeyCache(DualCache):
51 """
52 DualCache for UserAPIKeyAuth-like payloads.
53
54 Stores a Redis-safe JSON payload in memory and, with ``enable_redis_auth_cache``, in Redis, to avoid
55 "memory returns BaseModel, Redis returns dict" format drift.
56
57 When ``model_type`` is provided:
58 - writes are serialized via ``CacheCodec.serialize(..., model_type=...)``
59 - reads are deserialized via ``CacheCodec.deserialize(..., model_type)``
60 and return ``Optional[T]``: the model on success, ``None`` on cache miss
61 **or** if the cached payload fails validation (schema drift). On
62 validation failure after a cache hit, an error line is emitted via
63 ``verbose_proxy_logger``.
64
65 When ``model_type`` is omitted, the interface behaves like ``DualCache``:
66 raw cached payload is returned (dict/str/etc.).
67
68 User-key objects (see ``is_user_key_cache_key``) live in their own in-memory partition,
69 ``key_object_cache``, so churn in the other management objects cannot evict them.
70
71 Without Redis both partitions are this class's Python memory. ``attach_redis_cache`` moves the whole
72 keyspace, both memory partitions included, onto the native ``AuthCacheStore`` (``shared``); redis-py never
73 reads or writes it, and ``redis_cache`` stays None.
74
75 ``get_cache`` / ``async_get_cache`` overloads and implementations must be contiguous
76 (no other methods in between) so mypy resolves ``@overload`` + implementation correctly.
77 """
78
79 def __init__(
80 self,
81 in_memory_cache: InMemoryCache | None = None,
82 redis_cache: RedisCache | None = None,
83 default_in_memory_ttl: float | None = None,
84 default_redis_ttl: float | None = None,
85 key_object_in_memory_cache: InMemoryCache | None = None,
86 budget_windows_absent_in_memory_cache: InMemoryCache | None = None,
87 auth_cache: AuthCacheFactory = _connected_auth_cache,
88 ) -> None:
89 super().__init__(
90 in_memory_cache=in_memory_cache,
91 default_in_memory_ttl=default_in_memory_ttl,
92 default_redis_ttl=default_redis_ttl,
93 )
94 self.key_object_cache: Final = DualCache(
95 in_memory_cache=key_object_in_memory_cache or InMemoryCache(),
96 default_in_memory_ttl=default_in_memory_ttl,
97 default_redis_ttl=default_redis_ttl,
98 )
99 self.budget_windows_absent_cache: Final = DualCache(
100 in_memory_cache=budget_windows_absent_in_memory_cache
101 or InMemoryCache(default_ttl=DEFAULT_MANAGEMENT_OBJECT_IN_MEMORY_CACHE_TTL),
102 default_in_memory_ttl=default_in_memory_ttl,
103 )
104 self._auth_cache: Final = auth_cache
105 self._redis_connection: RedisCache | None = None
106 self._shared: AuthCacheStore | None = None
107 self.attach_redis_cache(redis_cache, default_redis_ttl=default_redis_ttl)

Calls

no outgoing calls