forked from phoenix/litellm-mirror
feat - delete cache key
This commit is contained in:
parent
9dc4127576
commit
c4cb0afa98
2 changed files with 56 additions and 0 deletions
|
@ -347,6 +347,12 @@ class RedisCache(BaseCache):
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
|
async def delete_cache_keys(self, keys):
|
||||||
|
_redis_client = self.init_async_client()
|
||||||
|
# keys is a list, unpack it so it gets passed as individual elements to delete
|
||||||
|
async with _redis_client as redis_client:
|
||||||
|
await redis_client.delete(*keys)
|
||||||
|
|
||||||
def client_list(self):
|
def client_list(self):
|
||||||
client_list = self.redis_client.client_list()
|
client_list = self.redis_client.client_list()
|
||||||
return client_list
|
return client_list
|
||||||
|
@ -1408,6 +1414,11 @@ class Cache:
|
||||||
return await self.cache.ping()
|
return await self.cache.ping()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
async def delete_cache_keys(self, keys):
|
||||||
|
if hasattr(self.cache, "delete_cache_keys"):
|
||||||
|
return await self.cache.delete_cache_keys(keys)
|
||||||
|
return None
|
||||||
|
|
||||||
async def disconnect(self):
|
async def disconnect(self):
|
||||||
if hasattr(self.cache, "disconnect"):
|
if hasattr(self.cache, "disconnect"):
|
||||||
await self.cache.disconnect()
|
await self.cache.disconnect()
|
||||||
|
|
|
@ -8202,6 +8202,51 @@ async def cache_ping():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/cache/delete",
|
||||||
|
tags=["caching"],
|
||||||
|
dependencies=[Depends(user_api_key_auth)],
|
||||||
|
)
|
||||||
|
async def cache_delete(request: Request):
|
||||||
|
"""
|
||||||
|
Endpoint for deleting a key from the cache. All responses from litellm proxy have `x-litellm-cache-key` in the headers
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- **keys**: *Optional[List[str]]* - A list of keys to delete from the cache. Example {"keys": ["key1", "key2"]}
|
||||||
|
|
||||||
|
```shell
|
||||||
|
curl -X POST "http://0.0.0.0:4000/cache/delete" \
|
||||||
|
-H "Authorization: Bearer sk-1234" \
|
||||||
|
-d '{"keys": ["key1", "key2"]}'
|
||||||
|
```
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if litellm.cache is None:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=503, detail="Cache not initialized. litellm.cache is None"
|
||||||
|
)
|
||||||
|
|
||||||
|
request_data = await request.json()
|
||||||
|
keys = request_data.get("keys", None)
|
||||||
|
|
||||||
|
if litellm.cache.type == "redis":
|
||||||
|
await litellm.cache.delete_cache_keys(keys=keys)
|
||||||
|
return {
|
||||||
|
"status": "success",
|
||||||
|
}
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500,
|
||||||
|
detail=f"Cache type {litellm.cache.type} does not support deleting a key. only `redis` is supported",
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=500,
|
||||||
|
detail=f"Cache Delete Failed({str(e)})",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
"/cache/redis/info",
|
"/cache/redis/info",
|
||||||
tags=["caching"],
|
tags=["caching"],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue