From c87c432c1a134252a2a736f64f700299f66e6c2e Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 7 Feb 2023 17:13:50 +0800 Subject: [PATCH 1/2] =?UTF-8?q?perf:=20=E5=BB=B6=E8=BF=9F=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E4=B8=80=E4=B8=AA=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/decorator.py | 44 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/apps/common/decorator.py b/apps/common/decorator.py index 1fc2b2a88..bb7b3e866 100644 --- a/apps/common/decorator.py +++ b/apps/common/decorator.py @@ -1,5 +1,11 @@ # -*- coding: utf-8 -*- # +import functools +import threading +import time +import uuid + +from django.core.cache import cache from django.db import transaction @@ -7,13 +13,16 @@ def on_transaction_commit(func): """ 如果不调用on_commit, 对象创建时添加多对多字段值失败 """ + def inner(*args, **kwargs): transaction.on_commit(lambda: func(*args, **kwargs)) + return inner class Singleton(object): """ 单例类 """ + def __init__(self, cls): self._cls = cls self._instance = {} @@ -22,3 +31,38 @@ class Singleton(object): if self._cls not in self._instance: self._instance[self._cls] = self._cls() return self._instance[self._cls] + + +def _run_func_if_is_last(ttl, func, *args, **kwargs): + ix = uuid.uuid4().__str__() + key = f'DELAY_RUN_{func.__name__}' + cache.set(key, ix, ttl) + st = (ttl - 2 > 1) and ttl - 2 or 1 + time.sleep(st) + got = cache.get(key, None) + + if ix == got: + func(*args, **kwargs) + cache.delete(key) + + +def delay_run(ttl=5): + def inner(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + t = threading.Thread(target=_run_func_if_is_last, args=(ttl, func, *args), kwargs=kwargs) + t.start() + + return wrapper + + return inner + + +@delay_run(ttl=10) +def run_it_many(username, year=2000): + print("Hello, %s, now is %s" % (username, year)) + + +if __name__ == '__main__': + for i in range(20): + run_it_many('test', 2000) From fe725106acd61c2e30fbab6f36a12cab7f560e81 Mon Sep 17 00:00:00 2001 From: ibuler Date: Tue, 7 Feb 2023 17:51:22 +0800 Subject: [PATCH 2/2] fix: role detail --- apps/rbac/api/role.py | 4 ++-- apps/users/api/user.py | 19 +++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/apps/rbac/api/role.py b/apps/rbac/api/role.py index 967aeb6b8..e2cca9bf0 100644 --- a/apps/rbac/api/role.py +++ b/apps/rbac/api/role.py @@ -69,8 +69,8 @@ class RoleViewSet(JMSModelViewSet): role.users_amount = role_user_amount_mapper.get(role.id, 0) return queryset - def filter_queryset(self, queryset): - queryset = super().filter_queryset(queryset) + def page_queryset(self, queryset): + queryset = super().page_queryset(queryset) queryset = self.set_users_amount(queryset) return queryset diff --git a/apps/users/api/user.py b/apps/users/api/user.py index c882719f6..69ee41785 100644 --- a/apps/users/api/user.py +++ b/apps/users/api/user.py @@ -2,27 +2,27 @@ from collections import defaultdict from django.utils.translation import ugettext as _ -from rest_framework.decorators import action from rest_framework import generics +from rest_framework.decorators import action from rest_framework.response import Response from rest_framework_bulk import BulkModelViewSet from common.api import CommonApiMixin -from common.utils import get_logger from common.api import SuggestionMixin +from common.utils import get_logger from orgs.utils import current_org, tmp_to_root_org from rbac.models import Role, RoleBinding from users.utils import LoginBlockUtil, MFABlockUtils from .mixins import UserQuerysetMixin -from ..notifications import ResetMFAMsg from .. import serializers +from ..filters import UserFilter +from ..models import User +from ..notifications import ResetMFAMsg from ..serializers import ( UserSerializer, MiniUserSerializer, InviteSerializer ) -from ..models import User from ..signals import post_user_create -from ..filters import UserFilter logger = get_logger(__name__) __all__ = [ @@ -53,13 +53,8 @@ class UserViewSet(CommonApiMixin, UserQuerysetMixin, SuggestionMixin, BulkModelV return queryset def paginate_queryset(self, queryset): - page = super().paginate_queryset(queryset) - - if page: - page = self.set_users_roles_for_cache(page) - else: - self.set_users_roles_for_cache(queryset) - return page + queryset = super().paginate_queryset(queryset) or queryset + return self.set_users_roles_for_cache(queryset) @action(methods=['get'], detail=False, url_path='suggestions') def match(self, request, *args, **kwargs):