ricecoder-storage 0.1.71

Storage and configuration management for RiceCoder
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Concrete caching implementations for ricecoder
//!
//! This module provides ready-to-use caching implementations for common operations:
//! - Configuration caching
//! - Specification caching
//! - Provider response caching
//! - Project analysis caching

use crate::CacheManager;
use std::path::Path;
use tracing::{debug, info};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use serde::Serialize;

/// Cache statistics tracker
#[derive(Debug, Clone)]
pub struct CacheStats {
    hits: Arc<AtomicU64>,
    misses: Arc<AtomicU64>,
}

impl CacheStats {
    /// Create new cache statistics tracker
    pub fn new() -> Self {
        Self {
            hits: Arc::new(AtomicU64::new(0)),
            misses: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Record a cache hit
    pub fn record_hit(&self) {
        self.hits.fetch_add(1, Ordering::Relaxed);
    }

    /// Record a cache miss
    pub fn record_miss(&self) {
        self.misses.fetch_add(1, Ordering::Relaxed);
    }

    /// Get cache hit rate (0.0 to 1.0)
    pub fn hit_rate(&self) -> f64 {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let total = hits + misses;

        if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        }
    }

    /// Get statistics tuple (hits, misses, hit_rate)
    pub fn stats(&self) -> (u64, u64, f64) {
        let hits = self.hits.load(Ordering::Relaxed);
        let misses = self.misses.load(Ordering::Relaxed);
        let rate = self.hit_rate();
        (hits, misses, rate)
    }

    /// Log cache statistics
    pub fn log_stats(&self, name: &str) {
        let (hits, misses, rate) = self.stats();
        info!(
            "{} cache statistics: {} hits, {} misses, {:.2}% hit rate",
            name,
            hits,
            misses,
            rate * 100.0
        );
    }

    /// Reset statistics
    pub fn reset(&self) {
        self.hits.store(0, Ordering::Relaxed);
        self.misses.store(0, Ordering::Relaxed);
    }
}

impl Default for CacheStats {
    fn default() -> Self {
        Self::new()
    }
}

/// Configuration caching wrapper
pub struct ConfigCache {
    cache: CacheManager,
    stats: CacheStats,
}

impl ConfigCache {
    /// Create new configuration cache
    pub fn new(cache_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Self {
            cache: CacheManager::new(cache_dir)?,
            stats: CacheStats::new(),
        })
    }

    /// Get cached configuration or load from file
    pub fn get_config<T: serde::de::DeserializeOwned + Serialize>(
        &self,
        path: &Path,
    ) -> Result<T, Box<dyn std::error::Error>> {
        let cache_key = format!("config_{}", path.display());

        // Check cache first
        if let Some(cached) = self.cache.get(&cache_key)? {
            debug!("Configuration cache hit: {}", path.display());
            self.stats.record_hit();
            return Ok(serde_json::from_str(&cached)?);
        }

        debug!("Configuration cache miss: {}", path.display());
        self.stats.record_miss();

        // Load and parse configuration
        let content = std::fs::read_to_string(path)?;
        let config: T = serde_yaml::from_str(&content)?;

        // Cache for 1 hour (3600 seconds)
        let json = serde_json::to_string(&config)?;
        self.cache.set(
            &cache_key,
            json,
            crate::CacheInvalidationStrategy::Ttl(3600),
        )?;

        Ok(config)
    }

    /// Invalidate configuration cache
    pub fn invalidate_config(&self, path: &Path) -> Result<(), Box<dyn std::error::Error>> {
        let cache_key = format!("config_{}", path.display());
        self.cache.invalidate(&cache_key)?;
        debug!("Configuration cache invalidated: {}", path.display());
        Ok(())
    }

    /// Get cache statistics
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }
}

/// Specification caching wrapper
pub struct SpecCache {
    cache: CacheManager,
    stats: CacheStats,
}

impl SpecCache {
    /// Create new specification cache
    pub fn new(cache_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Self {
            cache: CacheManager::new(cache_dir)?,
            stats: CacheStats::new(),
        })
    }

    /// Get cached specification or load from file
    pub fn get_spec<T: serde::de::DeserializeOwned + Serialize>(
        &self,
        path: &Path,
    ) -> Result<T, Box<dyn std::error::Error>> {
        let cache_key = format!("spec_{}", path.display());

        // Check cache first
        if let Some(cached) = self.cache.get(&cache_key)? {
            debug!("Specification cache hit: {}", path.display());
            self.stats.record_hit();
            return Ok(serde_json::from_str(&cached)?);
        }

        debug!("Specification cache miss: {}", path.display());
        self.stats.record_miss();

        // Load and parse specification
        let content = std::fs::read_to_string(path)?;
        let spec: T = serde_yaml::from_str(&content)?;

        // Cache for 1 hour (3600 seconds)
        let json = serde_json::to_string(&spec)?;
        self.cache.set(
            &cache_key,
            json,
            crate::CacheInvalidationStrategy::Ttl(3600),
        )?;

        Ok(spec)
    }

    /// Invalidate specification cache
    pub fn invalidate_spec(&self, path: &Path) -> Result<(), Box<dyn std::error::Error>> {
        let cache_key = format!("spec_{}", path.display());
        self.cache.invalidate(&cache_key)?;
        debug!("Specification cache invalidated: {}", path.display());
        Ok(())
    }

    /// Get cache statistics
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }
}

/// Provider response caching wrapper
pub struct ProviderCache {
    cache: CacheManager,
    stats: CacheStats,
}

impl ProviderCache {
    /// Create new provider response cache
    pub fn new(cache_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Self {
            cache: CacheManager::new(cache_dir)?,
            stats: CacheStats::new(),
        })
    }

    /// Get cached provider response
    pub fn get_response(
        &self,
        provider: &str,
        model: &str,
        prompt: &str,
    ) -> Result<Option<String>, Box<dyn std::error::Error>> {
        let cache_key = self.make_cache_key(provider, model, prompt);

        // Check cache first
        if let Some(cached) = self.cache.get(&cache_key)? {
            debug!("Provider response cache hit: {}/{}", provider, model);
            self.stats.record_hit();
            return Ok(Some(cached));
        }

        debug!("Provider response cache miss: {}/{}", provider, model);
        self.stats.record_miss();
        Ok(None)
    }

    /// Cache provider response
    pub fn cache_response(
        &self,
        provider: &str,
        model: &str,
        prompt: &str,
        response: &str,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let cache_key = self.make_cache_key(provider, model, prompt);

        // Cache for 24 hours (86400 seconds)
        self.cache.set(
            &cache_key,
            response.to_string(),
            crate::CacheInvalidationStrategy::Ttl(86400),
        )?;

        debug!("Provider response cached: {}/{}", provider, model);
        Ok(())
    }

    /// Make cache key from provider, model, and prompt
    fn make_cache_key(&self, provider: &str, model: &str, prompt: &str) -> String {
        // Use simple hash of prompt to avoid long keys
        // Calculate a simple hash by summing byte values
        let hash = prompt
            .bytes()
            .fold(0u64, |acc, b| acc.wrapping_mul(31).wrapping_add(b as u64));

        format!("provider_{}_{}_{}",provider, model, hash)
    }

    /// Get cache statistics
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }
}

/// Project analysis caching wrapper
pub struct ProjectAnalysisCache {
    cache: CacheManager,
    stats: CacheStats,
}

impl ProjectAnalysisCache {
    /// Create new project analysis cache
    pub fn new(cache_dir: &Path) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(Self {
            cache: CacheManager::new(cache_dir)?,
            stats: CacheStats::new(),
        })
    }

    /// Get cached project analysis
    pub fn get_analysis<T: serde::de::DeserializeOwned + Serialize>(
        &self,
        project_path: &Path,
    ) -> Result<Option<T>, Box<dyn std::error::Error>> {
        let cache_key = format!("analysis_{}", project_path.display());

        if let Some(cached) = self.cache.get(&cache_key)? {
            debug!("Project analysis cache hit: {}", project_path.display());
            self.stats.record_hit();
            return Ok(Some(serde_json::from_str(&cached)?));
        }

        debug!("Project analysis cache miss: {}", project_path.display());
        self.stats.record_miss();
        Ok(None)
    }

    /// Cache project analysis
    pub fn cache_analysis<T: serde::Serialize>(
        &self,
        project_path: &Path,
        analysis: &T,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let cache_key = format!("analysis_{}", project_path.display());

        // Cache for 1 hour (3600 seconds)
        let json = serde_json::to_string(analysis)?;
        self.cache.set(
            &cache_key,
            json,
            crate::CacheInvalidationStrategy::Ttl(3600),
        )?;

        debug!("Project analysis cached: {}", project_path.display());
        Ok(())
    }

    /// Invalidate project analysis cache
    pub fn invalidate_analysis(&self, project_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
        let cache_key = format!("analysis_{}", project_path.display());
        self.cache.invalidate(&cache_key)?;
        debug!("Project analysis cache invalidated: {}", project_path.display());
        Ok(())
    }

    /// Get cache statistics
    pub fn stats(&self) -> &CacheStats {
        &self.stats
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_cache_stats() {
        let stats = CacheStats::new();
        
        stats.record_hit();
        stats.record_hit();
        stats.record_miss();
        
        let (hits, misses, rate) = stats.stats();
        assert_eq!(hits, 2);
        assert_eq!(misses, 1);
        assert!((rate - 2.0/3.0).abs() < 0.01);
    }

    #[test]
    fn test_cache_stats_reset() {
        let stats = CacheStats::new();
        
        stats.record_hit();
        stats.record_miss();
        stats.reset();
        
        let (hits, misses, _) = stats.stats();
        assert_eq!(hits, 0);
        assert_eq!(misses, 0);
    }

    #[test]
    fn test_config_cache() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;
        let cache_dir = temp_dir.path().join("cache");
        std::fs::create_dir(&cache_dir)?;

        let config_path = temp_dir.path().join("config.yaml");
        std::fs::write(&config_path, "key: value")?;

        let cache = ConfigCache::new(&cache_dir)?;
        
        // First access: miss
        let _: serde_json::Value = cache.get_config(&config_path)?;
        assert_eq!(cache.stats().stats().1, 1); // 1 miss

        // Second access: hit
        let _: serde_json::Value = cache.get_config(&config_path)?;
        assert_eq!(cache.stats().stats().0, 1); // 1 hit

        Ok(())
    }

    #[test]
    fn test_spec_cache() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;
        let cache_dir = temp_dir.path().join("cache");
        std::fs::create_dir(&cache_dir)?;

        let spec_path = temp_dir.path().join("spec.yaml");
        std::fs::write(&spec_path, "name: test")?;

        let cache = SpecCache::new(&cache_dir)?;
        
        // First access: miss
        let _: serde_json::Value = cache.get_spec(&spec_path)?;
        assert_eq!(cache.stats().stats().1, 1); // 1 miss

        // Second access: hit
        let _: serde_json::Value = cache.get_spec(&spec_path)?;
        assert_eq!(cache.stats().stats().0, 1); // 1 hit

        Ok(())
    }

    #[test]
    fn test_provider_cache() -> Result<(), Box<dyn std::error::Error>> {
        let temp_dir = TempDir::new()?;
        let cache_dir = temp_dir.path().join("cache");
        std::fs::create_dir(&cache_dir)?;

        let cache = ProviderCache::new(&cache_dir)?;
        
        // First access: miss
        let result = cache.get_response("openai", "gpt-4", "hello")?;
        assert!(result.is_none());
        assert_eq!(cache.stats().stats().1, 1); // 1 miss

        // Cache response
        cache.cache_response("openai", "gpt-4", "hello", "world")?;

        // Second access: hit
        let result = cache.get_response("openai", "gpt-4", "hello")?;
        assert_eq!(result, Some("world".to_string()));
        assert_eq!(cache.stats().stats().0, 1); // 1 hit

        Ok(())
    }
}