use std::io::{self, Write};
use std::path::{Path, PathBuf};
use serde::{Serialize, de::DeserializeOwned};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CacheKey {
uri_hash: String,
content_hash: String,
}
impl CacheKey {
fn as_filename(&self) -> &str {
&self.uri_hash
}
}
#[derive(Debug, Clone)]
pub struct WorkspaceCache {
dir: PathBuf,
}
pub const CACHE_SIZE_CAP: u64 = 512 * 1024 * 1024;
impl WorkspaceCache {
pub fn new(root: &Path) -> Option<Self> {
let base = cache_base_dir()?;
let schema = schema_version();
let php_lsp_dir = base.join("php-lsp");
drop(prune_stale_schema_dirs(&php_lsp_dir, schema));
let workspace = workspace_hash(root);
let dir = php_lsp_dir.join(schema).join(workspace);
std::fs::create_dir_all(&dir).ok()?;
let cache = Self { dir };
if cache.size_bytes().unwrap_or(0) > CACHE_SIZE_CAP {
let _ = cache.clear();
}
Some(cache)
}
pub fn cache_dir(&self) -> &std::path::Path {
&self.dir
}
pub fn size_bytes(&self) -> io::Result<u64> {
dir_size(&self.dir)
}
pub fn with_dir(dir: PathBuf) -> Self {
Self { dir }
}
pub fn key_for(uri: &str, content: &str) -> CacheKey {
let uri_hash = blake3::hash(uri.as_bytes()).to_hex().as_str()[..32].to_string();
let content_hash = blake3::hash(content.as_bytes()).to_hex().as_str()[..32].to_string();
CacheKey {
uri_hash,
content_hash,
}
}
pub fn read<T: DeserializeOwned>(&self, key: &CacheKey) -> Option<T> {
let path = self.path_for(key);
let bytes = std::fs::read(&path).ok()?;
let config = bincode::config::standard();
let (stored_hash, value): (String, T) =
bincode::serde::decode_from_slice(&bytes, config).ok()?.0;
if stored_hash != key.content_hash {
return None;
}
Some(value)
}
pub fn write<T: Serialize>(&self, key: &CacheKey, value: &T) -> io::Result<()> {
let path = self.path_for(key);
let tmp = path.with_extension("tmp");
let config = bincode::config::standard();
let bytes = bincode::serde::encode_to_vec((&key.content_hash, value), config)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
{
let mut f = std::fs::File::create(&tmp)?;
f.write_all(&bytes)?;
}
std::fs::rename(&tmp, &path)?;
Ok(())
}
pub fn clear(&self) -> io::Result<()> {
if self.dir.exists() {
std::fs::remove_dir_all(&self.dir)?;
std::fs::create_dir_all(&self.dir)?;
}
Ok(())
}
fn path_for(&self, key: &CacheKey) -> PathBuf {
self.dir.join(format!("{}.bin", key.as_filename()))
}
}
fn dir_size(dir: &Path) -> io::Result<u64> {
let mut total = 0u64;
let entries = match std::fs::read_dir(dir) {
Ok(e) => e,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(0),
Err(e) => return Err(e),
};
for entry in entries.flatten() {
let meta = match entry.metadata() {
Ok(m) => m,
Err(_) => continue,
};
if meta.is_dir() {
total = total.saturating_add(dir_size(&entry.path())?);
} else if meta.is_file() {
total = total.saturating_add(meta.len());
}
}
Ok(total)
}
fn prune_stale_schema_dirs(
php_lsp_dir: &Path,
current_schema: &str,
) -> Option<std::thread::JoinHandle<()>> {
let entries = std::fs::read_dir(php_lsp_dir).ok()?;
let stale: Vec<PathBuf> = entries
.flatten()
.filter(|entry| {
entry.file_name() != current_schema && entry.file_type().is_ok_and(|t| t.is_dir())
})
.map(|entry| entry.path())
.collect();
if stale.is_empty() {
return None;
}
Some(std::thread::spawn(move || {
for dir in stale {
let _ = std::fs::remove_dir_all(dir);
}
}))
}
pub(crate) fn cache_base_dir() -> Option<PathBuf> {
if let Some(xdg) = std::env::var_os("XDG_CACHE_HOME")
&& !xdg.is_empty()
{
return Some(PathBuf::from(xdg));
}
if cfg!(windows) {
if let Some(local) = std::env::var_os("LOCALAPPDATA")
&& !local.is_empty()
{
return Some(PathBuf::from(local));
}
} else if let Some(home) = std::env::var_os("HOME")
&& !home.is_empty()
{
return Some(PathBuf::from(home).join(".cache"));
}
None
}
fn schema_version() -> &'static str {
concat!(env!("CARGO_PKG_VERSION"), "-fi-v5")
}
fn workspace_hash(root: &Path) -> String {
let canonical = root.canonicalize().unwrap_or_else(|_| root.to_path_buf());
let hex = blake3::hash(canonical.as_os_str().as_encoded_bytes()).to_hex();
hex.as_str()[..16].to_string()
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
#[derive(Serialize, serde::Deserialize, PartialEq, Debug)]
struct SamplePayload {
name: String,
values: Vec<u32>,
}
#[test]
fn key_for_is_deterministic_per_uri_and_content() {
let k1 = WorkspaceCache::key_for("file:///a.php", "<?php echo 1;");
let k2 = WorkspaceCache::key_for("file:///a.php", "<?php echo 1;");
assert_eq!(k1, k2);
}
#[test]
fn key_for_differs_when_content_differs() {
let k1 = WorkspaceCache::key_for("file:///a.php", "<?php echo 1;");
let k2 = WorkspaceCache::key_for("file:///a.php", "<?php echo 2;");
assert_ne!(k1, k2);
}
#[test]
fn key_for_differs_when_uri_differs() {
let k1 = WorkspaceCache::key_for("file:///a.php", "<?php");
let k2 = WorkspaceCache::key_for("file:///b.php", "<?php");
assert_ne!(k1, k2);
}
#[test]
fn write_overwrites_slot_instead_of_orphaning_old_revision() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let uri = "file:///churn.php";
let key_v1 = WorkspaceCache::key_for(uri, "<?php echo 1;");
cache
.write(
&key_v1,
&SamplePayload {
name: "v1".into(),
values: vec![1],
},
)
.unwrap();
let key_v2 = WorkspaceCache::key_for(uri, "<?php echo 2;");
cache
.write(
&key_v2,
&SamplePayload {
name: "v2".into(),
values: vec![2],
},
)
.unwrap();
let bin_files: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "bin"))
.collect();
assert_eq!(
bin_files.len(),
1,
"editing a file must not orphan its previous cache entry"
);
let stale: Option<SamplePayload> = cache.read(&key_v1);
assert!(stale.is_none());
let current: SamplePayload = cache.read(&key_v2).unwrap();
assert_eq!(current.name, "v2");
}
#[test]
fn write_then_read_round_trips() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let key = WorkspaceCache::key_for("file:///x.php", "<?php");
let payload = SamplePayload {
name: "x".into(),
values: vec![1, 2, 3],
};
cache.write(&key, &payload).unwrap();
let decoded: SamplePayload = cache.read(&key).unwrap();
assert_eq!(decoded, payload);
}
#[test]
fn read_returns_none_for_missing_key() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let missing = WorkspaceCache::key_for("file:///nope.php", "");
let decoded: Option<SamplePayload> = cache.read(&missing);
assert!(decoded.is_none());
}
#[test]
fn read_returns_none_for_corrupted_entry() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let key = WorkspaceCache::key_for("file:///c.php", "<?php");
std::fs::write(cache.path_for(&key), b"not valid bincode").unwrap();
let decoded: Option<SamplePayload> = cache.read(&key);
assert!(
decoded.is_none(),
"corrupted entry must look missing, not panic"
);
}
#[test]
fn write_is_atomic_via_rename() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let key = WorkspaceCache::key_for("file:///atomic.php", "<?php");
let payload = SamplePayload {
name: "a".into(),
values: vec![],
};
cache.write(&key, &payload).unwrap();
let tmp = cache.path_for(&key).with_extension("tmp");
assert!(!tmp.exists(), "tmp file should be removed by rename");
}
#[test]
fn clear_drops_all_entries() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
for i in 0..3 {
let k = WorkspaceCache::key_for(&format!("file:///c{i}.php"), "");
cache
.write(
&k,
&SamplePayload {
name: i.to_string(),
values: vec![],
},
)
.unwrap();
}
cache.clear().unwrap();
for i in 0..3 {
let k = WorkspaceCache::key_for(&format!("file:///c{i}.php"), "");
let decoded: Option<SamplePayload> = cache.read(&k);
assert!(decoded.is_none());
}
}
#[test]
fn size_bytes_sums_flat_bin_files() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
assert_eq!(cache.size_bytes().unwrap(), 0);
let key1 = WorkspaceCache::key_for("file:///s1.php", "<?php");
cache
.write(
&key1,
&SamplePayload {
name: "s1".into(),
values: vec![0u32; 16],
},
)
.unwrap();
let key2 = WorkspaceCache::key_for("file:///s2.php", "<?php");
cache
.write(
&key2,
&SamplePayload {
name: "s2".into(),
values: vec![0u32; 16],
},
)
.unwrap();
let total = cache.size_bytes().unwrap();
let expected1 = cache.path_for(&key1).metadata().unwrap().len();
let expected2 = cache.path_for(&key2).metadata().unwrap().len();
assert_eq!(total, expected1 + expected2);
}
#[test]
fn size_bytes_recurses_into_nested_session_dir() {
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let key = WorkspaceCache::key_for("file:///s.php", "<?php");
cache
.write(
&key,
&SamplePayload {
name: "s".into(),
values: vec![0u32; 16],
},
)
.unwrap();
let session_dir = dir.path().join("session");
std::fs::create_dir_all(&session_dir).unwrap();
std::fs::write(session_dir.join("cache.bin"), vec![0u8; 128]).unwrap();
let flat_len = cache.path_for(&key).metadata().unwrap().len();
assert_eq!(cache.size_bytes().unwrap(), flat_len + 128);
}
#[test]
fn file_index_round_trips() {
use crate::document::ast::ParsedDoc;
use crate::index::file_index::FileIndex;
let dir = TempDir::new().unwrap();
let cache = WorkspaceCache::with_dir(dir.path().to_path_buf());
let src = "<?php\nnamespace App;\nclass Foo { public function bar(): string {} }";
let key = WorkspaceCache::key_for("file:///Foo.php", src);
let doc = ParsedDoc::parse(src.to_string());
let index = FileIndex::extract(&doc);
cache.write(&key, &index).unwrap();
let decoded: FileIndex = cache.read(&key).unwrap();
assert_eq!(decoded.namespace.as_deref(), Some("App"));
assert_eq!(decoded.classes.len(), 1);
assert_eq!(decoded.classes[0].name.as_ref(), "Foo");
assert_eq!(decoded.classes[0].methods.len(), 1);
assert_eq!(decoded.classes[0].methods[0].name.as_ref(), "bar");
}
#[test]
fn prune_stale_schema_dirs_removes_old_schemas_keeps_current_and_flat_files() {
let dir = TempDir::new().unwrap();
let php_lsp_dir = dir.path();
let stale = php_lsp_dir.join("0.21.0-fi-v3");
std::fs::create_dir_all(stale.join("workspacehash")).unwrap();
std::fs::write(stale.join("workspacehash").join("a.bin"), b"old").unwrap();
let current = php_lsp_dir.join("0.22.0-fi-v4");
std::fs::create_dir_all(¤t).unwrap();
std::fs::write(php_lsp_dir.join("php-binary-version.json"), b"{}").unwrap();
prune_stale_schema_dirs(php_lsp_dir, "0.22.0-fi-v4")
.expect("a stale dir is present, so a cleanup thread must be spawned")
.join()
.unwrap();
assert!(!stale.exists(), "stale schema directory must be removed");
assert!(current.exists(), "current schema directory must survive");
assert!(
php_lsp_dir.join("php-binary-version.json").exists(),
"flat sibling files must not be touched"
);
}
}