Skip to content

Commit

Permalink
fix(backend): 修复appcache缓存失效的问题 TencentBlueKing#7799
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud committed Nov 7, 2024
1 parent b4c3184 commit 4a5de3d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
12 changes: 11 additions & 1 deletion dbm-ui/backend/db_meta/models/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,20 @@ def batch_get_app_attr(cls, bk_biz_ids, attr_name="db_app_abbr"):
app_infos = {info["bk_biz_id"]: info[attr_name] for info in infos}
return app_infos

@classmethod
def get_appcache(cls, key):
if key not in ["appcache_list", "appcache_dict"]:
raise ValueError(_("缓存key不存在,请检查key是否为appcache_dict/appcache_list"))
if not cache.get(key):
from backend.db_meta.utils import cache_appcache_data

cache_appcache_data(cls)
return cache.get(key)

@classmethod
def get_choices(cls):
try:
appcache_data = cache.get("appcache_list")
appcache_data = cls.get_appcache("appcache_list")
biz_choices = [(app["bk_biz_id"], f"[{app['bk_biz_id']}]{app['bk_biz_name']}") for app in appcache_data]
except Exception: # pylint: disable=broad-except
# 忽略出现的异常,此时可能因为表未初始化
Expand Down
3 changes: 1 addition & 2 deletions dbm-ui/backend/db_meta/models/db_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"""
import logging

from django.core.cache import cache
from django.db import models
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -71,7 +70,7 @@ def get_choices_with_filter(cls, cluster_type=None):
for dm in cls.objects.filter(q).all():

try:
appcache_dict = cache.get("appcache_dict")
appcache_dict = AppCache.get_appcache(key="appcache_dict")
appcache = appcache_dict.get(str(dm.bk_biz_id))
db_module_choices.append(
(
Expand Down
6 changes: 4 additions & 2 deletions dbm-ui/backend/db_meta/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,7 @@ def cache_appcache_data(sender, **kwargs):
data = AppCache.objects.all().values()
appcache_list = list(data) if data else []
appcache_dict = {app["bk_biz_id"]: app for app in data}
cache.set("appcache_list", appcache_list)
cache.set("appcache_dict", appcache_dict)
# 默认30min过期,稍微晚于周期同步cc拓扑的定时任务(20min)
timeout = 60 * 30
cache.set("appcache_list", appcache_list, timeout=timeout)
cache.set("appcache_dict", appcache_dict, timeout=timeout)
11 changes: 8 additions & 3 deletions dbm-ui/backend/db_services/dbresource/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,15 @@ def get_subzones(self, request):
def get_device_class(self, request):
data = self.params_validate(self.get_serializer_class())
page = {"start": data["offset"], "limit": data["limit"]}
# 默认过滤条件是支持申请的机型
dvc_filter = {"condition": "AND", "rules": [{"field": "enable_apply", "operator": "equal", "value": True}]}
# 默认过滤条件是支持申请的机型,且是常规项目
rules = [
{"field": "enable_apply", "operator": "equal", "value": True},
{"field": "require_type", "operator": "equal", "value": 1},
]
# 如果有名称过滤,则加上
if data.get("name"):
dvc_filter["rules"].append({"field": "device_type", "operator": "contains", "value": data["name"]})
rules.append({"field": "device_type", "operator": "contains", "value": data["name"]})
dvc_filter = {"condition": "AND", "rules": rules}
# 请求hcm平台获取机型列表,若报错则忽略
try:
device_list = HCMApi.list_cvm_device(params={"filter": dvc_filter, "page": page})
Expand Down

0 comments on commit 4a5de3d

Please sign in to comment.