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
impl KvStore
Sourcepub fn with_backend(self, backend: Arc<dyn KvBackend>) -> Self
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.
Sourcepub fn is_clustered(&self) -> bool
pub fn is_clustered(&self) -> bool
Whether this store is running in cluster mode (a backend is installed).
Sourcepub fn with_max_value_size(self, size: usize) -> Self
pub fn with_max_value_size(self, size: usize) -> Self
Builder-style setter for the maximum value size in bytes.
Sourcepub fn with_max_keys(self, count: usize) -> Self
pub fn with_max_keys(self, count: usize) -> Self
Builder-style setter for the maximum number of keys.
Sourcepub fn set_max_value_size(&mut self, size: usize)
pub fn set_max_value_size(&mut self, size: usize)
Set the maximum value size in bytes.
Sourcepub fn set_max_keys(&mut self, count: usize)
pub fn set_max_keys(&mut self, count: usize)
Set the maximum number of keys.
Sourcepub fn max_value_size(&self) -> usize
pub fn max_value_size(&self) -> usize
The configured maximum value size in bytes.
Sourcepub fn validate_key(key: &str) -> Result<(), KvError>
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.
Sourcepub fn clean_expired(&self)
pub fn clean_expired(&self)
Remove all expired entries from the store.
Sourcepub fn get(&self, key: &str) -> Result<Option<Vec<u8>>, KvError>
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.
Sourcepub fn get_string(&self, key: &str) -> Result<Option<String>, KvError>
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.
Sourcepub fn set(&self, key: &str, value: &[u8]) -> Result<(), KvError>
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.
Sourcepub fn delete(&self, key: &str) -> Result<bool, KvError>
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.
Sourcepub fn list_keys(&self, prefix: &str) -> Result<Vec<String>, KvError>
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.
Sourcepub fn increment(&self, key: &str, delta: i64) -> Result<i64, KvError>
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.
Sourcepub fn compare_and_swap(
&self,
key: &str,
expected: Option<&[u8]>,
new_value: &[u8],
) -> Result<bool, KvError>
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.
Sourcepub async fn get_async(&self, key: &str) -> Result<Option<Vec<u8>>, KvError>
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.
Sourcepub async fn set_async(&self, key: &str, value: &[u8]) -> Result<(), KvError>
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.
Sourcepub async fn set_with_ttl_async(
&self,
key: &str,
value: &[u8],
ttl_ns: u64,
) -> Result<(), KvError>
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.
Sourcepub async fn delete_async(&self, key: &str) -> Result<bool, KvError>
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.
Sourcepub async fn exists_async(&self, key: &str) -> Result<bool, KvError>
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.
Sourcepub async fn list_keys_async(
&self,
prefix: &str,
) -> Result<Vec<String>, KvError>
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.
Sourcepub async fn increment_async(
&self,
key: &str,
delta: i64,
) -> Result<i64, KvError>
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.
Sourcepub async fn compare_and_swap_async(
&self,
key: &str,
expected: Option<&[u8]>,
new: &[u8],
) -> Result<bool, KvError>
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.
Sourcepub fn subscribe(&self) -> Receiver<KvEvent>
pub fn subscribe(&self) -> Receiver<KvEvent>
Subscribe to all change events.
Returns a raw broadcast receiver. Slow consumers may observe
broadcast::error::RecvError::Lagged.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for KvStore
impl !UnwindSafe for KvStore
impl Freeze for KvStore
impl Send for KvStore
impl Sync for KvStore
impl Unpin for KvStore
impl UnsafeUnpin for KvStore
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Requestimpl<T> OptionalSend for T
impl<T> OptionalSync for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ServiceExt for T
impl<T> ServiceExt for T
Source§fn propagate_header(self, header: HeaderName) -> PropagateHeader<Self>where
Self: Sized,
fn propagate_header(self, header: HeaderName) -> PropagateHeader<Self>where
Self: Sized,
Source§fn add_extension<T>(self, value: T) -> AddExtension<Self, T>where
Self: Sized,
fn add_extension<T>(self, value: T) -> AddExtension<Self, T>where
Self: Sized,
Source§fn map_request_body<F>(self, f: F) -> MapRequestBody<Self, F>where
Self: Sized,
fn map_request_body<F>(self, f: F) -> MapRequestBody<Self, F>where
Self: Sized,
Source§fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>where
Self: Sized,
Source§fn compression(self) -> Compression<Self>where
Self: Sized,
fn compression(self) -> Compression<Self>where
Self: Sized,
Source§fn decompression(self) -> Decompression<Self>where
Self: Sized,
fn decompression(self) -> Decompression<Self>where
Self: Sized,
Source§fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>where
Self: Sized,
Source§fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>where
Self: Sized,
Source§fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
fn follow_redirects(self) -> FollowRedirect<Self>where
Self: Sized,
Source§fn sensitive_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveRequestHeaders<SetSensitiveResponseHeaders<Self>>where
Self: Sized,
fn sensitive_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveRequestHeaders<SetSensitiveResponseHeaders<Self>>where
Self: Sized,
Source§fn sensitive_request_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveRequestHeaders<Self>where
Self: Sized,
fn sensitive_request_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveRequestHeaders<Self>where
Self: Sized,
Source§fn sensitive_response_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveResponseHeaders<Self>where
Self: Sized,
fn sensitive_response_headers(
self,
headers: impl IntoIterator<Item = HeaderName>,
) -> SetSensitiveResponseHeaders<Self>where
Self: Sized,
Source§fn override_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
fn override_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
Source§fn append_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
fn append_request_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
Source§fn insert_request_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
fn insert_request_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetRequestHeader<Self, M>where
Self: Sized,
Source§fn override_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
fn override_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
Source§fn append_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
fn append_response_header<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
Source§fn insert_response_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
fn insert_response_header_if_not_present<M>(
self,
header_name: HeaderName,
make: M,
) -> SetResponseHeader<Self, M>where
Self: Sized,
Source§fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_request_id<M>(
self,
header_name: HeaderName,
make_request_id: M,
) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
Source§fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
fn set_x_request_id<M>(self, make_request_id: M) -> SetRequestId<Self, M>where
Self: Sized,
M: MakeRequestId,
x-request-id as the header name. Read moreSource§fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_request_id(
self,
header_name: HeaderName,
) -> PropagateRequestId<Self>where
Self: Sized,
Source§fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
fn propagate_x_request_id(self) -> PropagateRequestId<Self>where
Self: Sized,
x-request-id as the header name. Read moreSource§fn catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>where
Self: Sized,
fn catch_panic(self) -> CatchPanic<Self, DefaultResponseForPanic>where
Self: Sized,
500 Internal Server responses. Read moreSource§fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
fn request_body_limit(self, limit: usize) -> RequestBodyLimit<Self>where
Self: Sized,
413 Payload Too Large responses. Read more