Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions litellm/caching/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import litellm
from litellm._logging import verbose_logger
from litellm.litellm_core_utils.core_helpers import _get_parent_otel_span_from_kwargs
from litellm.types.caching import *
from litellm.types.services import ServiceLoggerPayload, ServiceTypes
from litellm.types.utils import CachingSupportedCallTypes, all_litellm_params
from litellm.types.utils import all_litellm_params


def print_verbose(print_statement):
Expand Down Expand Up @@ -2125,9 +2126,7 @@ async def async_delete_cache(self, key: str):
class Cache:
def __init__(
self,
type: Optional[
Literal["local", "redis", "redis-semantic", "s3", "disk", "qdrant-semantic"]
] = "local",
type: Optional[LiteLLMCacheType] = LiteLLMCacheType.LOCAL,
mode: Optional[
CacheMode
] = CacheMode.default_on, # when default_on cache is always on, when default_off cache is opt in
Expand Down Expand Up @@ -2221,7 +2220,7 @@ def __init__(
Returns:
None. Cache is set as a litellm param
"""
if type == "redis":
if type == LiteLLMCacheType.REDIS:
self.cache: BaseCache = RedisCache(
host=host,
port=port,
Expand All @@ -2230,7 +2229,7 @@ def __init__(
startup_nodes=redis_startup_nodes,
**kwargs,
)
elif type == "redis-semantic":
elif type == LiteLLMCacheType.REDIS_SEMANTIC:
self.cache = RedisSemanticCache(
host=host,
port=port,
Expand All @@ -2240,7 +2239,7 @@ def __init__(
embedding_model=redis_semantic_cache_embedding_model,
**kwargs,
)
elif type == "qdrant-semantic":
elif type == LiteLLMCacheType.QDRANT_SEMANTIC:
self.cache = QdrantSemanticCache(
qdrant_api_base=qdrant_api_base,
qdrant_api_key=qdrant_api_key,
Expand All @@ -2249,9 +2248,9 @@ def __init__(
quantization_config=qdrant_quantization_config,
embedding_model=qdrant_semantic_cache_embedding_model,
)
elif type == "local":
elif type == LiteLLMCacheType.LOCAL:
self.cache = InMemoryCache()
elif type == "s3":
elif type == LiteLLMCacheType.S3:
self.cache = S3Cache(
s3_bucket_name=s3_bucket_name,
s3_region_name=s3_region_name,
Expand All @@ -2266,7 +2265,7 @@ def __init__(
s3_path=s3_path,
**kwargs,
)
elif type == "disk":
elif type == LiteLLMCacheType.DISK:
self.cache = DiskCache(disk_cache_dir=disk_cache_dir)
if "cache" not in litellm.input_callback:
litellm.input_callback.append("cache")
Expand All @@ -2281,11 +2280,12 @@ def __init__(
self.ttl = ttl
self.mode: CacheMode = mode or CacheMode.default_on

if self.type == "local" and default_in_memory_ttl is not None:
if self.type == LiteLLMCacheType.LOCAL and default_in_memory_ttl is not None:
self.ttl = default_in_memory_ttl

if (
self.type == "redis" or self.type == "redis-semantic"
self.type == LiteLLMCacheType.REDIS
or self.type == LiteLLMCacheType.REDIS_SEMANTIC
) and default_in_redis_ttl is not None:
self.ttl = default_in_redis_ttl

Expand Down Expand Up @@ -2694,6 +2694,17 @@ async def disconnect(self):
if hasattr(self.cache, "disconnect"):
await self.cache.disconnect()

def _supports_async(self) -> bool:
"""
Internal method to check if the cache type supports async get/set operations

Only S3 Cache Does NOT support async operations

"""
if self.type and self.type == LiteLLMCacheType.S3:
return False
return True


class DiskCache(BaseCache):
def __init__(self, disk_cache_dir: Optional[str] = None):
Expand Down Expand Up @@ -2774,7 +2785,7 @@ def delete_cache(self, key):


def enable_cache(
type: Optional[Literal["local", "redis", "s3", "disk"]] = "local",
type: Optional[LiteLLMCacheType] = LiteLLMCacheType.LOCAL,
host: Optional[str] = None,
port: Optional[str] = None,
password: Optional[str] = None,
Expand Down Expand Up @@ -2832,7 +2843,7 @@ def enable_cache(


def update_cache(
type: Optional[Literal["local", "redis", "s3", "disk"]] = "local",
type: Optional[LiteLLMCacheType] = LiteLLMCacheType.LOCAL,
host: Optional[str] = None,
port: Optional[str] = None,
password: Optional[str] = None,
Expand Down
Loading