Skip to main content

MemoryStore

Struct MemoryStore 

Source
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

Source

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.

This is equivalent to calling MemoryStore::new().

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl RateLimitStore for MemoryStore

Source§

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:

  1. Gets or creates an entry for the client key
  2. Removes expired timestamps outside the time window
  3. Checks if the remaining request count exceeds the limit
  4. 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

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,