lrust_cache 0.1.0

A high-performance LRU cache implementation in Rust
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use super::basic_lru_cache::private::Cache;
use super::BasicLruCache;
use parking_lot::Mutex;
use std::collections::hash_map::DefaultHasher;
use std::fmt::Debug;
use std::hash::{Hash, Hasher};
use std::ops::Deref;

// Minimum capacity per shard
const MIN_SHARD_CAPACITY: usize = 4;
// Maximum number of shards, must be a power of 2
const MAX_SHARDS: usize = 16;

/// A sharded LRU cache implementation for high-concurrency scenarios.
///
/// This implementation divides the cache into multiple shards, each protected by its own mutex,
/// to reduce contention in concurrent access scenarios. The number of shards is automatically
/// determined based on the total capacity, but will not exceed `MAX_SHARDS`.
///
/// # Type Parameters
///
/// * `K` - The type of keys used in the cache. Must implement `Clone + Debug + Hash + Eq + Send + Sync + 'static`
/// * `V` - The type of values stored in the cache. Must implement `Clone + Debug + Send + Sync + 'static`
///
/// # Examples
///
/// ```rust
/// use lrust_cache::ShardedLruCache;
///
/// let cache = ShardedLruCache::new(1000);
/// cache.put("key1".to_string(), "value1".to_string());
/// assert_eq!(cache.get(&"key1".to_string()), Some("value1".to_string()));
/// ```
pub struct ShardedLruCache<K, V>
where
    K: Clone + Debug + Hash + Eq + Send + Sync + 'static,
    V: Clone + Debug + Send + Sync + 'static,
{
    shards: Vec<Mutex<BasicLruCache<K, V>>>,
    total_capacity: usize,
    num_shards: usize, // Actual number of shards in use
}

impl<K, V> ShardedLruCache<K, V>
where
    K: Clone + Debug + Hash + Eq + Send + Sync + 'static,
    V: Clone + Debug + Send + Sync + 'static,
{
    /// Creates a new sharded LRU cache with the specified total capacity.
    ///
    /// The number of shards is automatically determined based on the capacity,
    /// ensuring that each shard has at least `MIN_SHARD_CAPACITY` entries
    /// and the total number of shards is a power of 2 not exceeding `MAX_SHARDS`.
    ///
    /// # Arguments
    ///
    /// * `capacity` - The total capacity of the cache
    ///
    /// # Panics
    ///
    /// Panics if capacity is 0
    pub fn new(capacity: usize) -> Self {
        assert!(capacity > 0, "Capacity must be positive");

        // Calculate appropriate number of shards
        let theoretical_shards = capacity / MIN_SHARD_CAPACITY;
        let num_shards = if theoretical_shards >= MAX_SHARDS {
            MAX_SHARDS
        } else {
            let mut n = 1;
            while n * 2 <= theoretical_shards && n < MAX_SHARDS {
                n *= 2;
            }
            n
        };

        let base_shard_capacity = capacity / num_shards;

        let remaining_capacity = capacity % num_shards;

        let mut shards = Vec::with_capacity(num_shards);

        for i in 0..num_shards {
            let shard_capacity = if i < remaining_capacity {
                base_shard_capacity + 1
            } else {
                base_shard_capacity
            };
            shards.push(Mutex::new(BasicLruCache::new(shard_capacity)));
        }

        Self {
            shards,
            total_capacity: capacity,
            num_shards,
        }
    }

    /// Returns the total capacity of the cache.
    pub fn capacity(&self) -> usize {
        self.total_capacity
    }

    /// Returns the number of shards in the cache.
    pub fn num_shards(&self) -> usize {
        self.num_shards
    }

    // Internal method to determine which shard a key belongs to
    fn get_shard_index(&self, key: &K) -> usize {
        let mut hasher = DefaultHasher::new();
        key.hash(&mut hasher);
        let hash = hasher.finish();
        (hash as usize) & (self.num_shards - 1)
    }

    /// Retrieves a value from the cache by its key.
    ///
    /// If the key exists, the value is cloned and returned, and the entry
    /// is marked as most recently used.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to look up
    ///
    /// # Returns
    ///
    /// * `Some(V)` if the key exists
    /// * `None` if the key doesn't exist
    pub fn get(&self, key: &K) -> Option<V> {
        let shard_idx = self.get_shard_index(key);
        let shard = self.shards[shard_idx].lock();
        shard.deref().get(key)
    }

    /// Inserts a key-value pair into the cache.
    ///
    /// If the key already exists, the value is updated and the old value
    /// is returned. If the cache is at capacity, the least recently used
    /// entry is removed to make space.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to insert
    /// * `value` - The value to insert
    ///
    /// # Returns
    ///
    /// * `Some(V)` if the key already existed (returns the old value)
    /// * `None` if the key didn't exist
    pub fn put(&self, key: K, value: V) -> Option<V> {
        let shard_idx = self.get_shard_index(&key);
        let shard = self.shards[shard_idx].lock();
        shard.deref().put(key, value)
    }

    /// Removes an entry from the cache by its key.
    ///
    /// # Arguments
    ///
    /// * `key` - The key to remove
    ///
    /// # Returns
    ///
    /// * `Some(V)` if the key existed (returns the removed value)
    /// * `None` if the key didn't exist
    pub fn remove(&self, key: &K) -> Option<V> {
        let shard_idx = self.get_shard_index(key);
        let shard = self.shards[shard_idx].lock();
        shard.deref().remove(key)
    }

    /// Returns the number of entries in the cache.
    pub fn len(&self) -> usize {
        self.shards
            .iter()
            .map(|shard| shard.lock().deref().len())
            .sum()
    }

    /// Returns true if the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.shards
            .iter()
            .all(|shard| shard.lock().deref().is_empty())
    }

    /// Removes all entries from the cache.
    pub fn clear(&self) {
        for shard in &self.shards {
            let shard = shard.lock();
            shard.deref().clear();
        }
    }
}

impl<K, V> Cache<K, V> for ShardedLruCache<K, V>
where
    K: Clone + Debug + Hash + Eq + Send + Sync + 'static,
    V: Clone + Debug + Send + Sync + 'static,
{
    fn get(&self, key: &K) -> Option<V> {
        self.get(key)
    }

    fn put(&self, key: K, value: V) -> Option<V> {
        self.put(key, value)
    }

    fn remove(&self, key: &K) -> Option<V> {
        self.remove(key)
    }

    fn len(&self) -> usize {
        self.len()
    }

    fn is_empty(&self) -> bool {
        self.is_empty()
    }

    fn clear(&self) {
        self.clear()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use parking_lot::Mutex as PLMutex;
    use std::collections::HashSet;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;
    use std::thread;
    use std::time::Duration;

    #[test]
    fn test_basic_operations() {
        let cache = ShardedLruCache::new(2);

        // Test empty cache
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);

        assert_eq!(cache.put("key1".to_string(), "one".to_string()), None);
        assert_eq!(cache.put("key2".to_string(), "two".to_string()), None);

        // Test non-empty cache
        assert!(!cache.is_empty());
        assert_eq!(cache.len(), 2);

        assert_eq!(cache.get(&"key1".to_string()), Some("one".to_string()));
        assert_eq!(cache.get(&"key2".to_string()), Some("two".to_string()));

        // Verify capacity limit
        cache.put("key3".to_string(), "three".to_string());
        println!("cache.len(): {}", cache.len());
        println!("cache.capacity(): {}", cache.capacity());
        assert!(cache.len() <= cache.capacity());
    }

    #[test]
    fn test_concurrent_access() {
        let cache = Arc::new(ShardedLruCache::new(1000));
        let mut handles = vec![];

        // Create multiple threads for concurrent access
        for i in 0..10 {
            let cache = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for j in 0..100 {
                    let key = format!("key_{}_{}", i, j);
                    cache.put(key.clone(), format!("value_{}", j));
                    thread::sleep(Duration::from_micros(1));
                    if let Some(value) = cache.get(&key) {
                        assert_eq!(value, format!("value_{}", j));
                    }
                }
            });
            handles.push(handle);
        }

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

        assert!(cache.len() <= cache.capacity());
    }

    #[test]
    fn test_concurrent_mixed_operations() {
        let cache = Arc::new(ShardedLruCache::new(2000));
        let mut handles = vec![];
        let operation_count = 1000;

        // Create writer threads
        for i in 0..4 {
            let cache = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for j in 0..operation_count {
                    let key = format!("key_{}", j % 100);
                    cache.put(key.clone(), format!("writer_{}_value_{}", i, j));
                }
            });
            handles.push(handle);
        }

        // Create reader threads
        for _i in 0..4 {
            let cache = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for j in 0..operation_count {
                    let key = format!("key_{}", j % 100);
                    if let Some(value) = cache.get(&key) {
                        assert!(
                            value.starts_with("writer_") || value.starts_with("mixed_"),
                            "Invalid value format: {}",
                            value
                        );
                    }
                }
            });
            handles.push(handle);
        }

        // Create mixed operation threads
        for i in 0..4 {
            let cache = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for j in 0..operation_count {
                    let key = format!("key_{}", j % 100);
                    if j % 2 == 0 {
                        cache.put(key.clone(), format!("mixed_{}_value_{}", i, j));
                    } else {
                        let _ = cache.get(&key);
                    }
                }
            });
            handles.push(handle);
        }

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

        assert!(cache.len() <= cache.capacity());
    }

    #[test]
    fn test_concurrent_capacity_correctness() {
        let capacity = 100;
        let cache = Arc::new(ShardedLruCache::new(capacity));
        let threads_count = 8;
        let operations_per_thread = 1000;
        let total_ops_counter = Arc::new(AtomicUsize::new(0));
        let mut handles = vec![];

        // Create multiple threads for concurrent writes
        for i in 0..threads_count {
            let cache = Arc::clone(&cache);
            let ops_counter = Arc::clone(&total_ops_counter);
            let handle = thread::spawn(move || {
                for j in 0..operations_per_thread {
                    let key = format!("key_{}_{}", i, j);
                    cache.put(key, j);
                    ops_counter.fetch_add(1, Ordering::SeqCst);
                }
            });
            handles.push(handle);
        }

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

        // Verify capacity limit
        assert!(
            cache.len() <= capacity,
            "Cache size {} exceeded capacity {}",
            cache.len(),
            capacity
        );

        // Verify total operations
        assert_eq!(
            total_ops_counter.load(Ordering::SeqCst),
            threads_count * operations_per_thread
        );
    }

    #[test]
    fn test_concurrent_remove_correctness() {
        let cache = Arc::new(ShardedLruCache::new(1000));
        let removed_values = Arc::new(PLMutex::new(HashSet::new()));
        let mut handles = vec![];

        // Pre-fill cache
        for i in 0..100 {
            cache.put(format!("key_{}", i), i);
        }

        // Create writer threads
        for i in 0..4 {
            let cache = Arc::clone(&cache);
            let removed = Arc::clone(&removed_values);
            let handle = thread::spawn(move || {
                for j in 0..25 {
                    let key = format!("key_{}", i * 25 + j);
                    if let Some(value) = cache.remove(&key) {
                        removed.lock().insert(value);
                    }
                }
            });
            handles.push(handle);
        }

        // Create reader threads
        for _ in 0..4 {
            let cache = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for i in 0..100 {
                    let key = format!("key_{}", i);
                    if let Some(value) = cache.get(&key) {
                        // Ensure retrieved value is valid
                        assert!(value >= 0 && value < 100);
                    }
                }
            });
            handles.push(handle);
        }

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

        // Verify removed values are unique
        let removed_count = removed_values.lock().len();
        assert!(removed_count > 0, "Should have values removed");
        assert!(
            removed_count <= 100,
            "Removed values should not exceed initial count"
        );
    }

    #[test]
    fn test_concurrent_clear_correctness() {
        let cache = Arc::new(ShardedLruCache::new(1000));
        let mut handles = vec![];

        // Pre-fill cache
        for i in 0..500 {
            cache.put(format!("init_key_{}", i), i);
        }

        // Create clear thread
        let cache_clone = Arc::clone(&cache);
        handles.push(thread::spawn(move || {
            thread::sleep(Duration::from_millis(50));
            cache_clone.clear();
        }));

        // Create concurrent read/write threads
        for i in 0..4 {
            let cache = Arc::clone(&cache);
            let handle = thread::spawn(move || {
                for j in 0..100 {
                    let key = format!("key_{}_{}", i, j);
                    cache.put(key.clone(), j);
                    thread::sleep(Duration::from_micros(10));
                    let _ = cache.get(&key);
                }
            });
            handles.push(handle);
        }

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

        // Verify final state
        assert!(cache.len() <= cache.capacity());
    }

    #[test]
    fn test_concurrent_shard_distribution() {
        let cache = Arc::new(ShardedLruCache::new(1000));
        let shard_counts = (0..cache.num_shards())
            .map(|_| Arc::new(AtomicUsize::new(0)))
            .collect::<Vec<_>>();
        let mut handles = vec![];

        // Create multiple threads for concurrent writes
        for i in 0..8 {
            let cache = Arc::clone(&cache);
            let shard_counts = shard_counts.clone();
            let handle = thread::spawn(move || {
                for j in 0..100 {
                    let key = format!("key_{}_{}", i, j);
                    let shard_idx = cache.get_shard_index(&key);
                    shard_counts[shard_idx].fetch_add(1, Ordering::SeqCst);
                    cache.put(key.clone(), j);
                }
            });
            handles.push(handle);
        }

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

        // Verify shard distribution
        let total_ops: usize = shard_counts
            .iter()
            .map(|counter| counter.load(Ordering::SeqCst))
            .sum();

        assert_eq!(total_ops, 800); // 8 threads * 100 operations

        // Check if all shards are used
        let unused_shards = shard_counts
            .iter()
            .filter(|counter| counter.load(Ordering::SeqCst) == 0)
            .count();
        assert_eq!(unused_shards, 0, "All shards should be used");
    }
}