Skip to content

Commit 781d754

Browse files
committed
Implement thread-safe cache storage
- [x] 2. Implement thread-safe cache storage - Define global cache using `std::sync::OnceLock<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>>` - Implement `get_cache()` function to initialize and return cache reference - Add necessary imports for `std::collections::HashMap`, `std::sync::{OnceLock, RwLock}`
1 parent 3dd32a3 commit 781d754

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

.kiro/specs/crc-params-caching/tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- Add module declaration to `src/lib.rs`
88
- _Requirements: 1.1, 3.1, 3.2_
99

10-
- [ ] 2. Implement thread-safe cache storage
10+
- [x] 2. Implement thread-safe cache storage
1111
- Define global cache using `std::sync::OnceLock<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>>`
1212
- Implement `get_cache()` function to initialize and return cache reference
1313
- Add necessary imports for `std::collections::HashMap`, `std::sync::{OnceLock, RwLock}`

src/cache.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use std::collections::HashMap;
2+
use std::sync::{OnceLock, RwLock};
3+
4+
/// Global cache storage for CRC parameter keys
5+
static CACHE: OnceLock<RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>>> = OnceLock::new();
6+
17
/// Cache key for storing CRC parameters that affect key generation
28
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
39
pub(crate) struct CrcParamsCacheKey {
@@ -17,4 +23,11 @@ impl CrcParamsCacheKey {
1723
reflected,
1824
}
1925
}
26+
}
27+
28+
/// Initialize and return reference to the global cache
29+
///
30+
/// Uses OnceLock to ensure thread-safe lazy initialization
31+
fn get_cache() -> &'static RwLock<HashMap<CrcParamsCacheKey, [u64; 23]>> {
32+
CACHE.get_or_init(|| RwLock::new(HashMap::new()))
2033
}

0 commit comments

Comments
 (0)