Skip to main content

KvStore

Struct KvStore 

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

Thread-safe, cloneable, in-memory key-value store.

Cloning yields a handle to the same underlying state (see module docs).

Implementations§

Source§

impl KvStore

Source

pub fn new() -> Self

Create a new store with default limits (1 MiB values, 10000 keys).

Source

pub fn with_backend(self, backend: Arc<dyn KvBackend>) -> Self

Builder-style setter that installs a pluggable async KvBackend.

With a backend installed, the store runs in “cluster mode”: the *_async methods delegate their data operations to the backend instead of the local in-memory map. The synchronous methods are unaffected and continue to operate on the local map.

Source

pub fn is_clustered(&self) -> bool

Whether this store is running in cluster mode (a backend is installed).

Source

pub fn with_max_value_size(self, size: usize) -> Self

Builder-style setter for the maximum value size in bytes.

Source

pub fn with_max_keys(self, count: usize) -> Self

Builder-style setter for the maximum number of keys.

Source

pub fn set_max_value_size(&mut self, size: usize)

Set the maximum value size in bytes.

Source

pub fn set_max_keys(&mut self, count: usize)

Set the maximum number of keys.

Source

pub fn max_value_size(&self) -> usize

The configured maximum value size in bytes.

Source

pub fn max_keys(&self) -> usize

The configured maximum number of keys.

Source

pub fn validate_key(key: &str) -> Result<(), KvError>

Validate key format.

Keys must be non-empty, at most 1024 bytes, and contain only alphanumeric characters or one of -_./:.

§Errors

Returns KvError::InvalidKey when the key fails validation.

Source

pub fn clean_expired(&self)

Remove all expired entries from the store.

Source

pub fn get(&self, key: &str) -> Result<Option<Vec<u8>>, KvError>

Get a value by key.

Returns None if the key is missing or expired.

§Errors

Returns KvError::InvalidKey when the key is invalid.

Source

pub fn get_string(&self, key: &str) -> Result<Option<String>, KvError>

Get a value as a UTF-8 string.

§Errors

Returns KvError::InvalidKey when the key is invalid, or KvError::Storage when the stored bytes are not valid UTF-8.

Source

pub fn set(&self, key: &str, value: &[u8]) -> Result<(), KvError>

Set a value.

§Errors

Returns KvError::InvalidKey for an invalid key, KvError::ValueTooLarge when the value exceeds the configured limit, or KvError::QuotaExceeded when adding a new key would exceed the configured key count.

Source

pub fn set_string(&self, key: &str, value: &str) -> Result<(), KvError>

Set a string value.

§Errors

See KvStore::set.

Source

pub fn set_with_ttl( &self, key: &str, value: &[u8], ttl_ns: u64, ) -> Result<(), KvError>

Set a value with a TTL in nanoseconds.

§Errors

See KvStore::set.

Source

pub fn delete(&self, key: &str) -> Result<bool, KvError>

Delete a key.

Returns true if the key existed and was deleted.

§Errors

Returns KvError::InvalidKey when the key is invalid.

Source

pub fn exists(&self, key: &str) -> bool

Check if a key exists (and has not expired).

Source

pub fn list_keys(&self, prefix: &str) -> Result<Vec<String>, KvError>

List all non-expired keys with a given prefix.

§Errors

This implementation never fails, but returns Result for parity with the WASM host interface and future backends.

Source

pub fn increment(&self, key: &str, delta: i64) -> Result<i64, KvError>

Increment a numeric value atomically, returning the new value.

A missing or expired key is treated as 0. The arithmetic saturates.

§Errors

Returns KvError::InvalidKey for an invalid key, KvError::Storage when the existing value is not a valid integer, or KvError::QuotaExceeded when adding a new key would exceed the quota.

Source

pub fn compare_and_swap( &self, key: &str, expected: Option<&[u8]>, new_value: &[u8], ) -> Result<bool, KvError>

Compare-and-swap: set new_value only if the current value equals expected.

Returns true if the swap succeeded, false if the current value did not match expected.

§Errors

Returns KvError::InvalidKey for an invalid key, KvError::ValueTooLarge when new_value exceeds the configured limit, or KvError::QuotaExceeded when adding a new key would exceed the quota.

Source

pub async fn get_async(&self, key: &str) -> Result<Option<Vec<u8>>, KvError>

Async KvStore::get: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend, or KvError::InvalidKey for an invalid key on the local path.

Source

pub async fn set_async(&self, key: &str, value: &[u8]) -> Result<(), KvError>

Async KvStore::set: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend, or the local KvStore::set errors.

Source

pub async fn set_with_ttl_async( &self, key: &str, value: &[u8], ttl_ns: u64, ) -> Result<(), KvError>

Async KvStore::set_with_ttl: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend, or the local KvStore::set_with_ttl errors.

Source

pub async fn delete_async(&self, key: &str) -> Result<bool, KvError>

Async KvStore::delete: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend, or KvError::InvalidKey for an invalid key on the local path.

Source

pub async fn exists_async(&self, key: &str) -> Result<bool, KvError>

Async existence check: delegates to the backend in cluster mode, otherwise wraps the infallible local KvStore::exists.

§Errors

Propagates errors from the backend. The local path never fails.

Source

pub async fn list_keys_async( &self, prefix: &str, ) -> Result<Vec<String>, KvError>

Async KvStore::list_keys: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend. The local path never fails.

Source

pub async fn increment_async( &self, key: &str, delta: i64, ) -> Result<i64, KvError>

Async KvStore::increment: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend, or the local KvStore::increment errors.

Source

pub async fn compare_and_swap_async( &self, key: &str, expected: Option<&[u8]>, new: &[u8], ) -> Result<bool, KvError>

Async KvStore::compare_and_swap: delegates to the backend in cluster mode, otherwise runs the local sync logic.

§Errors

Propagates errors from the backend, or the local KvStore::compare_and_swap errors.

Source

pub fn clear(&self)

Remove all entries from the store.

Source

pub fn subscribe(&self) -> Receiver<KvEvent>

Subscribe to all change events.

Returns a raw broadcast receiver. Slow consumers may observe broadcast::error::RecvError::Lagged.

Source

pub fn watch_prefix( &self, prefix: impl Into<String>, ) -> impl Stream<Item = KvEvent>

Watch for change events whose key starts with prefix.

Returns a Stream that yields matching KvEvents. Lagged events (dropped because a slow consumer fell behind) are skipped.

Trait Implementations§

Source§

impl Clone for KvStore

Source§

fn clone(&self) -> KvStore

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KvStore

Source§

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

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

impl Default for KvStore

Source§

fn default() -> Self

Returns the “default value” for a type. 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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> OptionalSend for T
where T: Send + ?Sized,

Source§

impl<T> OptionalSync for T
where T: Sync + ?Sized,

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ServiceExt for T

Source§

fn propagate_header(self, header: HeaderName) -> PropagateHeader<Self>
where Self: Sized,

Propagate a header from the request to the response. Read more
Source§

fn add_extension<T>(self, value: T) -> AddExtension<Self, T>
where Self: Sized,

Add some shareable value to request extensions. Read more
Source§

fn map_request_body<F>(self, f: F) -> MapRequestBody<Self, F>
where Self: Sized,

Apply a transformation to the request body. Read more
Source§

fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>
where Self: Sized,

Apply a transformation to the response body. Read more
Source§

fn compression(self) -> Compression<Self>
where Self: Sized,

Compresses response bodies. Read more
Source§

fn decompression(self) -> Decompression<Self>
where Self: Sized,

Decompress response bodies. Read more
Source§

fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using HTTP status codes. Read more
Source§

fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using gRPC headers. Read more
Source§

fn follow_redirects(self) -> FollowRedirect<Self>
where Self: Sized,

Follow redirect resposes using the Standard policy. Read more
Source§

fn sensitive_headers( self, headers: impl IntoIterator<Item = HeaderName>, ) -> SetSensitiveRequestHeaders<SetSensitiveResponseHeaders<Self>>
where Self: Sized,

Mark headers as sensitive on both requests and responses. Read more
Source§

fn sensitive_request_headers( self, headers: impl IntoIterator<Item = HeaderName>, ) -> SetSensitiveRequestHeaders<Self>
where Self: Sized,

Mark headers as sensitive on requests. Read more
Source§

fn sensitive_response_headers( self, headers: impl IntoIterator<Item = HeaderName>, ) -> SetSensitiveResponseHeaders<Self>
where Self: Sized,

Mark headers as sensitive on responses. Read more
Source§

fn override_request_header<M>( self, header_name: HeaderName, make: M, ) -> SetRequestHeader<Self, M>
where Self: Sized,

Insert a header into the request. Read more
Source§

fn append_request_header<M>( self, header_name: HeaderName, make: M, ) -> SetRequestHeader<Self, M>
where Self: Sized,

Append a header into the request. Read more
Source§

fn insert_request_header_if_not_present<M>( self, header_name: HeaderName, make: M, ) -> SetRequestHeader<Self, M>
where Self: Sized,

Insert a header into the request, if the header is not already present. Read more
Source§

fn override_response_header<M>( self, header_name: HeaderName, make: M, ) -> SetResponseHeader<Self, M>
where Self: Sized,

Insert a header into the response. Read more
Source§

fn append_response_header<M>( self, header_name: HeaderName, make: M, ) -> SetResponseHeader<Self, M>
where Self: Sized,

Append a header into the response. Read more
Source§

fn insert_response_header_if_not_present<M>( self, header_name: HeaderName, make: M, ) -> SetResponseHeader<Self, M>
where Self: Sized,

Insert a header into the response, if the header is not already present. Read more
Source§

fn set_request_id<M>( self, header_name: HeaderName, make_request_id: M, ) -> SetRequestId<Self, M>
where Self: Sized, M: MakeRequestId,

Add request id header and extension. Read more
Source§

fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>
where Self: Sized, M: MakeRequestId,

Add request id header and extension, using x-request-id as the header name. Read more
Source§

fn propagate_request_id( self, header_name: HeaderName, ) -> PropagateRequestId<Self>
where Self: Sized,

Propgate request ids from requests to responses. Read more
Source§

fn propagate_x_request_id(self) -> PropagateRequestId<Self>
where Self: Sized,

Propgate request ids from requests to responses, using x-request-id as the header name. Read more
Source§

fn catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>
where Self: Sized,

Catch panics and convert them into 500 Internal Server responses. Read more
Source§

fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>
where Self: Sized,

Intercept requests with over-sized payloads and convert them into 413 Payload Too Large responses. Read more
Source§

fn trim_trailing_slash(self) -> NormalizePath<Self>
where Self: Sized,

Remove trailing slashes from paths. Read more
Source§

fn append_trailing_slash(self) -> NormalizePath<Self>
where Self: Sized,

Append trailing slash to paths. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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> 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