Skip to main content

yggdrasil/pages/admin/settings/
cache_section.rs

1//! 图片缓存分区:`uploads/.cache/` 的容量与保留时长。
2//!
3//! 清理任务每小时 tick 时从 runtime accessor 读取最新值,保存后即时生效。
4
5use leptos::prelude::*;
6
7use super::kit::{use_section, SettingRow, SettingsCard};
8use super::SettingsSection;
9use crate::api::error::ApiError;
10use crate::api::settings::{get_image_cache_settings, update_image_cache_settings};
11use crate::components::forms::NumberInput;
12use crate::models::settings::ImageCacheSettings;
13
14async fn save(d: ImageCacheSettings) -> Result<ImageCacheSettings, ApiError> {
15    update_image_cache_settings(d.disk_cache_max_mb, d.disk_cache_max_age_hours).await
16}
17
18/// 图片缓存分区组件。
19#[component]
20pub fn CacheSection() -> impl IntoView {
21    let st = use_section(SettingsSection::Cache, get_image_cache_settings, save);
22    view! {
23        <SettingsCard
24            section=SettingsSection::Cache
25            description="缩略图与缩放图的磁盘缓存,清理任务每小时扫描一次。"
26            ready=st.ready()
27            load_failed=st.load_failed()
28            failed=st.failed()
29        >
30            <SettingRow label="最大容量" html_for="cache-max-mb">
31                <NumberInput
32                    id="cache-max-mb"
33                    value=st.field(|d| i64::from(d.disk_cache_max_mb))
34                    min=1
35                    max=1_048_576
36                    step=128
37                    unit="MB"
38                    hint="超限时按修改时间删除最旧的缓存文件。默认 1024 MB。"
39                    onchange=move |v: i64| st.edit(|d| d.disk_cache_max_mb = v as u32)
40                />
41            </SettingRow>
42            <SettingRow label="最长保留" html_for="cache-max-age">
43                <NumberInput
44                    id="cache-max-age"
45                    value=st.field(|d| i64::from(d.disk_cache_max_age_hours))
46                    min=1
47                    max=87_600
48                    step=24
49                    unit="小时"
50                    hint="超期的缓存文件优先删除。默认 168 小时(7 天)。"
51                    onchange=move |v: i64| st.edit(|d| d.disk_cache_max_age_hours = v as u32)
52                />
53            </SettingRow>
54        </SettingsCard>
55    }
56}