pub struct MemoryStore {
pub store: DashMap<String, Vec<Instant>>,
}Expand description
In-memory implementation of RateLimitStore using DashMap for concurrent access.
This store uses a thread-safe HashMap (DashMap) to store request timestamps for each client identifier. It’s suitable for single-instance applications where rate limiting data doesn’t need to be shared across multiple processes.
§Performance
- Fast access with O(1) lookup time
- Thread-safe concurrent operations
- Memory usage grows with the number of unique clients
§Limitations
- Data is lost on application restart
- Not suitable for distributed systems
- Memory usage can grow if clients are not cleaned up
Fields§
§store: DashMap<String, Vec<Instant>>Thread-safe map storing client identifiers and their request timestamps
Implementations§
Source§impl MemoryStore
impl MemoryStore
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new MemoryStore instance with an empty DashMap.
§Returns
A new MemoryStore instance ready for use.
§Example
use actix_web_ratelimit::store::MemoryStore;
use std::sync::Arc;
let store = Arc::new(MemoryStore::new());Trait Implementations§
Source§impl Default for MemoryStore
Default implementation that creates a new MemoryStore instance.
impl Default for MemoryStore
Default implementation that creates a new MemoryStore instance.
This is equivalent to calling MemoryStore::new().
Source§impl RateLimitStore for MemoryStore
impl RateLimitStore for MemoryStore
Source§fn is_limited(&self, key: &str, config: &RateLimitConfig) -> bool
fn is_limited(&self, key: &str, config: &RateLimitConfig) -> bool
Checks if the client has exceeded the rate limit and records the current request.
This method implements the sliding window algorithm:
- Gets or creates an entry for the client key
- Removes expired timestamps outside the time window
- Checks if the remaining request count exceeds the limit
- If not exceeded, records the current timestamp
§Arguments
key- Client identifier (typically IP address)config- Rate limiting configuration
§Returns
true if the client has exceeded the rate limit, false otherwise