Skip to main content

yggdrasil/pages/admin/settings/
page_cache_section.rs

1//! 页面缓存分区:匿名公开页 HTML 缓存的单页上限与 CDN 边缘缓存时长。
2//!
3//! 写入数据库,下次启动生效(HTML 缓存在首次使用时读取)。
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_ssr_cache_settings, update_ssr_cache_settings};
11use crate::components::forms::NumberInput;
12use crate::models::settings::{SsrCacheSettings, MAX_SSR_CACHE_EDGE_TTL_SECS};
13
14async fn save(d: SsrCacheSettings) -> Result<SsrCacheSettings, ApiError> {
15    update_ssr_cache_settings(d.max_page_bytes, d.edge_ttl_secs).await
16}
17
18/// 页面缓存分区组件。
19#[component]
20pub fn PageCacheSection() -> impl IntoView {
21    let st = use_section(SettingsSection::PageCache, get_ssr_cache_settings, save);
22    view! {
23        <SettingsCard
24            section=SettingsSection::PageCache
25            description="匿名访客的公开页 HTML 缓存在内存里,总量 32 MiB;可选让 CDN 节点一并缓存。"
26            ready=st.ready()
27            load_failed=st.load_failed()
28            failed=st.failed()
29        >
30            <SettingRow label="单页上限" html_for="ssr-cache-max-page">
31                <NumberInput
32                    id="ssr-cache-max-page"
33                    value=st.field(|d| (d.max_page_bytes / 1024) as i64)
34                    min=64
35                    max=32_768
36                    step=256
37                    unit="KB"
38                    hint="渲染后更大的页面不缓存,每次请求重新渲染。默认 2048 KB。"
39                    onchange=move |v: i64| st.edit(|d| d.max_page_bytes = v as u64 * 1024)
40                />
41            </SettingRow>
42            <SettingRow label="边缘缓存时长" html_for="ssr-cache-edge-ttl">
43                <NumberInput
44                    id="ssr-cache-edge-ttl"
45                    value=st.field(|d| d.edge_ttl_secs as i64)
46                    min=0
47                    max=MAX_SSR_CACHE_EDGE_TTL_SECS as i64
48                    step=60
49                    unit="秒"
50                    hint="同一批页面在 CDN 节点缓存多久(s-maxage),浏览器仍每次校验。0 表示不让 CDN 缓存。内容变更后清 EdgeOne 缓存需要服务器配置腾讯云凭据,否则只能等过期。"
51                    onchange=move |v: i64| st.edit(|d| d.edge_ttl_secs = v.max(0) as u64)
52                />
53            </SettingRow>
54        </SettingsCard>
55    }
56}