Skip to content

Commit 7ce768d

Browse files
authored
feat(open api v2): list user support cache page (#2315)
1 parent 8fe506b commit 7ce768d

3 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/bk-user/bkuser/apis/open_v2/mixins.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ def get_collaboration_field_mapping(self) -> Dict[Tuple[str, str], str]:
101101

102102
@cachedmethod(timeout=60 * 60)
103103
def data_source_id_to_tenant_id_map(self) -> Dict[int, str]:
104-
return {
105-
ds.id: ds.owner_tenant_id
106-
for ds in DataSource.objects.filter(owner_tenant=self.default_tenant).only("id", "owner_tenant_id")
107-
}
104+
return {ds.id: ds.owner_tenant_id for ds in DataSource.objects.all().only("id", "owner_tenant_id")}
108105

109106

110107
class DataSourceDomainMixin:

src/bk-user/bkuser/apis/open_v2/views/profilers.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@
2323

2424
import phonenumbers
2525
from blue_krill.data_types.enum import EnumField, StructuredEnum
26+
from django.conf import settings
2627
from django.db.models import Q, QuerySet
27-
28-
# from django.conf import settings
2928
from django.http import Http404
30-
31-
# from django.utils.decorators import method_decorator
32-
# from django.views.decorators.cache import cache_page
29+
from django.utils.decorators import method_decorator
30+
from django.views.decorators.cache import cache_page
3331
from rest_framework import generics
3432
from rest_framework.response import Response
3533

@@ -49,6 +47,7 @@
4947
)
5048
from bkuser.apps.tenant.constants import TenantUserStatus
5149
from bkuser.apps.tenant.models import DataSourceDepartment, TenantDepartment, TenantUser
50+
from bkuser.common.cache import CacheEnum
5251
from bkuser.common.error_codes import error_codes
5352
from bkuser.common.views import ExcludePatchAPIViewMixin
5453
from bkuser.utils.tree import Tree
@@ -384,7 +383,7 @@ class ProfileListApi(LegacyOpenApiCommonMixin, TenantUserListToUserInfosMixin, g
384383

385384
pagination_class = LegacyOpenApiPagination
386385

387-
# @method_decorator(cache_page(cache="redis", 60 * 60, key_prefix="openapi_v2_profile_list"))
386+
@method_decorator(cache_page(cache=CacheEnum.REDIS.value, timeout=settings.OPEN_API_V2_LIST_USER_CACHE_TIMEOUT))
388387
def get(self, request, *args, **kwargs):
389388
slz = ProfileListInputSLZ(data=request.query_params)
390389
slz.is_valid(raise_exception=True)

src/bk-user/bkuser/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,9 @@ def _build_file_handler(log_path: Path, filename: str, format: str) -> Dict:
865865
# 限制人员选择器用户/部门搜索 API 返回的最大条数,避免性能问题
866866
SELECTOR_SEARCH_API_LIMIT = env.int("SELECTOR_SEARCH_API_LIMIT", 100)
867867

868+
# Open API V2 ListUser 接口缓存过期时间,单位秒,默认 1 小时
869+
OPEN_API_V2_LIST_USER_CACHE_TIMEOUT = env.int("OPEN_API_V2_LIST_USER_CACHE_TIMEOUT", default=60 * 60)
870+
868871
# 限制 OpenWeb API 调用频率,避免恶意请求问题
869872
OPEN_WEB_API_THROTTLE_RATES = env.str("OPEN_WEB_API_THROTTLE_RATES", "100/minute")
870873

0 commit comments

Comments
 (0)