pub struct MemoryStorage { /* private fields */ }Expand description
A simple in-memory storage implementation for testing and single-instance applications.
This implementation uses a HashMap wrapped in Arc<RwLock<>> for
thread-safe access. It doesn’t persist data across restarts and doesn’t
implement automatic expiration (expired entries are only removed during
cleanup operations).
§Features
- Zero dependencies: No external storage dependencies required
- Thread-safe: Uses tokio’s RwLock for concurrent access
- Fast operations: All operations are in-memory and very fast
- Context isolation: Supports nonce namespacing via contexts
- No persistence: Data is lost when the application restarts
- Pre-allocated capacity: Optional capacity hint for better performance
- Batch operations: Support for bulk operations
§Use Cases
- Development and testing environments
- Single-instance applications with short-lived nonces
- Applications that don’t require persistence across restarts
- Proof-of-concept implementations
§Example
use nonce_auth::storage::{MemoryStorage, NonceStorage};
use std::time::Duration;
let storage = MemoryStorage::new();
// Store a nonce
storage.set("test-nonce", None, Duration::from_secs(300)).await?;
// Check if it exists
let exists = storage.exists("test-nonce", None).await?;
assert!(exists);
// Get the entry
let entry = storage.get("test-nonce", None).await?;
assert!(entry.is_some());Implementations§
Source§impl MemoryStorage
impl MemoryStorage
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new in-memory storage instance.
§Example
use nonce_auth::storage::MemoryStorage;
let storage = MemoryStorage::new();Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new in-memory storage instance with pre-allocated capacity.
This can improve performance when you know approximately how many nonces you’ll be storing, as it avoids HashMap reallocations.
§Arguments
capacity- Initial capacity hint for the internal HashMap
§Example
use nonce_auth::storage::MemoryStorage;
// Pre-allocate for ~1000 nonces
let storage = MemoryStorage::with_capacity(1000);Source§impl MemoryStorage
Batch operations support for better performance
impl MemoryStorage
Batch operations support for better performance
Sourcepub async fn batch_set(
&self,
nonces: Vec<(&str, Option<&str>)>,
_ttl: Duration,
) -> Result<usize, NonceError>
pub async fn batch_set( &self, nonces: Vec<(&str, Option<&str>)>, _ttl: Duration, ) -> Result<usize, NonceError>
Insert multiple nonces in a batch operation.
This method acquires the write lock once and performs all insertions, which can be more efficient than individual set operations.
§Arguments
nonces- Vector of (nonce, context) tuples to insert_ttl- Time-to-live (not used in memory storage but kept for consistency)
§Returns
Number of successfully inserted nonces (duplicates are skipped)
§Example
use nonce_auth::storage::MemoryStorage;
use std::time::Duration;
let storage = MemoryStorage::new();
let nonces = vec![
("nonce1", None),
("nonce2", Some("ctx1")),
("nonce3", Some("ctx2")),
];
let inserted = storage.batch_set(nonces, Duration::from_secs(300)).await?;
assert_eq!(inserted, 3);Sourcepub async fn batch_exists(
&self,
nonces: Vec<(&str, Option<&str>)>,
) -> Result<Vec<bool>, NonceError>
pub async fn batch_exists( &self, nonces: Vec<(&str, Option<&str>)>, ) -> Result<Vec<bool>, NonceError>
Check existence of multiple nonces in a batch operation.
This method acquires the read lock once and checks all nonces, which can be more efficient than individual exists operations.
§Arguments
nonces- Vector of (nonce, context) tuples to check
§Returns
Vector of boolean values indicating existence for each nonce
§Example
use nonce_auth::storage::MemoryStorage;
let storage = MemoryStorage::new();
let check_nonces = vec![
("nonce1", None),
("nonce2", Some("ctx1")),
];
let results = storage.batch_exists(check_nonces).await?;Sourcepub async fn batch_get(
&self,
nonces: Vec<(&str, Option<&str>)>,
) -> Result<Vec<Option<NonceEntry>>, NonceError>
pub async fn batch_get( &self, nonces: Vec<(&str, Option<&str>)>, ) -> Result<Vec<Option<NonceEntry>>, NonceError>
Get multiple nonces in a batch operation.
This method acquires the read lock once and retrieves all nonces, which can be more efficient than individual get operations.
§Arguments
nonces- Vector of (nonce, context) tuples to retrieve
§Returns
Vector of optional NonceEntry values
§Example
use nonce_auth::storage::MemoryStorage;
let storage = MemoryStorage::new();
let get_nonces = vec![
("nonce1", None),
("nonce2", Some("ctx1")),
];
let results = storage.batch_get(get_nonces).await?;