Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

fix:singleton class cache-service (#84)

Kirtiman Mishra63134eedcfb2
diff --git a/backend/account/authentication_controller.py b/backend/account/authentication_controller.py
index bc13270d1..46037baea 100644
--- a/backend/account/authentication_controller.py
+++ b/backend/account/authentication_controller.py
@@ -5,7 +5,6 @@ from urllib.parse import urlencode
from account.authentication_helper import AuthenticationHelper
from account.authentication_plugin_registry import AuthenticationPluginRegistry
from account.authentication_service import AuthenticationService
-from account.cache_service import CacheService
from account.constants import (
AuthorizationErrorCode,
Cookie,
@@ -47,6 +46,7 @@ from rest_framework.request import Request
from rest_framework.response import Response
from tenant_account.models import OrganizationMember as OrganizationMember
from tenant_account.organization_member_service import OrganizationMemberService
+from utils.cache_service import CacheService
Logger = logging.getLogger(__name__)
diff --git a/backend/account/authentication_service.py b/backend/account/authentication_service.py
index 6056d21da..b60521ef9 100644
--- a/backend/account/authentication_service.py
+++ b/backend/account/authentication_service.py
@@ -3,7 +3,6 @@ import uuid
from typing import Any, Optional
from account.authentication_helper import AuthenticationHelper
-from account.cache_service import CacheService
from account.constants import Common, DefaultOrg
from account.custom_exceptions import Forbidden, MethodNotImplemented
from account.dto import (
@@ -23,6 +22,7 @@ from django.http import HttpRequest
from rest_framework.request import Request
from rest_framework.response import Response
from tenant_account.models import OrganizationMember as OrganizationMember
+from utils.cache_service import CacheService
Logger = logging.getLogger(__name__)
diff --git a/backend/account/custom_auth_middleware.py b/backend/account/custom_auth_middleware.py
index d68e82548..24736b198 100644
--- a/backend/account/custom_auth_middleware.py
+++ b/backend/account/custom_auth_middleware.py
@@ -2,17 +2,18 @@ from typing import Optional
from account.authentication_plugin_registry import AuthenticationPluginRegistry
from account.authentication_service import AuthenticationService
-from account.cache_service import CacheService
from account.constants import Common, Cookie, DefaultOrg
from account.dto import UserSessionInfo
from account.models import User
from account.user import UserService
-from backend.constants import RequestHeader
from django.conf import settings
from django.core.cache import cache
from django.db import connection
from django.http import HttpRequest, HttpResponse, JsonResponse
from tenant_account.organization_member_service import OrganizationMemberService
+from utils.cache_service import CacheService
+
+from backend.constants import RequestHeader
class CustomAuthMiddleware:
diff --git a/backend/backend/settings/base.py b/backend/backend/settings/base.py
index 86fac8c9e..f7fcd159e 100644
--- a/backend/backend/settings/base.py
+++ b/backend/backend/settings/base.py
@@ -138,6 +138,8 @@ STRUCTURE_TOOL_IMAGE_NAME = get_required_setting("STRUCTURE_TOOL_IMAGE_NAME")
STRUCTURE_TOOL_IMAGE_TAG = get_required_setting("STRUCTURE_TOOL_IMAGE_TAG")
WORKFLOW_DATA_DIR = os.environ.get("WORKFLOW_DATA_DIR")
API_STORAGE_DIR = os.environ.get("API_STORAGE_DIR")
+CACHE_TTL_SEC = os.environ.get("CACHE_TTL_SEC", 10800)
+
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
@@ -243,7 +245,7 @@ MIDDLEWARE = [
"account.custom_auth_middleware.CustomAuthMiddleware",
"middleware.exception.ExceptionLoggingMiddleware",
"social_django.middleware.SocialAuthExceptionMiddleware",
- "middleware.remove_allow_header.RemoveAllowHeaderMiddleware"
+ "middleware.remove_allow_header.RemoveAllowHeaderMiddleware",
]
PUBLIC_SCHEMA_URLCONF = "backend.public_urls"
@@ -298,7 +300,7 @@ CACHES = {
"USERNAME": REDIS_USER,
"PASSWORD": REDIS_PASSWORD,
},
- "KEY_FUNCTION": "account.cache_service.custom_key_function",
+ "KEY_FUNCTION": "utils.cache_service.custom_key_function",
}
}
@@ -393,8 +395,7 @@ WHITELISTED_PATHS_LIST = [
"/logout",
"/signup",
]
-WHITELISTED_PATHS = [
- f"/{PATH_PREFIX}{PATH}" for PATH in WHITELISTED_PATHS_LIST]
+WHITELISTED_PATHS = [f"/{PATH_PREFIX}{PATH}" for PATH in WHITELISTED_PATHS_LIST]
# White lists workflow-api-deployment path
WHITELISTED_PATHS.append(f"/{API_DEPLOYMENT_PATH_PREFIX}")
diff --git a/backend/sample.env b/backend/sample.env
index 2494d77e4..9cb61c155 100644
--- a/backend/sample.env
+++ b/backend/sample.env
@@ -104,3 +104,6 @@ X2TEXT_PORT=3004
# Encryption Key
# Key must be 32 url-safe base64-encoded bytes. Check the README.md for details
ENCRYPTION_KEY="Sample-Key"
+
+# Cache TTL
+CACHE_TTL_SEC=10800
diff --git a/backend/account/cache_service.py b/backend/utils/cache_service.py
similarity index 88%
rename from backend/account/cache_service.py
rename to backend/utils/cache_service.py
index 48f80fd5f..6feedf970 100644
--- a/backend/account/cache_service.py
+++ b/backend/utils/cache_service.py
@@ -1,6 +1,5 @@
from typing import Any, Optional, Union
-import redis
from account.custom_cache import CustomCache
from account.dto import UserSessionInfo
from django.conf import settings
@@ -8,27 +7,26 @@ from django.core.cache import cache
class CacheService:
- def __init__(self) -> None:
- self.cache = redis.Redis(
- host=settings.REDIS_HOST,
- port=int(settings.REDIS_PORT),
- password=settings.REDIS_PASSWORD,
- username=settings.REDIS_USER,
- )
-
- def get_a_key(self, key: str) -> Optional[Any]:
- data = self.cache.get(str(key))
+ @staticmethod
+ def get_key(key: str) -> Optional[Any]:
+ data = cache.get(str(key))
if data is not None:
return data.decode("utf-8")
return data
- def set_a_key(self, key: str, value: Any) -> None:
- self.cache.set(
+ @staticmethod
+ def set_key(key: str, value: Any) -> None:
+ cache.set(
str(key),
value,
- int(settings.WORKFLOW_ACTION_EXPIRATION_TIME_IN_SECOND),
+ int(settings.CACHE_TTL_SEC),
)
+ @staticmethod
+ def clear_cache(key_pattern: str) -> Any:
+ """Delete keys in bulk based on the key pattern."""
+ cache.delete_pattern(key_pattern)
+
@staticmethod
def set_cookie(cookie: str, token: dict[str, Any]) -> None:
cache.set(cookie, token)
@@ -126,6 +124,7 @@ class CacheService:
# KEY_FUNCTION for cache settings
def custom_key_function(key: str, key_prefix: str, version: int) -> str:
+ version = int(version)
if version > 1:
return f"{key_prefix}:{version}:{key}"
if key_prefix:
diff --git a/backend/workflow_manager/workflow/workflow_helper.py b/backend/workflow_manager/workflow/workflow_helper.py
index cef6de655..e0568b8da 100644
--- a/backend/workflow_manager/workflow/workflow_helper.py
+++ b/backend/workflow_manager/workflow/workflow_helper.py
@@ -5,24 +5,22 @@ import traceback
import uuid
from typing import Any, Optional
-from account.cache_service import CacheService
from account.models import Organization
from api.models import APIDeployment
from celery import current_task
from celery import exceptions as celery_exceptions
from celery import shared_task
from celery.result import AsyncResult
-from django.conf import settings
from django.db import IntegrityError, connection
from django_tenants.utils import get_tenant_model, tenant_context
from pipeline.models import Pipeline
-from redis import StrictRedis
from rest_framework import serializers
from tool_instance.constants import ToolInstanceKey
from tool_instance.models import ToolInstance
from tool_instance.tool_instance_helper import ToolInstanceHelper
from unstract.workflow_execution.enums import LogComponent, LogState
from unstract.workflow_execution.exceptions import StopExecution
+from utils.cache_service import CacheService
from workflow_manager.endpoint.destination import DestinationConnector
from workflow_manager.endpoint.source import SourceConnector
from workflow_manager.workflow.constants import (
@@ -203,10 +201,10 @@ class WorkflowHelper:
workflow_execution: Optional[WorkflowExecution] = None,
execution_mode: Optional[tuple[str, str]] = None,
) -> ExecutionResponse:
- tool_instances: list[ToolInstance] = (
- ToolInstanceHelper.get_tool_instances_by_workflow(
- workflow.id, ToolInstanceKey.STEP
- )
+ tool_instances: list[
+ ToolInstance
+ ] = ToolInstanceHelper.get_tool_instances_by_workflow(
+ workflow.id, ToolInstanceKey.STEP
)
execution_mode = execution_mode or WorkflowExecution.Mode.INSTANT
execution_service = WorkflowHelper.build_workflow_execution_service(
@@ -536,14 +534,11 @@ class WorkflowHelper:
!= WorkflowExecution.Type.STEP
):
raise InvalidRequest(WorkflowErrors.INVALID_EXECUTION_ID)
- cache_service = CacheService()
- current_action: Optional[str] = cache_service.get_a_key(
- execution_id
- )
+ current_action: Optional[str] = CacheService.get_key(execution_id)
logger.info(f"workflow_execution.current_action {current_action}")
if current_action is None:
raise InvalidRequest(WorkflowErrors.INVALID_EXECUTION_ID)
- cache_service.set_a_key(execution_id, execution_action)
+ CacheService.set_key(execution_id, execution_action)
workflow_execution = WorkflowExecution.objects.get(pk=execution_id)
return ExecutionResponse(
@@ -583,23 +578,10 @@ class WorkflowHelper:
@staticmethod
def clear_cache(workflow_id: str) -> dict[str, Any]:
"""Function to clear cache with a specific pattern."""
-
- # TODO: Clear this section if not needed
- cache = StrictRedis(
- host=settings.REDIS_HOST,
- port=int(settings.REDIS_PORT),
- password=settings.REDIS_PASSWORD,
- username=settings.REDIS_USER,
- )
response: dict[str, Any] = {}
try:
- keys = cache.scan_iter(f"cache:{workflow_id}*")
- for key in keys:
- if cache.get(key):
- cache.delete(key)
- logger.info(f"Successfully Cleared Cache, deleted {key!r}")
- else:
- logger.info(f"No key {key!r} exists in cache")
+ key_pattern = f"*:cache:{workflow_id}:*"
+ CacheService.clear_cache(key_pattern)
response["message"] = WorkflowMessages.CACHE_CLEAR_SUCCESS
response["status"] = 200
return response