amberjs 1.17.0

JavaScript/TypeScript runtime built with Rust and V8
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Edge Caching Strategy
// Multi-layer intelligent caching for optimal performance

use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex, RwLock};
use tokio::time::{TokioDuration, TokioInstant};
use anyhow::{Result, Error};
use std::time::{Duration, Instant};
use std::time::SystemTime;

/// Multi-layer edge cache
#[derive(Debug)]
pub struct EdgeCache {
    name: String,
    l1_cache: Arc<RwLock<L1Cache>>,          // L1: Edge node cache (fastest)
    l2_cache: Arc<RwLock<L2Cache>>,          // L2: Regional cache
    l3_cache: Arc<RwLock<L3Cache>>,          // L3: Central data center
    predictor: Arc<RwLock<CachePredictor>>,   // AI-powered prediction
    stats: Arc<RwLock<CacheStats>>,
}
#[derive(Debug)]
struct L1Cache {
    capacity: usize,
    data: HashMap<String, CacheEntry>,
    access_order: Vec<String>, // LRU tracking
}
#[derive(Debug, Clone)]
struct CacheEntry {
    key: String,
    value: Vec<u8>,
    timestamp: std::time::SystemTime,
    ttl: Duration,
    access_count: u64,
}
#[derive(Debug)]
struct L2Cache {
    region: String,
    capacity: usize,
    data: HashMap<String, CacheEntry>,
}
#[derive(Debug)]
struct L3Cache {
    endpoint: String,
    capacity: usize,
    data: HashMap<String, CacheEntry>,
}
#[derive(Debug)]
struct CachePredictor {
    access_patterns: HashMap<String, Vec<String>>,
    predictions: HashMap<String, Vec<String>>,
}
#[derive(Debug, Clone)]
pub struct CacheStats {
    pub l1_hits: u64,
    pub l1_misses: u64,
    pub l2_hits: u64,
    pub l2_misses: u64,
    pub l3_hits: u64,
    pub l3_misses: u64,
    pub total_operations: u64,
    pub hit_ratio: f64,
}
impl EdgeCache {
    /// Create a new edge cache with specified layers
    pub fn new(name: &str, l1_capacity: usize) -> Result<Self> {
        Ok(EdgeCache {
            name: name.to_string(),
            l1_cache: Arc::new(Mutex::new(L1Cache::new(l1_capacity)))
            l2_cache: Arc::new(Mutex::new(L2Cache::new("regional", l1_capacity * 10)),?)),
            l3_cache: Arc::new(Mutex::new(L3Cache::new("central", l1_capacity * 100)),?)),
            predictor: Arc::new(Mutex::new(CachePredictor::new()))
            stats: Arc::new(Mutex::new(CacheStats {)),
                l1_hits: 0,
                l1_misses: 0,
                l2_hits: 0,
                l2_misses: 0,
                l3_hits: 0,
                l3_misses: 0,
                total_operations: 0,
                hit_ratio: 0.0,
            }))
        })
    }
    /// Set a value in the cache
    pub async fn set(&self, key: &str, value: &[u8]) -> Result<()> {
        let entry: _ = CacheEntry {
            key: key.to_string(),
            value: value.to_vec(),
            timestamp: std::time::SystemTime::now(),
            ttl: Duration::from_secs(300), // 5 minutes default TTL
            access_count: 0,
        };
        // Try L1 cache first
        {
            let mut l1 = self.l1_cache.write().await;
            if let Err(_) = l1.insert(key, entry.clone()) {
                // L1 is full, need to evict
                l1.evict_lru();
                l1.insert(key, entry.clone())?;
            }
        }
        // Also store in L2
        {
            let mut l2 = self.l2_cache.write().await;
            let _: _ = l2.insert(key, entry.clone());
        }
        // Also store in L3
        {
            let mut l3 = self.l3_cache.write().await;
            let _: _ = l3.insert(key, entry.clone());
        }
        Ok(())
    }
    /// Get a value from the cache (multi-layer)
    pub async fn get(&self, key: &str) -> Result<Option<Vec<u8> {
        let _start: _ = Instant::now();
        // Try L1 cache first
        {
            let mut l1 = self.l1_cache.write().await;
            if let Some(entry) = l1.get(key)? {
                // Update stats
                self.update_stats(true, true, false, false, false, false).await;
                // Record access pattern for prediction
                {
                    let mut predictor = self.predictor.write().await;
                    predictor.record_access(key);
                }
                return Ok(Some(entry.value));
            }
        }
        // L1 miss, try L2
        {
            let mut l2 = self.l2_cache.write().await;
            if let Some(entry) = l2.get(key)? {
                // Promote to L1
                {
                    let mut l1 = self.l1_cache.write().await;
                    let _: _ = l1.insert(key, entry.clone());
                }
                // Update stats
                self.update_stats(false, false, true, false, false, false).await;
                return Ok(Some(entry.value));
            }
        }
        // L2 miss, try L3
        {
            let mut l3 = self.l3_cache.write().await;
            if let Some(entry) = l3.get(key)? {
                // Promote to L2 and L1
                {
                    let mut l2 = self.l2_cache.write().await;
                    let _: _ = l2.insert(key, entry.clone());
                }
                {
                    let mut l1 = self.l1_cache.write().await;
                    let _: _ = l1.insert(key, entry.clone());
                }
                // Update stats
                self.update_stats(false, false, false, false, true, false).await;
                return Ok(Some(entry.value));
            }
        }
        // Cache miss at all levels
        self.update_stats(false, false, false, false, false, true).await;
        Ok(None)
    }
    /// Invalidate a specific key
    pub async fn invalidate(&self, key: &str) -> Result<()> {
        // Remove from all layers
        {
            let mut l1 = self.l1_cache.write().await;
            let _: _ = l1.remove(key);
        }
        {
            let mut l2 = self.l2_cache.write().await;
            let _: _ = l2.remove(key);
        }
        {
            let mut l3 = self.l3_cache.write().await;
            let _: _ = l3.remove(key);
        }
        Ok(())
    }
    /// Get cache statistics
    pub async fn get_stats(&self) -> Result<CacheStats> {
        let stats: _ = self.stats.read().await;
        Ok(stats.clone())
    }
    /// Update cache statistics
    async fn update_stats(
        &self,
        l1_hit: bool,
        l1_miss: bool,
        l2_hit: bool,
        l2_miss: bool,
        l3_hit: bool,
        l3_miss: bool,
    ) {
        let mut stats = self.stats.write().await;
        stats.total_operations += 1;
        if l1_hit { stats.l1_hits += 1; }
        if l1_miss { stats.l1_misses += 1; }
        if l2_hit { stats.l2_hits += 1; }
        if l2_miss { stats.l2_misses += 1; }
        if l3_hit { stats.l3_hits += 1; }
        if l3_miss { stats.l3_misses += 1; }
        // Calculate hit ratio
        let total_hits: _ = stats.l1_hits + stats.l2_hits + stats.l3_hits;
        stats.hit_ratio = if stats.total_operations > 0 {
            total_hits as f64 / stats.total_operations as f64
        } else {
            0.0
        };
    }
    /// Clear all caches
    pub async fn clear(&self) -> Result<()> {
        {
            let mut l1 = self.l1_cache.write().await;
            l1.clear();
        }
        {
            let mut l2 = self.l2_cache.write().await;
            l2.clear();
        }
        {
            let mut l3 = self.l3_cache.write().await;
            l3.clear();
        }
        Ok(())
    }
    /// Preload frequently accessed items
    pub async fn preload(&self, keys: &[String]) -> Result<()> {
        for key in keys {
            // Check if key exists, if not we might want to fetch it
            let _: _ = self.get(key).await;
        }
        Ok(())
    }
}
/// L1 Cache Implementation (Edge Node)
impl L1Cache {
    fn new(capacity: usize) -> Self {
        L1Cache {
            capacity,
            data: HashMap::new(),
            access_order: Vec::new(),
        }
    }
    fn insert(&mut self, key: &str, entry: CacheEntry) -> Result<()> {
        if self.data.len() >= self.capacity && !self.data.contains_key(key) {
            return Err(anyhow::anyhow!("Cache is full"));
        }
        self.data.insert(key.to_string(), entry);
        self.update_access_order(key);
        Ok(())
    }
    fn get(&mut self, key: &str) -> Result<Option<CacheEntry>> {
        if let Some(entry) = self.data.get_mut(key) {
            entry.access_count += 1;
        }
        self.update_access_order(key);
        Ok(self.data.get(key).cloned())
    }
    fn remove(&mut self, key: &str) -> Result<()> {
        self.data.remove(key);
        self.access_order.retain(|k| k != key);
        Ok(())
    }
    fn update_access_order(&mut self, key: &str) {
        self.access_order.retain(|k| k != key);
        self.access_order.push(key.to_string());
    }
    fn evict_lru(&mut self) {
        if let Some(lru_key) = self.access_order.first().cloned() {
            self.data.remove(&lru_key);
            self.access_order.remove(0);
        }
    }
    fn clear(&mut self) {
        self.data.clear();
        self.access_order.clear();
    }
}
/// L2 Cache Implementation (Regional)
impl L2Cache {
    fn new(region: &str, capacity: usize) -> Result<Self> {
        Ok(L2Cache {
            region: region.to_string(),
            capacity,
            data: HashMap::new(),
        })
    }
    fn insert(&mut self, key: &str, entry: CacheEntry) -> Result<()> {
        if self.data.len() >= self.capacity {
            // Simple eviction: remove oldest entry
            let oldest_key: _ = self.data.keys().next().unwrap().clone();
            self.data.remove(&oldest_key);
        }
        self.data.insert(key.to_string(), entry);
        Ok(())
    }
    fn get(&mut self, key: &str) -> Result<Option<CacheEntry>> {
        Ok(self.data.get(key).cloned())
    }
    fn remove(&mut self, key: &str) -> Result<()> {
        self.data.remove(key);
        Ok(())
    }
    fn clear(&mut self) {
        self.data.clear();
    }
}
/// L3 Cache Implementation (Central Data Center)
impl L3Cache {
    fn new(endpoint: &str, capacity: usize) -> Result<Self> {
        Ok(L3Cache {
            endpoint: endpoint.to_string(),
            capacity,
            data: HashMap::new(),
        })
    }
    fn insert(&mut self, key: &str, entry: CacheEntry) -> Result<()> {
        if self.data.len() >= self.capacity {
            // LRU eviction
            let oldest_key: _ = self.data.keys().next().unwrap().clone();
            self.data.remove(&oldest_key);
        }
        self.data.insert(key.to_string(), entry);
        Ok(())
    }
    fn get(&mut self, key: &str) -> Result<Option<CacheEntry>> {
        Ok(self.data.get(key).cloned())
    }
    fn remove(&mut self, key: &str) -> Result<()> {
        self.data.remove(key);
        Ok(())
    }
    fn clear(&mut self) {
        self.data.clear();
    }
}
/// Cache Predictor for intelligent preloading
impl CachePredictor {
    fn new() -> Self {
        CachePredictor {
            access_patterns: HashMap::new(),
            predictions: HashMap::new(),
        }
    }
    fn record_access(&mut self, key: &str) {
        // Record access pattern
        let pattern: _ = self.access_patterns.entry(key.to_string()).or_insert_with(Vec::new);
        pattern.push(format!("access-{}", std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH).unwrap().as_secs());
        // Keep only last 100 accesses
        if pattern.len() > 100 {
            pattern.remove(0);
        }
    }
    fn predict(&mut self, recent_accesses: &[String]) -> Result<Vec<String> {
        let mut predictions = Vec::new();
        // Simple prediction: if keys are accessed together, predict they will be accessed together
        for key in recent_accesses {
            if let Some(pattern) = self.access_patterns.get(key) {
                // Extract patterns and make predictions
                predictions.push(key.clone());
            }
        }
        Ok(predictions)
    }
}
#[cfg(test)]
mod tests {
    #[tokio::test]
    async fn test_edge_cache_creation() {
        let cache: _ = EdgeCache::new("test-cache", 100);
        assert!(cache.is_ok());
    }
    #[tokio::test]
    async fn test_l1_cache_set_get() {
        let cache: _ = EdgeCache::new("test-cache", 100).unwrap();
        cache.set("key1", b"value1").await.unwrap();
        let result: _ = cache.get("key1").await.unwrap();
        assert_eq!(result, Some(b"value1".to_vec());
    }
    #[tokio::test]
    async fn test_cache_invalidation() {
        let cache: _ = EdgeCache::new("test-cache", 100).unwrap();
        cache.set("key1", b"value1").await.unwrap();
        cache.invalidate("key1").await.unwrap();
        let result: _ = cache.get("key1").await.unwrap();
        assert_eq!(result, None);
    }
    #[tokio::test]
    async fn test_cache_hit_ratio() {
        let cache: _ = EdgeCache::new("test-cache", 100).unwrap();
        // Add items to cache
        for i in 0..100 {
            cache.set(&format!("key_{}", i), b"value").await.unwrap();
        }
        // Access items
        for i in 0..100 {
            cache.get(&format!("key_{}", i)).await.unwrap();
        }
        let stats: _ = cache.get_stats().await.unwrap();
        assert!(stats.hit_ratio > 0.95);
    }
    #[tokio::test]
    async fn test_multi_layer_cache() {
        let cache: _ = EdgeCache::new("test-cache", 10).unwrap();
        // Set a value
        cache.set("test-key", b"test-value").await.unwrap();
        // Get from L1
        let result1: _ = cache.get("test-key").await.unwrap();
        assert!(result1.is_some());
        // Clear L1 and get from L2
        {
            let mut l1 = cache.l1_cache.write().await;
            l1.clear();
        }
        let result2: _ = cache.get("test-key").await.unwrap();
        assert!(result2.is_some());
    }
    #[tokio::test]
    async fn test_cache_predictor() {
        let cache: _ = EdgeCache::new("test-cache", 100).unwrap();
        // Record access patterns
        cache.set("user_1", b"data").await.unwrap();
        cache.set("user_2", b"data").await.unwrap();
        // Access patterns might predict future accesses
        let recent: _ = vec!["user_1".to_string(), "user_2".to_string()];
        let predictions: _ = cache.predictor.write().await.predict(&recent);
        assert!(predictions.is_ok());
    }
    #[tokio::test]
    async fn test_cache_performance() {
        let cache: _ = EdgeCache::new("test-cache", 1000).unwrap();
        let start: _ = Instant::now();
        for i in 0..1000 {
            cache.set(&format!("key_{}", i), b"value").await.unwrap();
        }
        let set_time: _ = start.elapsed();
        let get_start: _ = Instant::now();
        for i in 0..1000 {
            cache.get(&format!("key_{}", i)).await.unwrap();
        }
        let get_time: _ = get_start.elapsed();
        // Use more lenient timeouts for CI environments
        assert!(set_time.as_millis() < 500, "Set operation took too long: {}ms", set_time.as_millis());
        assert!(get_time.as_millis() < 200, "Get operation took too long: {}ms", get_time.as_millis());
    }
    #[tokio::test]
    async fn test_concurrent_cache_access() {
        let cache: _ = Arc::new(Mutex::new(EdgeCache::new("test-cache", 1000)),.unwrap());
        let mut handles = vec![];
        for i in 0..10 {
            let cache_clone: _ = Arc::clone(cache);
            let handle: _ = tokio::spawn(async move {
                for j in 0..100 {
                    let key: _ = format!("concurrent_key_{}_{}, i", j));
                    cache_clone.set(&key, b"value").await.unwrap();
                    cache_clone.get(&key).await.unwrap();
                }
            });
            handles.push(handle);
        }
        for handle in handles {
            handle.await.unwrap();
        }
        let stats: _ = cache.get_stats().await.unwrap();
        assert!(stats.total_operations >= 1000);  // Expect at least 1000 operations (10 tasks * 100 ops)
    }
}