2025-12-30 02:08:26 +05:30
|
|
|
use crate::models::{CacheEntry, FileMetadata, OrganizationPlan};
|
2026-01-08 20:33:44 +05:30
|
|
|
use blake3::Hasher;
|
2025-12-28 19:15:53 +05:30
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::fs;
|
|
|
|
|
use std::path::Path;
|
|
|
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
/// Result of checking the cache - includes pre-fetched metadata to avoid double lookups
|
|
|
|
|
pub struct CacheCheckResult {
|
|
|
|
|
pub cached_response: Option<OrganizationPlan>,
|
|
|
|
|
pub file_metadata: HashMap<String, FileMetadata>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
|
pub struct Cache {
|
|
|
|
|
entries: HashMap<String, CacheEntry>,
|
2025-12-28 23:58:40 +05:30
|
|
|
max_entries: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for Cache {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Cache {
|
|
|
|
|
pub fn new() -> Self {
|
2025-12-28 23:58:40 +05:30
|
|
|
Self::with_max_entries(1000)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn with_max_entries(max_entries: usize) -> Self {
|
2025-12-28 19:15:53 +05:30
|
|
|
Self {
|
|
|
|
|
entries: HashMap::new(),
|
2025-12-28 23:58:40 +05:30
|
|
|
max_entries,
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load_or_create(cache_path: &Path) -> Self {
|
|
|
|
|
if cache_path.exists() {
|
|
|
|
|
match fs::read_to_string(cache_path) {
|
2025-12-29 00:54:29 +05:30
|
|
|
Ok(content) => match serde_json::from_str::<Cache>(&content) {
|
|
|
|
|
Ok(cache) => {
|
|
|
|
|
println!("Loaded cache with {} entries", cache.entries.len());
|
|
|
|
|
cache
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
2025-12-29 00:54:29 +05:30
|
|
|
Err(_) => {
|
|
|
|
|
println!("Cache corrupted, creating new cache");
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
},
|
2025-12-28 19:15:53 +05:30
|
|
|
Err(_) => {
|
|
|
|
|
println!("Failed to read cache, creating new cache");
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
println!("Creating new cache file");
|
|
|
|
|
Self::new()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn save(&self, cache_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
|
if let Some(parent) = cache_path.parent() {
|
|
|
|
|
fs::create_dir_all(parent)?;
|
|
|
|
|
}
|
2025-12-29 00:54:29 +05:30
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
let content = serde_json::to_string_pretty(self)?;
|
|
|
|
|
fs::write(cache_path, content)?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
/// Checks cache and returns pre-fetched metadata to avoid double lookups.
|
|
|
|
|
/// The returned metadata can be passed to `cache_response_with_metadata` on cache miss.
|
|
|
|
|
pub fn check_cache(&self, filenames: &[String], base_path: &Path) -> CacheCheckResult {
|
|
|
|
|
// Fetch metadata once for all files
|
|
|
|
|
let file_metadata: HashMap<String, FileMetadata> = filenames
|
|
|
|
|
.iter()
|
|
|
|
|
.filter_map(|filename| {
|
|
|
|
|
let file_path = base_path.join(filename);
|
|
|
|
|
Self::get_file_metadata(&file_path)
|
|
|
|
|
.ok()
|
|
|
|
|
.map(|m| (filename.clone(), m))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
let cache_key = self.generate_cache_key(filenames);
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
let cached_response = self.entries.get(&cache_key).and_then(|entry| {
|
|
|
|
|
// Validate all files are unchanged using pre-fetched metadata
|
|
|
|
|
let all_unchanged = filenames.iter().all(|filename| {
|
|
|
|
|
match (
|
|
|
|
|
file_metadata.get(filename),
|
|
|
|
|
entry.file_metadata.get(filename),
|
|
|
|
|
) {
|
|
|
|
|
(Some(current), Some(cached)) => current == cached,
|
|
|
|
|
_ => false,
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
2026-01-08 23:18:39 +05:30
|
|
|
});
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
if all_unchanged {
|
2025-12-28 19:15:53 +05:30
|
|
|
println!("Using cached response (timestamp: {})", entry.timestamp);
|
2026-01-08 23:18:39 +05:30
|
|
|
Some(entry.response.clone())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
2026-01-08 23:18:39 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
|
|
CacheCheckResult {
|
|
|
|
|
cached_response,
|
|
|
|
|
file_metadata,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Cache response using pre-fetched metadata (avoids double metadata lookup)
|
|
|
|
|
pub fn cache_response_with_metadata(
|
|
|
|
|
&mut self,
|
|
|
|
|
filenames: &[String],
|
|
|
|
|
response: OrganizationPlan,
|
|
|
|
|
file_metadata: HashMap<String, FileMetadata>,
|
|
|
|
|
) {
|
|
|
|
|
let cache_key = self.generate_cache_key(filenames);
|
|
|
|
|
|
|
|
|
|
let timestamp = SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.as_secs();
|
|
|
|
|
|
|
|
|
|
let entry = CacheEntry {
|
|
|
|
|
response,
|
|
|
|
|
timestamp,
|
|
|
|
|
file_metadata,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.entries.insert(cache_key, entry);
|
|
|
|
|
|
|
|
|
|
if self.entries.len() > self.max_entries {
|
|
|
|
|
self.evict_oldest();
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
println!("Cached response for {} files", filenames.len());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Legacy method - checks cache for a response (fetches metadata internally)
|
|
|
|
|
#[deprecated(
|
|
|
|
|
note = "Use check_cache() + cache_response_with_metadata() to avoid double metadata lookups"
|
|
|
|
|
)]
|
|
|
|
|
pub fn get_cached_response(
|
|
|
|
|
&self,
|
|
|
|
|
filenames: &[String],
|
|
|
|
|
base_path: &Path,
|
|
|
|
|
) -> Option<OrganizationPlan> {
|
|
|
|
|
let result = self.check_cache(filenames, base_path);
|
|
|
|
|
result.cached_response
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
|
|
|
|
|
2026-01-08 23:18:39 +05:30
|
|
|
/// Legacy method - caches a response (fetches metadata internally)
|
|
|
|
|
#[deprecated(note = "Use cache_response_with_metadata() with pre-fetched metadata")]
|
2025-12-28 23:58:40 +05:30
|
|
|
pub fn cache_response(
|
|
|
|
|
&mut self,
|
|
|
|
|
filenames: &[String],
|
|
|
|
|
response: OrganizationPlan,
|
|
|
|
|
base_path: &Path,
|
|
|
|
|
) {
|
2025-12-28 19:15:53 +05:30
|
|
|
let cache_key = self.generate_cache_key(filenames);
|
2025-12-28 23:58:40 +05:30
|
|
|
let mut file_metadata = HashMap::new();
|
|
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
for filename in filenames {
|
|
|
|
|
let file_path = base_path.join(filename);
|
2025-12-28 23:58:40 +05:30
|
|
|
if let Ok(metadata) = Self::get_file_metadata(&file_path) {
|
|
|
|
|
file_metadata.insert(filename.clone(), metadata);
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
|
|
|
|
}
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
let timestamp = SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.as_secs();
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
let entry = CacheEntry {
|
|
|
|
|
response,
|
|
|
|
|
timestamp,
|
2025-12-28 23:58:40 +05:30
|
|
|
file_metadata,
|
2025-12-28 19:15:53 +05:30
|
|
|
};
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
self.entries.insert(cache_key, entry);
|
2025-12-28 23:58:40 +05:30
|
|
|
|
|
|
|
|
if self.entries.len() > self.max_entries {
|
|
|
|
|
self.evict_oldest();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
println!("Cached response for {} files", filenames.len());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generate_cache_key(&self, filenames: &[String]) -> String {
|
|
|
|
|
let mut sorted_filenames = filenames.to_vec();
|
|
|
|
|
sorted_filenames.sort();
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2026-01-08 20:33:44 +05:30
|
|
|
let mut hasher = Hasher::new();
|
2025-12-28 19:15:53 +05:30
|
|
|
for filename in &sorted_filenames {
|
|
|
|
|
hasher.update(filename.as_bytes());
|
|
|
|
|
hasher.update(b"|");
|
|
|
|
|
}
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2026-01-08 20:33:44 +05:30
|
|
|
hasher.finalize().to_hex().to_string()
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
|
|
|
|
|
2025-12-28 23:58:40 +05:30
|
|
|
fn get_file_metadata(file_path: &Path) -> Result<FileMetadata, Box<dyn std::error::Error>> {
|
|
|
|
|
let metadata = fs::metadata(file_path)?;
|
2025-12-29 00:54:29 +05:30
|
|
|
let modified = metadata.modified()?.duration_since(UNIX_EPOCH)?.as_secs();
|
2025-12-28 23:58:40 +05:30
|
|
|
|
|
|
|
|
Ok(FileMetadata {
|
|
|
|
|
size: metadata.len(),
|
|
|
|
|
modified,
|
|
|
|
|
})
|
2025-12-28 19:15:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn cleanup_old_entries(&mut self, max_age_seconds: u64) {
|
|
|
|
|
let current_time = SystemTime::now()
|
|
|
|
|
.duration_since(UNIX_EPOCH)
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.as_secs();
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
let initial_count = self.entries.len();
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2025-12-29 00:54:29 +05:30
|
|
|
self.entries
|
|
|
|
|
.retain(|_, entry| current_time - entry.timestamp < max_age_seconds);
|
2025-12-28 23:58:40 +05:30
|
|
|
|
2025-12-28 19:15:53 +05:30
|
|
|
let removed_count = initial_count - self.entries.len();
|
|
|
|
|
if removed_count > 0 {
|
|
|
|
|
println!("Cleaned up {} old cache entries", removed_count);
|
|
|
|
|
}
|
2025-12-28 23:58:40 +05:30
|
|
|
|
|
|
|
|
if self.entries.len() > self.max_entries {
|
|
|
|
|
self.compact_cache();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn evict_oldest(&mut self) {
|
|
|
|
|
if let Some(oldest_key) = self
|
|
|
|
|
.entries
|
|
|
|
|
.iter()
|
|
|
|
|
.min_by_key(|(_, entry)| entry.timestamp)
|
|
|
|
|
.map(|(k, _)| k.clone())
|
|
|
|
|
{
|
|
|
|
|
self.entries.remove(&oldest_key);
|
|
|
|
|
println!("Evicted oldest cache entry to maintain limit");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compact_cache(&mut self) {
|
|
|
|
|
while self.entries.len() > self.max_entries {
|
|
|
|
|
self.evict_oldest();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|