pub struct WorkerContext { /* private fields */ }Expand description
Shared context for stateful workers with in-memory key-value store.
Provides a process-local, concurrency-safe storage for workers to share state.
The store is backed by DashMap for lock-free concurrent access.
§Performance
- Uses
DashMapfor lock-free concurrent operations - Cloning is cheap (only clones the
Arc, not the data)
Implementations§
Source§impl WorkerContext
impl WorkerContext
Sourcepub fn get(&self, key: &str) -> Option<Value>
pub fn get(&self, key: &str) -> Option<Value>
Gets a value from the store by key.
Returns None if the key doesn’t exist.
Note: This method clones the value. For read-heavy workloads, consider caching values or using primitives that are cheap to clone.
Sourcepub fn with_value<F, R>(&self, key: &str, f: F) -> R
pub fn with_value<F, R>(&self, key: &str, f: F) -> R
Gets a value and applies a function to it without cloning.
This is more efficient than get() when you only need to inspect the value.
§Examples
let ctx = WorkerContext::new();
ctx.set("count", serde_json::json!(42));
let is_positive = ctx.with_value("count", |v| {
v.and_then(|val| val.as_i64()).map(|n| n > 0).unwrap_or(false)
});
assert!(is_positive);Sourcepub fn delete(&self, key: &str) -> Option<Value>
pub fn delete(&self, key: &str) -> Option<Value>
Deletes a key from the store.
Returns the previous value if it existed.
Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Checks if a key exists in the store without retrieving the value.
Sourcepub fn update<F>(&self, key: &str, f: F)
pub fn update<F>(&self, key: &str, f: F)
Updates a value in the store using a function.
The function receives the current value (or None if the key doesn’t exist)
and returns an Option to control the update:
Some(value)- Insert or update the key with the new valueNone- Remove the key from the store (if it existed)
§Warning
Returning None from the update function will delete the key from the store.
This is useful for conditional removal but can lead to unexpected data loss if not intended.
§Examples
let ctx = WorkerContext::new();
// Increment a counter, initializing to 1 if it doesn't exist
ctx.update("counter", |v| {
let count = v.and_then(|v| v.as_u64()).unwrap_or(0);
Some(serde_json::json!(count + 1))
});
assert_eq!(ctx.get("counter").and_then(|v| v.as_u64()), Some(1));
// Conditionally remove a key by returning None
ctx.set("temp", serde_json::json!("remove me"));
ctx.update("temp", |_| None); // Key is now deleted
assert!(!ctx.contains_key("temp"));Trait Implementations§
Source§impl Clone for WorkerContext
impl Clone for WorkerContext
Source§fn clone(&self) -> WorkerContext
fn clone(&self) -> WorkerContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for WorkerContext
impl Debug for WorkerContext
Auto Trait Implementations§
impl Freeze for WorkerContext
impl !RefUnwindSafe for WorkerContext
impl Send for WorkerContext
impl Sync for WorkerContext
impl Unpin for WorkerContext
impl UnsafeUnpin for WorkerContext
impl !UnwindSafe for WorkerContext
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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> 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> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.