impl CacheStrategy for AstCacheStrategy {
type Key = PathBuf;
type Value = FileContext;
fn cache_key(&self, path: &PathBuf) -> String {
let mtime = fs::metadata(path)
.ok()
.and_then(|m| m.modified().ok())
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map_or(0, |d| d.as_secs());
format!("ast:{}:{}", path.display(), mtime)
}
fn validate(&self, path: &PathBuf, cached: &FileContext) -> bool {
if !path.exists() {
return false;
}
let current_mtime = fs::metadata(path)
.ok()
.and_then(|m| m.modified().ok())
.and_then(|t| t.duration_since(UNIX_EPOCH).ok())
.map_or(0, |d| d.as_secs());
let cached_path = PathBuf::from(&cached.path);
if cached_path != *path {
return false;
}
if let Ok(cached_metadata) = fs::metadata(&cached.path) {
if let Ok(cached_modified) = cached_metadata.modified() {
if let Ok(cached_duration) = cached_modified.duration_since(UNIX_EPOCH) {
let file_mtime = cached_duration.as_secs();
return current_mtime == file_mtime;
}
}
}
false
}
fn ttl(&self) -> Option<Duration> {
Some(Duration::from_secs(300)) }
fn max_size(&self) -> usize {
100 }
}