Skip to content

Commit 4f74566

Browse files
authored
Merge pull request #644 from MoYingJi/feat-cache-size
feat(setting): 支持自定义缓存大小上限
2 parents 604dd2e + e8de5e3 commit 4f74566

1 file changed

Lines changed: 45 additions & 44 deletions

File tree

src/components/Setting/LocalSetting.vue

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,43 @@
9595
<n-card class="set-item">
9696
<div class="label">
9797
<n-text class="name">缓存大小上限</n-text>
98-
<n-text class="tip" :depth="3">达到上限后将清理最旧的缓存</n-text>
98+
<n-text class="tip" :depth="3">达到上限后将清理最旧的缓存,数值可以是小数,最低 2GB</n-text>
9999
</div>
100-
<n-select
101-
v-model:value="cacheLimit"
102-
:options="cacheSizeOptions"
103-
class="set"
104-
@update:value="changeCacheLimit"
105-
/>
100+
<n-input-group class="set">
101+
<n-input-number
102+
:value="cacheLimit"
103+
:update-value-on-input="false"
104+
:min="2"
105+
:max="9999"
106+
:style="{
107+
width: cacheLimited ? '55%' : '0%',
108+
transition: isMounted ? 'width 0.3s ease' : 'none',
109+
}"
110+
@update:value="(value) => {
111+
cacheLimit = value ?? 2;
112+
changeCacheLimit(cacheLimit);
113+
}"
114+
/>
115+
<n-select
116+
v-model:value="cacheLimited"
117+
:options="[
118+
{ label: '不限制', value: 0 },
119+
{ label: cacheLimited === 0 ? '输入数值 (单位 GB)' : 'GB', value: 1 },
120+
]"
121+
:style="{
122+
width: cacheLimited ? '45%' : '100%',
123+
transition: isMounted ? 'width 0.3s ease' : 'none',
124+
}"
125+
@update:value="(value) => {
126+
if (value === 0) {
127+
changeCacheLimit(0);
128+
} else {
129+
if (cacheLimit === 0) cacheLimit = 2;
130+
changeCacheLimit(cacheLimit);
131+
}
132+
}"
133+
/>
134+
</n-input-group>
106135
</n-card>
107136
<n-card class="set-item">
108137
<div class="label">
@@ -289,9 +318,13 @@ import { isDevBuild } from "@/utils/env";
289318
290319
const settingStore = useSettingStore();
291320
const cacheManager = useCacheManager();
321+
322+
const isMounted = ref<boolean>(false);
323+
292324
const cachePath = ref<string>("");
293325
const cacheSizeDisplay = ref<string>("--");
294326
const cacheLimit = ref<number>(10); // 本地状态
327+
const cacheLimited = ref<number>(1); // 是否限制缓存 (1 为限制)
295328
296329
// 默认下载音质选项
297330
const downloadQualityOptions = computed(() => {
@@ -332,41 +365,6 @@ const folderStrategyOptions = [
332365
},
333366
];
334367
335-
const cacheSizeOptions = [
336-
{
337-
label: "不限制",
338-
value: 0,
339-
},
340-
{
341-
label: "5G",
342-
value: 5,
343-
},
344-
{
345-
label: "10G",
346-
value: 10,
347-
},
348-
{
349-
label: "15G",
350-
value: 15,
351-
},
352-
{
353-
label: "20G",
354-
value: 20,
355-
},
356-
{
357-
label: "25G",
358-
value: 25,
359-
},
360-
{
361-
label: "30G",
362-
value: 30,
363-
},
364-
{
365-
label: "50G",
366-
value: 50,
367-
},
368-
];
369-
370368
// 选择下载路径
371369
const choosePath = async () => {
372370
const path = await window.electron.ipcRenderer.invoke("choose-path");
@@ -397,7 +395,6 @@ const confirmChangeCachePath = () => {
397395
398396
// 更改缓存大小限制
399397
const changeCacheLimit = async (value: number) => {
400-
cacheLimit.value = value;
401398
await window.api.store.set("cacheLimit", value);
402399
};
403400
@@ -465,11 +462,15 @@ onMounted(async () => {
465462
const path = await window.api.store.get("cachePath");
466463
cachePath.value = path || "";
467464
const limit = await window.api.store.get("cacheLimit");
468-
if (typeof limit === "number") cacheLimit.value = limit;
465+
if (typeof limit === "number") {
466+
cacheLimit.value = limit;
467+
if (limit === 0) cacheLimited.value = 0;
468+
}
469469
} catch (error) {
470470
console.error("读取缓存路径失败:", error);
471471
}
472472
await loadCacheSize();
473+
isMounted.value = true;
473474
});
474475
</script>
475476

0 commit comments

Comments
 (0)