pub struct SharedKeyCache { /* private fields */ }Expand description
Lets callers find the same live shared value by key.
Clones share the same cache. Self::new creates a separate cache.
Cache entries do not keep values or subscriptions alive. A reader or writable handle keeps its value alive.
The last reference to the value drops the subscription.
Self::reader and Self::shared reuse a live value without loading or subscribing again.
On a miss, each call evaluates its seed, asks the provider to load, then subscribes.
Concurrent misses for a compatible key can both run these steps.
If one call finds the other’s live value, it uses that value and drops its own subscription outside the cache lock.
Self::require_reader and Self::require always check storage first, even on a cache hit.
The stored value can change between the load and the subscription. The cache does not replay missed updates. To close this gap, the provider must replay updates, register atomically with a snapshot, or reconcile revisions. A provider can also deliver a new value during subscription, before the caller receives the handle.
Implementations§
Returns a writable handle with its own binding to the shared value.
This method uses the same load and subscription rules as Self::reader.
Rebinding this handle does not rebind a handle from another call for the same key.
Clones of this handle do follow the new binding. See Shared::rebind.
§Errors
Returns collision, metadata poison, or an original load/subscription failure.
§Panics
Propagates the same callback and destructor panics as Self::reader.
Sourcepub async fn reader<Key: SharedReaderKey>(
&self,
key: Key,
seed: impl FnOnce() -> Key::Value,
) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>
pub async fn reader<Key: SharedReaderKey>( &self, key: Key, seed: impl FnOnce() -> Key::Value, ) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>
Returns a reader for a live value or asks the provider for one.
On a cache miss, the seed runs even if storage contains a value.
If storage has no value, the cache uses the seed as the initial value before it subscribes.
See SharedKeyCache for simultaneous misses and updates between load and subscription.
§Errors
Returns collision, metadata poison, or an original load/subscription failure.
§Panics
Propagates seed, provider and discarded value/resource destructor panics. These callbacks run without the cache lock; publication may already have occurred before cleanup panics.
Constructs with a default closure while preserving the base key identity.
§Errors
Returns the same failures as Self::shared.
§Panics
Propagates the same callback and destructor panics as Self::reader.
Sourcepub async fn reader_default<Key: SharedReaderKey, Seed: Fn() -> Key::Value>(
&self,
default: SharedKeyDefault<Key, Seed>,
) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>
pub async fn reader_default<Key: SharedReaderKey, Seed: Fn() -> Key::Value>( &self, default: SharedKeyDefault<Key, Seed>, ) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>
Constructs a reader without adding the default wrapper to cache identity.
§Errors
Returns the same failures as Self::reader.
§Panics
Propagates the same callback and destructor panics as Self::reader.
Sourcepub async fn require<Key: SharedKey>(
&self,
key: Key,
) -> Result<Shared<Key::Value>, SharedKeyError<Key::Error>>
pub async fn require<Key: SharedKey>( &self, key: Key, ) -> Result<Shared<Key::Value>, SharedKeyError<Key::Error>>
Requires stored data before creating or reusing a writable reference.
Use SharedKeyDefault::into_key to require a default key without using
its seed. A live cached value may win over the newly loaded value.
§Errors
Returns absence, collision, poison, or original load/subscription failure.
§Panics
Propagates the same provider and destructor panics as Self::require_reader.
Sourcepub async fn require_reader<Key: SharedReaderKey>(
&self,
key: Key,
) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>
pub async fn require_reader<Key: SharedReaderKey>( &self, key: Key, ) -> Result<SharedReader<Key::Value>, SharedKeyError<Key::Error>>
Requires stored data before looking up a read-only reference.
§Errors
Returns absence even with a live cache hit, or collision/poison/provider failure.
§Panics
Propagates provider and discarded value/resource destructor panics. Cleanup completes before a selected reader or original provider cause is returned.
Sourcepub async fn reload<Key: SharedReaderKey>(
&self,
shared: &Shared<Key::Value>,
key: &Key,
) -> Result<(), SharedKeyError<Key::Error>>
pub async fn reload<Key: SharedReaderKey>( &self, shared: &Shared<Key::Value>, key: &Key, ) -> Result<(), SharedKeyError<Key::Error>>
Reloads a captured writable reference, retaining its current value on absence.
Concurrent rebinding does not retarget this operation after capture. Only the latest started reload on that reference may replace its value; completion, failure or cancellation never revives an older request.
§Errors
Returns incompatible key, access failure, or original provider failure.
§Panics
Propagates provider and displaced-value cleanup panics. Releasing the captured reference can also run a panicking destructor before return.
Sourcepub async fn reload_reader<Key: SharedReaderKey>(
&self,
reader: &SharedReader<Key::Value>,
key: &Key,
) -> Result<(), SharedKeyError<Key::Error>>
pub async fn reload_reader<Key: SharedReaderKey>( &self, reader: &SharedReader<Key::Value>, key: &Key, ) -> Result<(), SharedKeyError<Key::Error>>
Sourcepub async fn load<Key: SharedKey>(
&self,
shared: &Shared<Key::Value>,
key: Key,
seed: impl FnOnce(&Key::Value) -> Key::Value,
) -> Result<(), SharedKeyError<Key::Error>>
pub async fn load<Key: SharedKey>( &self, shared: &Shared<Key::Value>, key: Key, seed: impl FnOnce(&Key::Value) -> Key::Value, ) -> Result<(), SharedKeyError<Key::Error>>
Rebinds to a key before reloading, seeding fresh owners from current data.
seed runs synchronously only on a target-cache miss and supplies an
owned current-value snapshot without requiring Clone. The new owner
skips initial load and subscribes with user intent. Reload failure does
not roll the binding back. Unwrap default keys with into_key.
§Errors
Returns collision, cache/value access, subscription or explicit-load failure.
§Panics
A seed callback panic poisons the captured source value. Provider or displaced value/resource destructor panics also propagate. The receiving handle and its clones can already refer to the replacement value.
Sourcepub async fn load_reader<Key: SharedReaderKey>(
&self,
reader: &mut SharedReader<Key::Value>,
key: Key,
seed: impl FnOnce(&Key::Value) -> Key::Value,
) -> Result<(), SharedKeyError<Key::Error>>
pub async fn load_reader<Key: SharedReaderKey>( &self, reader: &mut SharedReader<Key::Value>, key: Key, seed: impl FnOnce(&Key::Value) -> Key::Value, ) -> Result<(), SharedKeyError<Key::Error>>
Replaces this reader’s reference before explicitly reloading the selected key.
Other reader clones retain their previously captured reference.
§Errors
Returns collision, cache/value access, subscription or explicit-load failure.
§Panics
A seed callback panic poisons the captured source value. Provider or displaced value/resource destructor panics also propagate. This reader may already be replaced and is not rolled back.
Sourcepub fn reset(&self) -> Result<(), SharedKeyError<Infallible>>
pub fn reset(&self) -> Result<(), SharedKeyError<Infallible>>
Clears weak lookup entries without revoking owners or touching storage.
In-flight constructors may publish again after this reset.
§Errors
Returns metadata poison if another operation panicked while holding its lock.