forgekit-core 0.5.0

Deterministic code intelligence SDK - Core library
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
451
452
453
//! Query caching layer with LRU eviction and TTL expiration.
//!
//! This module provides a thread-safe cache for query results to reduce
//! database load and improve response times.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

/// Cache entry with expiration time.
#[derive(Debug, Clone)]
struct CacheEntry<V> {
    /// The cached value.
    value: V,
    /// When this entry expires.
    expires_at: Instant,
}

/// Thread-safe query cache with LRU eviction.
///
/// The `QueryCache` stores query results with a TTL (time-to-live)
/// and evicts oldest entries when the cache is full.
///
/// # Examples
///
/// ```no_run
/// use forgekit_core::cache::QueryCache;
/// use std::time::Duration;
///
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// let cache = QueryCache::<String, String>::new(100, Duration::from_secs(300));
///
/// // Insert a value
/// cache.insert("key".to_string(), "value".to_string()).await;
///
/// // Retrieve it
/// if let Some(value) = cache.get(&"key".to_string()).await {
///     println!("Cached: {}", value);
/// }
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct QueryCache<K, V>
where
    K: Clone + Eq + std::hash::Hash + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Maximum number of entries.
    max_size: usize,
    /// Time-to-live for cache entries.
    ttl: Duration,
    /// The underlying cache store.
    inner: Arc<RwLock<CacheInner<K, V>>>,
}

/// Internal cache storage.
#[derive(Debug)]
struct CacheInner<K, V>
where
    K: Clone + Eq + std::hash::Hash,
    V: Clone,
{
    /// Map from key to cached entry.
    entries: HashMap<K, CacheEntry<V>>,
    /// Keys in insertion order (for FIFO eviction).
    keys: Vec<K>,
}

impl<K, V> QueryCache<K, V>
where
    K: Clone + Eq + std::hash::Hash + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    /// Creates a new query cache.
    ///
    /// # Arguments
    ///
    /// * `max_size` - Maximum number of entries before eviction
    /// * `ttl` - Time-to-live for cache entries
    pub fn new(max_size: usize, ttl: Duration) -> Self {
        Self {
            max_size,
            ttl,
            inner: Arc::new(RwLock::new(CacheInner {
                entries: HashMap::new(),
                keys: Vec::new(),
            })),
        }
    }

    /// Gets a cached value if it exists and hasn't expired.
    ///
    /// # Arguments
    ///
    /// * `key` - The cache key
    ///
    /// # Returns
    ///
    /// `Some(value)` if cached and valid, `None` otherwise.
    pub async fn get(&self, key: &K) -> Option<V> {
        let mut inner = self.inner.write().await;
        let now = Instant::now();

        // Clone key to avoid borrow issues
        let key_clone = key.clone();
        let value_opt = inner.entries.get(&key_clone).cloned();

        if let Some(entry) = value_opt {
            if now < entry.expires_at {
                // Touch key: move to end of list (LRU behavior)
                if let Some(pos) = inner.keys.iter().position(|k| k == &key_clone) {
                    inner.keys.remove(pos);
                    inner.keys.push(key_clone);
                }
                return Some(entry.value);
            } else {
                // Expired - remove it
                inner.entries.remove(&key_clone);
                if let Some(pos) = inner.keys.iter().position(|k| k == &key_clone) {
                    inner.keys.remove(pos);
                }
            }
        }
        None
    }

    /// Inserts a value into the cache.
    ///
    /// If the cache is full, the oldest entry is evicted (FIFO).
    ///
    /// # Arguments
    ///
    /// * `key` - The cache key
    /// * `value` - The value to cache
    pub async fn insert(&self, key: K, value: V) {
        let mut inner = self.inner.write().await;

        // If max_size is 0, don't insert anything
        if self.max_size == 0 {
            return;
        }

        // Check if we need to evict
        while inner.keys.len() >= self.max_size && !inner.keys.is_empty() {
            // Evict oldest (FIFO) - or first key
            if let Some(old_key) = inner.keys.first() {
                let old_key = old_key.clone();
                inner.keys.remove(0);
                inner.entries.remove(&old_key);
            }
        }

        let expires_at = Instant::now() + self.ttl;

        // Update or insert
        if !inner.keys.contains(&key) {
            inner.keys.push(key.clone());
        }
        inner.entries.insert(key, CacheEntry { value, expires_at });
    }

    /// Invalidates a specific cache entry.
    ///
    /// # Arguments
    ///
    /// * `key` - The cache key to invalidate
    pub async fn invalidate(&self, key: &K) {
        let mut inner = self.inner.write().await;
        inner.entries.remove(key);
        if let Some(pos) = inner.keys.iter().position(|k| k == key) {
            inner.keys.remove(pos);
        }
    }

    /// Clears all cached entries.
    pub async fn clear(&self) {
        let mut inner = self.inner.write().await;
        inner.entries.clear();
        inner.keys.clear();
    }

    /// Returns the current number of cached entries.
    pub async fn len(&self) -> usize {
        let inner = self.inner.read().await;
        inner.entries.len()
    }

    /// Returns true if the cache is empty.
    pub async fn is_empty(&self) -> bool {
        let inner = self.inner.read().await;
        inner.entries.is_empty()
    }
}

impl<K, V> std::fmt::Debug for QueryCache<K, V>
where
    K: Clone + Eq + std::hash::Hash + Send + Sync + 'static,
    V: Clone + Send + Sync + 'static,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("QueryCache")
            .field("max_size", &self.max_size)
            .field("ttl", &self.ttl)
            .field("inner", &"<cache>")
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    #[tokio::test]
    async fn test_cache_insert_get() {
        let cache = QueryCache::new(10, Duration::from_secs(60));

        cache.insert("key1".to_string(), "value1".to_string()).await;
        let value = cache.get(&"key1".to_string()).await;

        assert_eq!(value, Some("value1".to_string()));
    }

    #[tokio::test]
    async fn test_cache_miss() {
        let cache = QueryCache::new(10, Duration::from_secs(60));

        let value: Option<String> = cache.get(&"nonexistent".to_string()).await;
        assert!(value.is_none());
    }

    #[tokio::test]
    async fn test_cache_expiration() {
        let cache = QueryCache::new(10, Duration::from_millis(50));

        cache.insert("key".to_string(), "value".to_string()).await;

        // Wait for expiration
        tokio::time::sleep(Duration::from_millis(100)).await;

        let value = cache.get(&"key".to_string()).await;
        assert!(value.is_none());
    }

    #[tokio::test]
    async fn test_cache_eviction() {
        let cache = QueryCache::new(2, Duration::from_secs(60));

        cache.insert("key1".to_string(), "value1".to_string()).await;
        cache.insert("key2".to_string(), "value2".to_string()).await;
        cache.insert("key3".to_string(), "value3".to_string()).await;

        // key1 should be evicted (FIFO)
        assert_eq!(cache.len().await, 2);
        assert!(cache.get(&"key1".to_string()).await.is_none());
        assert_eq!(
            cache.get(&"key2".to_string()).await,
            Some("value2".to_string())
        );
        assert_eq!(
            cache.get(&"key3".to_string()).await,
            Some("value3".to_string())
        );
    }

    #[tokio::test]
    async fn test_cache_invalidate() {
        let cache = QueryCache::new(10, Duration::from_secs(60));

        cache.insert("key".to_string(), "value".to_string()).await;
        cache.invalidate(&"key".to_string()).await;

        assert!(cache.get(&"key".to_string()).await.is_none());
        assert_eq!(cache.len().await, 0);
    }

    #[tokio::test]
    async fn test_cache_clear() {
        let cache = QueryCache::new(10, Duration::from_secs(60));

        cache.insert("key1".to_string(), "value1".to_string()).await;
        cache.insert("key2".to_string(), "value2".to_string()).await;
        cache.clear().await;

        assert!(cache.is_empty().await);
    }

    #[tokio::test]
    async fn test_cache_lru_touch() {
        let cache = QueryCache::new(3, Duration::from_secs(60));

        // Insert items 1, 2, 3
        cache.insert("key1".to_string(), "value1".to_string()).await;
        cache.insert("key2".to_string(), "value2".to_string()).await;
        cache.insert("key3".to_string(), "value3".to_string()).await;

        // Access item 1 (moves to end)
        let _ = cache.get(&"key1".to_string()).await;

        // Insert item 4 (causes eviction of oldest, which should be key2)
        cache.insert("key4".to_string(), "value4".to_string()).await;

        // Verify key2 is evicted (not key1 which was touched)
        assert_eq!(cache.len().await, 3);
        assert!(cache.get(&"key2".to_string()).await.is_none());
        assert_eq!(
            cache.get(&"key1".to_string()).await,
            Some("value1".to_string())
        );
        assert_eq!(
            cache.get(&"key3".to_string()).await,
            Some("value3".to_string())
        );
        assert_eq!(
            cache.get(&"key4".to_string()).await,
            Some("value4".to_string())
        );
    }

    #[tokio::test]
    async fn test_cache_update_existing() {
        let cache = QueryCache::new(10, Duration::from_millis(100));

        // Insert key with value A
        cache.insert("key".to_string(), "valueA".to_string()).await;

        // Wait partial TTL
        tokio::time::sleep(Duration::from_millis(50)).await;

        // Insert same key with value B (refreshes TTL)
        cache.insert("key".to_string(), "valueB".to_string()).await;

        // Immediately get key - should return B with fresh TTL
        assert_eq!(
            cache.get(&"key".to_string()).await,
            Some("valueB".to_string())
        );

        // Wait for original TTL to pass (100ms total)
        tokio::time::sleep(Duration::from_millis(60)).await;

        // Should still be valid because update refreshed TTL
        assert_eq!(
            cache.get(&"key".to_string()).await,
            Some("valueB".to_string())
        );
    }

    #[tokio::test]
    async fn test_cache_concurrent_access() {
        let cache = QueryCache::new(20, Duration::from_secs(60));
        let mut handles = vec![];

        // Spawn 10 tasks concurrently inserting different keys
        for i in 0..10 {
            let cache_clone = cache.clone();
            handles.push(tokio::spawn(async move {
                cache_clone
                    .insert(format!("key{}", i), format!("value{}", i))
                    .await;
            }));
        }

        // Wait for all to complete
        for handle in handles {
            handle.await.unwrap();
        }

        // Verify all 10 items are in cache
        assert_eq!(cache.len().await, 10);
        for i in 0..10 {
            assert_eq!(
                cache.get(&format!("key{}", i)).await,
                Some(format!("value{}", i))
            );
        }
    }

    #[tokio::test]
    async fn test_cache_stress_eviction() {
        let cache = QueryCache::new(5, Duration::from_secs(60));

        // Insert 100 items sequentially
        for i in 0..100 {
            cache
                .insert(format!("key{}", i), format!("value{}", i))
                .await;
        }

        // Verify only 5 items remain
        assert_eq!(cache.len().await, 5);

        // Verify remaining are the last 5 inserted
        assert!(cache.get(&"key0".to_string()).await.is_none());
        assert!(cache.get(&"key94".to_string()).await.is_none());
        assert_eq!(
            cache.get(&"key95".to_string()).await,
            Some("value95".to_string())
        );
        assert_eq!(
            cache.get(&"key96".to_string()).await,
            Some("value96".to_string())
        );
        assert_eq!(
            cache.get(&"key97".to_string()).await,
            Some("value97".to_string())
        );
        assert_eq!(
            cache.get(&"key98".to_string()).await,
            Some("value98".to_string())
        );
        assert_eq!(
            cache.get(&"key99".to_string()).await,
            Some("value99".to_string())
        );
    }

    #[tokio::test]
    async fn test_cache_zero_max_size() {
        let cache = QueryCache::new(0, Duration::from_secs(60));

        // Insert should not add entries
        cache.insert("key1".to_string(), "value1".to_string()).await;
        cache.insert("key2".to_string(), "value2".to_string()).await;

        // Verify len() always returns 0
        assert_eq!(cache.len().await, 0);
        assert!(cache.is_empty().await);
        assert!(cache.get(&"key1".to_string()).await.is_none());
        assert!(cache.get(&"key2".to_string()).await.is_none());
    }

    #[tokio::test]
    async fn test_cache_ttl_expiration() {
        let cache = QueryCache::new(10, Duration::from_millis(100));

        // Insert key
        cache.insert("key".to_string(), "value".to_string()).await;
        assert_eq!(cache.len().await, 1);

        // Wait for expiration
        tokio::time::sleep(Duration::from_millis(150)).await;

        // Verify get returns None (expired)
        assert!(cache.get(&"key".to_string()).await.is_none());

        // Verify len decreased
        assert_eq!(cache.len().await, 0);
    }
}