mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-07 08:26:10 +00:00
fix: cache cooldown key (#16954)
There's no need to generate a key multiple times for the same model, cache it with a max limit.
This commit is contained in:
parent
6881594632
commit
f542011076
1 changed files with 5 additions and 3 deletions
|
|
@ -2,6 +2,7 @@
|
|||
Wrapper around router cache. Meant to handle model cooldown logic
|
||||
"""
|
||||
|
||||
import functools
|
||||
import time
|
||||
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union
|
||||
|
||||
|
|
@ -44,7 +45,7 @@ class CooldownCache:
|
|||
) -> Tuple[str, CooldownCacheValue]:
|
||||
try:
|
||||
current_time = time.time()
|
||||
cooldown_key = f"deployment:{model_id}:cooldown"
|
||||
cooldown_key = CooldownCache.get_cooldown_cache_key(model_id)
|
||||
|
||||
# Store the cooldown information for the deployment separately
|
||||
cooldown_data = CooldownCacheValue(
|
||||
|
|
@ -104,8 +105,9 @@ class CooldownCache:
|
|||
raise e
|
||||
|
||||
@staticmethod
|
||||
@functools.lru_cache(maxsize=1024)
|
||||
def get_cooldown_cache_key(model_id: str) -> str:
|
||||
return f"deployment:{model_id}:cooldown"
|
||||
return "deployment:" + model_id + ":cooldown"
|
||||
|
||||
async def async_get_active_cooldowns(
|
||||
self, model_ids: List[str], parent_otel_span: Optional[Span]
|
||||
|
|
@ -140,7 +142,7 @@ class CooldownCache:
|
|||
self, model_ids: List[str], parent_otel_span: Optional[Span]
|
||||
) -> List[Tuple[str, CooldownCacheValue]]:
|
||||
# Generate the keys for the deployments
|
||||
keys = [f"deployment:{model_id}:cooldown" for model_id in model_ids]
|
||||
keys = [CooldownCache.get_cooldown_cache_key(model_id) for model_id in model_ids]
|
||||
# Retrieve the values for the keys using mget
|
||||
results = (
|
||||
self.cache.batch_get_cache(keys=keys, parent_otel_span=parent_otel_span)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue