diff --git a/apps/application/serializers/application_serializers.py b/apps/application/serializers/application_serializers.py index 503313ec1..790bc25eb 100644 --- a/apps/application/serializers/application_serializers.py +++ b/apps/application/serializers/application_serializers.py @@ -26,15 +26,14 @@ from rest_framework import serializers from application.flow.workflow_manage import Flow from application.models import Application, ApplicationDatasetMapping, ApplicationTypeChoices, WorkFlowVersion from application.models.api_key_model import ApplicationAccessToken, ApplicationApiKey +from common.cache_data.application_access_token_cache import get_application_access_token, del_application_access_token +from common.cache_data.application_api_key_cache import del_application_api_key, get_application_api_key from common.config.embedding_config import VectorStore from common.constants.authentication_type import AuthenticationType -from common.constants.cache_code_constants import CacheCodeConstants from common.db.search import get_dynamics_model, native_search, native_page_search from common.db.sql_execute import select_list from common.exception.app_exception import AppApiException, NotFound404, AppUnauthorizedFailed from common.field.common import UploadedImageField -from common.middleware.cross_domain_middleware import get_application_api_key -from common.middleware.static_headers_middleware import get_application_access_token from common.models.db_model_manage import DBModelManage from common.util.common import valid_license from common.util.field_message import ErrMessage @@ -253,8 +252,7 @@ class ApplicationSerializer(serializers.Serializer): if 'is_active' in instance: application_access_token.is_active = instance.get("is_active") if 'access_token_reset' in instance and instance.get('access_token_reset'): - cache.cache.delete(application_access_token.access_token, - version=CacheCodeConstants.APPLICATION_ACCESS_TOKEN_CACHE.value) + del_application_access_token(application_access_token.access_token) application_access_token.access_token = hashlib.md5(str(uuid.uuid1()).encode()).hexdigest()[8:24] if 'access_num' in instance and instance.get('access_num') is not None: application_access_token.access_num = instance.get("access_num") @@ -780,8 +778,7 @@ class ApplicationSerializer(serializers.Serializer): application_id = self.data.get('application_id') application_api_key = QuerySet(ApplicationApiKey).filter(id=api_key_id, application_id=application_id).first() - cache.cache.delete(application_api_key.secret_key, - version=CacheCodeConstants.APPLICATION_API_KEY_CACHE.value) + del_application_api_key(application_api_key.secret_key) application_api_key.delete() def edit(self, instance, with_valid=True): diff --git a/apps/common/cache_data/application_access_token_cache.py b/apps/common/cache_data/application_access_token_cache.py new file mode 100644 index 000000000..54f2a7e54 --- /dev/null +++ b/apps/common/cache_data/application_access_token_cache.py @@ -0,0 +1,31 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: application_access_token_cache.py + @date:2024/7/25 11:34 + @desc: +""" +from django.core.cache import cache +from django.db.models import QuerySet + +from application.models.api_key_model import ApplicationAccessToken +from common.constants.cache_code_constants import CacheCodeConstants +from common.util.cache_util import get_cache + + +@get_cache(cache_key=lambda access_token, use_get_data: access_token, + use_get_data=lambda access_token, use_get_data: use_get_data, + version=CacheCodeConstants.APPLICATION_ACCESS_TOKEN_CACHE.value) +def get_application_access_token(access_token, use_get_data): + application_access_token = QuerySet(ApplicationAccessToken).filter(access_token=access_token).first() + if application_access_token is None: + return None + return {'white_active': application_access_token.white_active, + 'white_list': application_access_token.white_list, + 'application_icon': application_access_token.application.icon, + 'application_name': application_access_token.application.name} + + +def del_application_access_token(access_token): + cache.delete(access_token, version=CacheCodeConstants.APPLICATION_ACCESS_TOKEN_CACHE.value) diff --git a/apps/common/cache_data/application_api_key_cache.py b/apps/common/cache_data/application_api_key_cache.py new file mode 100644 index 000000000..a7d810cee --- /dev/null +++ b/apps/common/cache_data/application_api_key_cache.py @@ -0,0 +1,27 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: application_api_key_cache.py + @date:2024/7/25 11:30 + @desc: +""" +from django.core.cache import cache +from django.db.models import QuerySet + +from application.models.api_key_model import ApplicationApiKey +from common.constants.cache_code_constants import CacheCodeConstants +from common.util.cache_util import get_cache + + +@get_cache(cache_key=lambda secret_key, use_get_data: secret_key, + use_get_data=lambda secret_key, use_get_data: use_get_data, + version=CacheCodeConstants.APPLICATION_API_KEY_CACHE.value) +def get_application_api_key(secret_key, use_get_data): + application_api_key = QuerySet(ApplicationApiKey).filter(secret_key=secret_key).first() + return {'allow_cross_domain': application_api_key.allow_cross_domain, + 'cross_domain_list': application_api_key.cross_domain_list} + + +def del_application_api_key(secret_key): + cache.delete(secret_key, version=CacheCodeConstants.APPLICATION_API_KEY_CACHE.value) diff --git a/apps/common/cache_data/static_resource_cache.py b/apps/common/cache_data/static_resource_cache.py new file mode 100644 index 000000000..1bb84e967 --- /dev/null +++ b/apps/common/cache_data/static_resource_cache.py @@ -0,0 +1,19 @@ +# coding=utf-8 +""" + @project: MaxKB + @Author:虎 + @file: static_resource_cache.py + @date:2024/7/25 11:30 + @desc: +""" +from common.constants.cache_code_constants import CacheCodeConstants +from common.util.cache_util import get_cache + + +@get_cache(cache_key=lambda index_path: index_path, + version=CacheCodeConstants.STATIC_RESOURCE_CACHE.value) +def get_index_html(index_path): + file = open(index_path, "r", encoding='utf-8') + content = file.read() + file.close() + return content diff --git a/apps/common/middleware/cross_domain_middleware.py b/apps/common/middleware/cross_domain_middleware.py index f7bde6f94..06c0a6aba 100644 --- a/apps/common/middleware/cross_domain_middleware.py +++ b/apps/common/middleware/cross_domain_middleware.py @@ -6,22 +6,10 @@ @date:2024/5/8 13:36 @desc: """ -from django.db.models import QuerySet from django.http import HttpResponse from django.utils.deprecation import MiddlewareMixin -from application.models.api_key_model import ApplicationApiKey -from common.constants.cache_code_constants import CacheCodeConstants -from common.util.cache_util import get_cache - - -@get_cache(cache_key=lambda secret_key, use_get_data: secret_key, - use_get_data=lambda secret_key, use_get_data: use_get_data, - version=CacheCodeConstants.APPLICATION_API_KEY_CACHE.value) -def get_application_api_key(secret_key, use_get_data): - application_api_key = QuerySet(ApplicationApiKey).filter(secret_key=secret_key).first() - return {'allow_cross_domain': application_api_key.allow_cross_domain, - 'cross_domain_list': application_api_key.cross_domain_list} +from common.cache_data.application_api_key_cache import get_application_api_key class CrossDomainMiddleware(MiddlewareMixin): diff --git a/apps/common/middleware/static_headers_middleware.py b/apps/common/middleware/static_headers_middleware.py index f835296ac..f5afcfb7c 100644 --- a/apps/common/middleware/static_headers_middleware.py +++ b/apps/common/middleware/static_headers_middleware.py @@ -6,25 +6,9 @@ @date:2024/3/13 18:26 @desc: """ -from django.db.models import QuerySet from django.utils.deprecation import MiddlewareMixin -from application.models.api_key_model import ApplicationAccessToken -from common.constants.cache_code_constants import CacheCodeConstants -from common.util.cache_util import get_cache - - -@get_cache(cache_key=lambda access_token, use_get_data: access_token, - use_get_data=lambda access_token, use_get_data: use_get_data, - version=CacheCodeConstants.APPLICATION_ACCESS_TOKEN_CACHE.value) -def get_application_access_token(access_token, use_get_data): - application_access_token = QuerySet(ApplicationAccessToken).filter(access_token=access_token).first() - if application_access_token is None: - return None - return {'white_active': application_access_token.white_active, - 'white_list': application_access_token.white_list, - 'application_icon': application_access_token.application.icon, - 'application_name': application_access_token.application.name} +from common.cache_data.application_access_token_cache import get_application_access_token class StaticHeadersMiddleware(MiddlewareMixin): diff --git a/apps/smartdoc/urls.py b/apps/smartdoc/urls.py index 85d61a954..8a1e9bf2f 100644 --- a/apps/smartdoc/urls.py +++ b/apps/smartdoc/urls.py @@ -22,6 +22,7 @@ from django.views import static from rest_framework import status from application.urls import urlpatterns as application_urlpatterns +from common.cache_data.static_resource_cache import get_index_html from common.constants.cache_code_constants import CacheCodeConstants from common.init.init_doc import init_doc from common.response.result import Result @@ -53,15 +54,6 @@ if not settings.DEBUG: pro() -@get_cache(cache_key=lambda index_path: index_path, - version=CacheCodeConstants.STATIC_RESOURCE_CACHE.value) -def get_index_html(index_path): - file = open(index_path, "r", encoding='utf-8') - content = file.read() - file.close() - return content - - def page_not_found(request, exception): """ 页面不存在处理