atmos/os_lib/web_engine/
cache.rs1use alloc::string::String;
4use alloc::vec::Vec;
5
6pub(super) fn cache_path_for(host: &str, path: &str) -> String {
8 super::cache_path::cache_path_for(host, path)
10}
11
12pub(super) fn write_cache(host: &str, path: &str, data: &[u8]) {
14 if data.is_empty() {
34 crate::warn!("[CACHE] 空データは保存しない host={} path={}", host, path);
35 return;
36 }
37 let cp = cache_path_for(host, path);
38 let payload = super::cache_path::pack_cache_payload(path, data);
41 let _fs_guard = crate::kernel::fs::FS_LOCK.lock();
46 let fs = crate::kernel::fs::get_fs();
47 let _t = super::perf::start();
50 let mut saved = fs.save_file(&cp, &payload, "browser_cache");
51 if saved.is_err() {
53 let files = fs.list_dir("/cache/browser/");
54 let need_bytes = payload.len().saturating_mul(2).max(2 * 1024 * 1024);
55 let mut freed = 0usize;
56 for meta in files {
57 let name = meta.get_filename();
58 if name.starts_with("/cache/browser/") && name != cp {
59 let sz = meta.size as usize;
60 if fs.delete_file(name).is_ok() {
61 freed = freed.saturating_add(sz);
62 if freed >= need_bytes {
63 break;
64 }
65 }
66 }
67 }
68 if freed > 0 {
69 saved = fs.save_file(&cp, &payload, "browser_cache");
70 }
71 }
72 super::perf::add(super::perf::Slot::CacheWrite, _t);
73 if saved.is_ok() {
75 crate::kernel::http_cache::DISK_RULES.lock().commit_stored(&alloc::format!("{}{}", host, path), now_sec());
76 }
77 if saved.is_ok() {
81 let verify_read = match fs.read_file_checked(&cp) {
82 Ok(v) => v,
83 Err(e) => {
84 crate::warn!("[CACHE] 保存直後の検証で読み出しエラー key={} err={}", cp, e);
85 None
86 }
87 };
88 match verify_read {
89 Some((_m, d)) if d.len() == payload.len() => {}
90 Some((_m, d)) => {
91 crate::warn!(
92 "[CACHE] 保存直後の読み返しでサイズが不一致 key={} 書込={} 読出={}",
93 cp,
94 payload.len(),
95 d.len()
96 );
97 }
98 None => {
99 crate::warn!(
100 "[CACHE] 保存は成功したのに読み返せない key={} bytes={}",
101 cp,
102 payload.len()
103 );
104 }
105 }
106 }
107 if let Err(e) = saved {
108 crate::warn!(
109 "[CACHE] 保存に失敗 host={} path={} bytes={} err={}",
110 host,
111 path,
112 payload.len(),
113 e
114 );
115 }
116}
117
118fn now_sec() -> u64 {
120 crate::kernel::timer::get_system_time_ms() / 1000
121}
122
123const MIN_REUSE_SEC: u64 = 300;
127
128pub(super) fn read_cache(host: &str, path: &str) -> Option<Vec<u8>> {
131 read_cache_with_grace(host, path, MIN_REUSE_SEC)
132}
133
134pub(super) fn read_cache_strict(host: &str, path: &str) -> Option<Vec<u8>> {
136 read_cache_with_grace(host, path, 0)
137}
138
139fn read_cache_with_grace(host: &str, path: &str, grace_sec: u64) -> Option<Vec<u8>> {
140 if crate::kernel::http_cache::DISK_RULES
141 .lock()
142 .is_stale(&alloc::format!("{}{}", host, path), now_sec(), grace_sec)
143 {
144 crate::warn!("[CACHE] Cache-Control の期限が切れた物は取り直す host={} path={}", host, path);
145 return None;
146 }
147 let cp = cache_path_for(host, path);
148 let _fs_guard = crate::kernel::fs::FS_LOCK.lock();
153 let fs = crate::kernel::fs::get_fs();
154 let _t = super::perf::start();
159 let got = match fs.read_file_checked(&cp) {
160 Ok(opt) => opt.map(|(_meta, data)| data),
161 Err(e) => {
162 crate::warn!("[CACHE] キャッシュ読み出しエラー host={} path={} err={}", host, path, e);
163 None
164 }
165 };
166 drop(_fs_guard);
167 let unpacked = got.and_then(super::cache_path::unpack_cache_payload);
168 super::perf::add(super::perf::Slot::CacheRead, _t);
169 unpacked.filter(|d| !d.is_empty())
170}
171
172pub fn clear_browser_cache() {
174 let _fs_guard = crate::kernel::fs::FS_LOCK.lock();
177 let fs = crate::kernel::fs::get_fs();
178 let files = fs.list_dir("/cache/browser/");
179 for meta in files {
180 let name = meta.get_filename();
181 if name.starts_with("/cache/browser/") {
182 let _ = fs.delete_file(name);
183 }
184 }
185 crate::info!("[SYS] BROWSER: cache cleared");
186}