| 552 | |
| 553 | #[test] |
| 554 | fn test_cache_functionality() { |
| 555 | let temp_dir = tempfile::tempdir().unwrap(); |
| 556 | let table = open_or_create::<TestTable>(temp_dir.path()).unwrap(); |
| 557 | |
| 558 | let key = TestKey(42); |
| 559 | let value = TestValue { |
| 560 | data: "hello world".to_string(), |
| 561 | }; |
| 562 | |
| 563 | // Insert value |
| 564 | table.insert(key, value.clone()); |
| 565 | |
| 566 | // First get - should load from RocksDB and cache it |
| 567 | let retrieved1 = table.get(key).unwrap(); |
| 568 | assert_eq!(retrieved1.read().data, value.data); |
| 569 | |
| 570 | // Check that cache has the item |
| 571 | let cache = table.cache().lock().unwrap(); |
| 572 | assert_eq!(cache.len(), 1); |
| 573 | assert_eq!(cache.cap().get(), 10); |
| 574 | drop(cache); |
| 575 | |
| 576 | // Second get - should come from cache (much faster) |
| 577 | let retrieved2 = table.get(key).unwrap(); |
| 578 | assert_eq!(retrieved2.read().data, value.data); |
| 579 | |
| 580 | // Cache should still have 1 item |
| 581 | let cache = table.cache().lock().unwrap(); |
| 582 | assert_eq!(cache.len(), 1); |
| 583 | drop(cache); |
| 584 | |
| 585 | // Remove the key - should clear from cache |
| 586 | table.remove(key); |
| 587 | assert!(table.get(key).is_none()); |
| 588 | } |
| 589 | |
| 590 | #[test] |
| 591 | fn test_cache_lru_eviction() { |