LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
fix:singleton class cache-service (#84)
Kirtiman Mishra63134eedcfb2
diff --git a/backend/account/authentication_controller.py b/backend/account/authentication_controller.pyindex bc13270d1..46037baea 100644--- a/backend/account/authentication_controller.py+++ b/backend/account/authentication_controller.py@@ -5,7 +5,6 @@ from urllib.parse import urlencodefrom account.authentication_helper import AuthenticationHelperfrom account.authentication_plugin_registry import AuthenticationPluginRegistryfrom account.authentication_service import AuthenticationService-from account.cache_service import CacheServicefrom account.constants import (AuthorizationErrorCode,Cookie,@@ -47,6 +46,7 @@ from rest_framework.request import Requestfrom rest_framework.response import Responsefrom tenant_account.models import OrganizationMember as OrganizationMemberfrom tenant_account.organization_member_service import OrganizationMemberService+from utils.cache_service import CacheServiceLogger = logging.getLogger(__name__)diff --git a/backend/account/authentication_service.py b/backend/account/authentication_service.pyindex 6056d21da..b60521ef9 100644--- a/backend/account/authentication_service.py+++ b/backend/account/authentication_service.py@@ -3,7 +3,6 @@ import uuidfrom typing import Any, Optionalfrom account.authentication_helper import AuthenticationHelper-from account.cache_service import CacheServicefrom account.constants import Common, DefaultOrgfrom account.custom_exceptions import Forbidden, MethodNotImplementedfrom account.dto import (@@ -23,6 +22,7 @@ from django.http import HttpRequestfrom rest_framework.request import Requestfrom rest_framework.response import Responsefrom tenant_account.models import OrganizationMember as OrganizationMember+from utils.cache_service import CacheServiceLogger = logging.getLogger(__name__)diff --git a/backend/account/custom_auth_middleware.py b/backend/account/custom_auth_middleware.pyindex d68e82548..24736b198 100644--- a/backend/account/custom_auth_middleware.py+++ b/backend/account/custom_auth_middleware.py@@ -2,17 +2,18 @@ from typing import Optionalfrom account.authentication_plugin_registry import AuthenticationPluginRegistryfrom account.authentication_service import AuthenticationService-from account.cache_service import CacheServicefrom account.constants import Common, Cookie, DefaultOrgfrom account.dto import UserSessionInfofrom account.models import Userfrom account.user import UserService-from backend.constants import RequestHeaderfrom django.conf import settingsfrom django.core.cache import cachefrom django.db import connectionfrom django.http import HttpRequest, HttpResponse, JsonResponsefrom tenant_account.organization_member_service import OrganizationMemberService+from utils.cache_service import CacheService++from backend.constants import RequestHeaderclass CustomAuthMiddleware:diff --git a/backend/backend/settings/base.py b/backend/backend/settings/base.pyindex 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 pathWHITELISTED_PATHS.append(f"/{API_DEPLOYMENT_PATH_PREFIX}")diff --git a/backend/sample.env b/backend/sample.envindex 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 detailsENCRYPTION_KEY="Sample-Key"++# Cache TTL+CACHE_TTL_SEC=10800diff --git a/backend/account/cache_service.py b/backend/utils/cache_service.pysimilarity index 88%rename from backend/account/cache_service.pyrename to backend/utils/cache_service.pyindex 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 redisfrom account.custom_cache import CustomCachefrom account.dto import UserSessionInfofrom django.conf import settings@@ -8,27 +7,26 @@ from django.core.cache import cacheclass 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)+@staticmethoddef set_cookie(cookie: str, token: dict[str, Any]) -> None:cache.set(cookie, token)@@ -126,6 +124,7 @@ class CacheService:# KEY_FUNCTION for cache settingsdef 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.pyindex cef6de655..e0568b8da 100644--- a/backend/workflow_manager/workflow/workflow_helper.py+++ b/backend/workflow_manager/workflow/workflow_helper.py@@ -5,24 +5,22 @@ import tracebackimport uuidfrom typing import Any, Optional-from account.cache_service import CacheServicefrom account.models import Organizationfrom api.models import APIDeploymentfrom celery import current_taskfrom celery import exceptions as celery_exceptionsfrom celery import shared_taskfrom celery.result import AsyncResult-from django.conf import settingsfrom django.db import IntegrityError, connectionfrom django_tenants.utils import get_tenant_model, tenant_contextfrom pipeline.models import Pipeline-from redis import StrictRedisfrom rest_framework import serializersfrom tool_instance.constants import ToolInstanceKeyfrom tool_instance.models import ToolInstancefrom tool_instance.tool_instance_helper import ToolInstanceHelperfrom unstract.workflow_execution.enums import LogComponent, LogStatefrom unstract.workflow_execution.exceptions import StopExecution+from utils.cache_service import CacheServicefrom workflow_manager.endpoint.destination import DestinationConnectorfrom workflow_manager.endpoint.source import SourceConnectorfrom 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.INSTANTexecution_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:@staticmethoddef 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_SUCCESSresponse["status"] = 200return response