MemoryStorage

Struct MemoryStorage 

Source
pub struct MemoryStorage { /* private fields */ }
Expand description

In-memory storage with garbage collection.

Uses DashMap for thread-safe concurrent access and includes configurable garbage collection to prevent unbounded memory growth.

§Example

use skp_ratelimit::storage::{MemoryStorage, GcConfig};
use std::time::Duration;

// Default GC (every 10000 requests)
let storage = MemoryStorage::new();

// Custom GC interval
let storage = MemoryStorage::with_gc(GcConfig::on_duration(Duration::from_secs(60)));

// Manual GC only
let storage = MemoryStorage::with_gc(GcConfig::manual());
storage.run_gc().await;

Implementations§

Source§

impl MemoryStorage

Source

pub fn new() -> Self

Create a new memory storage with default GC configuration.

Source

pub fn with_gc(gc_config: GcConfig) -> Self

Create a new memory storage with custom GC configuration.

Source

pub async fn run_gc(&self)

Manually trigger garbage collection.

Source

pub fn len(&self) -> usize

Get the number of entries currently stored.

Source

pub fn is_empty(&self) -> bool

Check if the storage is empty.

Source

pub fn clear(&self)

Clear all entries.

Trait Implementations§

Source§

impl Debug for MemoryStorage

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryStorage

Source§

fn default() -> Self

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

impl Drop for MemoryStorage

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Storage for MemoryStorage

Source§

async fn get(&self, key: &str) -> Result<Option<StorageEntry>>

Get an entry by key. Read more
Source§

async fn set(&self, key: &str, entry: StorageEntry, ttl: Duration) -> Result<()>

Set an entry with a TTL. Read more
Source§

async fn delete(&self, key: &str) -> Result<()>

Delete an entry. Read more
Source§

async fn increment( &self, key: &str, delta: u64, window_start: u64, ttl: Duration, ) -> Result<u64>

Atomically increment a counter. Read more
Source§

async fn execute_atomic<F, T>( &self, key: &str, ttl: Duration, operation: F, ) -> Result<T>
where F: FnOnce(Option<StorageEntry>) -> (StorageEntry, T) + Send, T: Send,

Execute an atomic read-modify-write operation. Read more
Source§

async fn compare_and_swap( &self, key: &str, expected: Option<&StorageEntry>, new: StorageEntry, ttl: Duration, ) -> Result<bool>

Compare-and-swap operation. Read more

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, 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, 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.