Skip to content

Commit dc54948

Browse files
authored
Merge pull request #223 from i0cLiceao/main
perf: enhance fallback cache mechanism
2 parents 026f63f + d9b9b81 commit dc54948

File tree

1 file changed

+76
-37
lines changed
  • scripts/transform_plugin_data/transform_plugin_data

1 file changed

+76
-37
lines changed

scripts/transform_plugin_data/transform_plugin_data/run.sh

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,84 @@
33
echo "开始转换插件数据格式..."
44

55
# 使用jq转换数据格式,增加容错处理,并过滤掉404的仓库
6-
jq --slurpfile repo_info repo_info.json '
7-
to_entries |
8-
# 只过滤掉确认已删除(404)的仓库,保留网络错误的仓库
9-
map(select(
10-
if .value.repo and ($repo_info[0][.value.repo]) then
11-
($repo_info[0][.value.repo].status != "deleted")
6+
existing_cache_file="existing_cache.json"
7+
cleanup_existing_cache="false"
8+
9+
# 如果没有历史缓存,为jq提供一个空对象以便统一逻辑
10+
if [ ! -f "$existing_cache_file" ]; then
11+
existing_cache_file=$(mktemp)
12+
if [ -f "plugin_cache_original.json" ]; then
13+
cp plugin_cache_original.json "$existing_cache_file"
14+
else
15+
echo "{}" > "$existing_cache_file"
16+
fi
17+
cleanup_existing_cache="true"
18+
fi
19+
20+
jq --slurpfile repo_info repo_info.json --slurpfile existing_cache "$existing_cache_file" '
21+
($repo_info | if length > 0 then .[0] else {} end) as $repos |
22+
($existing_cache | if length > 0 then .[0] else {} end) as $raw_cache |
23+
(if ($raw_cache | type) == "object" and ($raw_cache | has("data")) and (($raw_cache.data | type) == "object") then $raw_cache.data
24+
elif ($raw_cache | type) == "object" then $raw_cache
25+
else {}
26+
end) as $cache |
27+
to_entries |
28+
map(
29+
. as $plugin |
30+
($repos[$plugin.value.repo] // null) as $repo_entry |
31+
($cache[$plugin.key] // {}) as $cache_entry |
32+
($repo_entry | if . then .status else "" end) as $repo_status |
33+
# 403 等非 success 且没有缓存的仓库直接丢弃,保持缓存一致性
34+
if ($repo_entry and ($repo_status == "deleted" or ($repo_status != "success" and ($cache_entry | length) == 0))) then
35+
empty
1236
else
13-
true
37+
($repo_entry | if . then .version else "" end) as $repo_version |
38+
($cache_entry.version // "") as $cache_version |
39+
($repo_entry | if . then .stars else null end) as $repo_stars |
40+
($cache_entry.stars // 0) as $cache_stars |
41+
($repo_entry | if . then .updated_at else "" end) as $repo_updated |
42+
($cache_entry.updated_at // "") as $cache_updated |
43+
($repo_entry | if . then .logo else "" end) as $repo_logo |
44+
($cache_entry.logo // "") as $cache_logo |
45+
(if ($repo_version // "") != "" then $repo_version
46+
elif ($cache_version // "") != "" then $cache_version
47+
else "1.0.0"
48+
end) as $final_version |
49+
(if ($repo_status == "success") and ($repo_stars != null) then $repo_stars else $cache_stars end) as $final_stars |
50+
(if ($repo_updated // "") != "" then $repo_updated
51+
elif ($cache_updated // "") != "" then $cache_updated
52+
else ""
53+
end) as $final_updated |
54+
(if ($repo_logo // "") != "" then $repo_logo
55+
elif ($cache_logo // "") != "" then $cache_logo
56+
else ""
57+
end) as $final_logo |
58+
{
59+
key: $plugin.key,
60+
value:
61+
(
62+
$plugin.value + {
63+
# 保持原有字段
64+
desc: $plugin.value.desc,
65+
author: $plugin.value.author,
66+
repo: $plugin.value.repo,
67+
tags: ($plugin.value.tags // [])
68+
}
69+
+ (if $plugin.value.social_link then { social_link: $plugin.value.social_link } else {} end)
70+
+ {
71+
stars: ($final_stars // 0),
72+
version: $final_version
73+
}
74+
+ (if ($final_updated // "") != "" then { updated_at: $final_updated } else {} end)
75+
+ (if ($final_logo // "") != "" then { logo: $final_logo } else {} end)
76+
)
77+
}
1478
end
15-
)) |
16-
map({
17-
key: .key,
18-
value: (
19-
.value + {
20-
# 保持原有字段
21-
desc: .value.desc,
22-
author: .value.author,
23-
repo: .value.repo,
24-
tags: (.value.tags // [])
25-
} +
26-
# 仅当social_link存在且不为空时添加
27-
(if .value.social_link then { social_link: .value.social_link } else {} end) +
28-
# 添加新字段,从repo_info中获取
29-
(if .value.repo and ($repo_info[0][.value.repo]) then
30-
($repo_info[0][.value.repo] | {
31-
stars: .stars,
32-
updated_at: .updated_at,
33-
version: (if .version != "" then .version else "1.0.0" end)
34-
} +
35-
# 仅当logo存在且不为空时添加logo字段
36-
(if .logo and .logo != "" then { logo: .logo } else {} end))
37-
else
38-
{
39-
stars: 0,
40-
version: "1.0.0"
41-
}
42-
end)
43-
)
44-
}) | from_entries' original_plugins.json > temp_plugin_cache_original.json
79+
) | from_entries' original_plugins.json > temp_plugin_cache_original.json
80+
81+
if [ "$cleanup_existing_cache" = "true" ]; then
82+
rm -f "$existing_cache_file"
83+
fi
4584

4685
# 格式化JSON使其更易读
4786
jq . temp_plugin_cache_original.json > plugin_cache_original.json

0 commit comments

Comments
 (0)