2024-03-08 20:31:13 +08:00
|
|
|
import json
|
|
|
|
from enum import Enum
|
|
|
|
from json import JSONDecodeError
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from extensions.ext_redis import redis_client
|
|
|
|
|
|
|
|
|
|
|
|
class ToolParameterCacheType(Enum):
|
|
|
|
PARAMETER = "tool_parameter"
|
|
|
|
|
2024-09-10 17:00:20 +08:00
|
|
|
|
2024-03-08 20:31:13 +08:00
|
|
|
class ToolParameterCache:
|
2024-09-10 17:00:20 +08:00
|
|
|
def __init__(
|
|
|
|
self, tenant_id: str, provider: str, tool_name: str, cache_type: ToolParameterCacheType, identity_id: str
|
|
|
|
):
|
2024-09-12 14:00:36 +08:00
|
|
|
self.cache_key = (
|
|
|
|
f"{cache_type.value}_secret:tenant_id:{tenant_id}:provider:{provider}:tool_name:{tool_name}"
|
|
|
|
f":identity_id:{identity_id}"
|
|
|
|
)
|
2024-03-08 20:31:13 +08:00
|
|
|
|
|
|
|
def get(self) -> Optional[dict]:
|
|
|
|
"""
|
|
|
|
Get cached model provider credentials.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
cached_tool_parameter = redis_client.get(self.cache_key)
|
|
|
|
if cached_tool_parameter:
|
|
|
|
try:
|
2024-09-10 17:00:20 +08:00
|
|
|
cached_tool_parameter = cached_tool_parameter.decode("utf-8")
|
2024-03-08 20:31:13 +08:00
|
|
|
cached_tool_parameter = json.loads(cached_tool_parameter)
|
|
|
|
except JSONDecodeError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return cached_tool_parameter
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set(self, parameters: dict) -> None:
|
|
|
|
"""
|
|
|
|
Cache model provider credentials.
|
|
|
|
|
|
|
|
:param credentials: provider credentials
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
redis_client.setex(self.cache_key, 86400, json.dumps(parameters))
|
|
|
|
|
|
|
|
def delete(self) -> None:
|
|
|
|
"""
|
|
|
|
Delete cached model provider credentials.
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
2024-09-10 17:00:20 +08:00
|
|
|
redis_client.delete(self.cache_key)
|