perf: refresh asset type tree

This commit is contained in:
feng 2025-04-15 20:17:39 +08:00 committed by ZhaoJiSen
parent 314da330c0
commit cb49e26387
3 changed files with 21 additions and 7 deletions

View File

@ -8,7 +8,7 @@ from common.const.signals import POST_ADD, POST_REMOVE, POST_CLEAR
from common.exceptions import M2MReverseNotAllowed
from common.utils import get_logger, get_object_or_none
from perms.models import AssetPermission
from perms.utils import UserPermTreeExpireUtil
from perms.utils import UserPermTreeExpireUtil, UserPermAssetUtil
from users.models import User, UserGroup
logger = get_logger(__file__)
@ -42,6 +42,7 @@ def on_user_groups_change(sender, instance, action, reverse, pk_set, **kwargs):
return
UserPermTreeExpireUtil().expire_perm_tree_for_users_orgs(user_ids, [org_id])
UserPermAssetUtil.refresh_type_nodes_tree_cache(user_ids)
@receiver([pre_delete], sender=AssetPermission)
@ -96,6 +97,7 @@ def on_asset_permission_users_changed(sender, action, reverse, instance, pk_set,
return
user_ids = pk_set
UserPermTreeExpireUtil().expire_perm_tree_for_users_orgs(user_ids, [instance.org.id])
UserPermAssetUtil.refresh_type_nodes_tree_cache(user_ids)
@receiver(m2m_changed, sender=AssetPermission.user_groups.through)

View File

@ -4,12 +4,13 @@ import re
from django.conf import settings
from django.core.cache import cache
from django.db.models import Q
from django.utils.translation import get_language
from rest_framework.utils.encoders import JSONEncoder
from assets.const import AllTypes
from assets.models import FavoriteAsset, Asset, Node
from common.utils.common import timeit, get_logger
from orgs.utils import current_org
from orgs.utils import current_org, get_current_org_id
from perms.models import PermNode, UserAssetGrantedTreeNodeRelation, AssetPermission
from .permission import AssetPermissionUtil
@ -93,7 +94,8 @@ class UserPermAssetUtil(AssetPermissionPermAssetUtil):
@classmethod
def get_type_nodes_tree_or_cached(cls, user):
key = f'perms:type-nodes-tree:{user.id}:{current_org.id}'
lang = get_language()
key = f'perms:type-nodes-tree:{user.id}:{current_org.id}:{lang}'
nodes = cache.get(key)
if nodes is None:
nodes = cls(user).get_type_nodes_tree()
@ -103,10 +105,18 @@ class UserPermAssetUtil(AssetPermissionPermAssetUtil):
nodes = json.loads(nodes)
return nodes
def refresh_type_nodes_tree_cache(self):
@classmethod
def refresh_type_nodes_tree_cache(cls, user_ids=None, org_id=None):
if user_ids is None:
user_ids = []
if org_id is None:
org_id = get_current_org_id()
logger.debug("Refresh type nodes tree cache")
key = f'perms:type-nodes-tree:{self.user.id}:{current_org.id}'
cache.delete(key)
for user_id in user_ids:
key = f'perms:type-nodes-tree:{user_id}:{org_id}*'
cache.delete_pattern(key)
def refresh_favorite_assets(self):
favor_ids = FavoriteAsset.objects.filter(user=self.user).values_list('asset_id', flat=True)

View File

@ -182,6 +182,7 @@ class UserPermTreeExpireUtil(_UserPermTreeCacheMixin):
for org_id, perms_id in org_perms_mapper.items():
user_ids = AssetPermission.get_all_users_for_perms(perm_ids, flat=True)
self.expire_perm_tree_for_users_orgs(user_ids, [org_id])
UserPermAssetUtil.refresh_type_nodes_tree_cache(user_ids, org_id)
def expire_perm_tree_for_user_group(self, user_group):
group_ids = [user_group.id]
@ -193,6 +194,7 @@ class UserPermTreeExpireUtil(_UserPermTreeCacheMixin):
.filter(usergroup_id__in=group_ids) \
.values_list('user_id', flat=True).distinct()
self.expire_perm_tree_for_users_orgs(user_ids, org_ids)
UserPermAssetUtil.refresh_type_nodes_tree_cache(user_ids)
@on_transaction_commit
def expire_perm_tree_for_users_orgs(self, user_ids, org_ids):
@ -232,7 +234,7 @@ def refresh_user_favorite_assets(users=()):
for user in users:
util = UserPermAssetUtil(user)
util.refresh_favorite_assets()
util.refresh_type_nodes_tree_cache()
util.refresh_type_nodes_tree_cache([user.id])
class UserPermTreeBuildUtil(object):