pub struct Counter { /* private fields */ }Expand description
Implementation of distributed counters using a JetStream stream.
Implementations§
Source§impl Counter
impl Counter
Sourcepub async fn from_stream(context: Context, stream: Stream) -> Result<Self>
pub async fn from_stream(context: Context, stream: Stream) -> Result<Self>
Creates a counter from an existing stream.
Sourcepub async fn add<S, V>(&self, subject: S, value: V) -> Result<BigInt>
pub async fn add<S, V>(&self, subject: S, value: V) -> Result<BigInt>
Adds a value to the counter for the given subject.
Sourcepub async fn get<S: ToSubject>(&self, subject: S) -> Result<Entry>
pub async fn get<S: ToSubject>(&self, subject: S) -> Result<Entry>
Gets the counter entry for a subject, including sources and last increment.
Returns complete information about a counter including its current value, source breakdown (if using stream sourcing), and the last increment value.
Sourcepub async fn load<S: ToSubject>(&self, subject: S) -> Result<BigInt>
pub async fn load<S: ToSubject>(&self, subject: S) -> Result<BigInt>
Loads just the counter value for a subject.
This is a convenience method that returns only the value, without sources or increment information.
Sourcepub async fn increment<S>(&self, subject: S) -> Result<BigInt>where
S: ToSubject,
pub async fn increment<S>(&self, subject: S) -> Result<BigInt>where
S: ToSubject,
Increments the counter by 1 for the given subject. This is a convenience method for the common case of incrementing by 1.
Sourcepub async fn decrement<S>(&self, subject: S) -> Result<BigInt>where
S: ToSubject,
pub async fn decrement<S>(&self, subject: S) -> Result<BigInt>where
S: ToSubject,
Decrements the counter by 1 for the given subject. This is a convenience method for the common case of decrementing by 1.
Sourcepub async fn get_multiple(
&self,
subjects: Vec<String>,
) -> Result<Pin<Box<dyn FutureStream<Item = Result<Entry>> + Send + '_>>>
pub async fn get_multiple( &self, subjects: Vec<String>, ) -> Result<Pin<Box<dyn FutureStream<Item = Result<Entry>> + Send + '_>>>
Gets multiple counter entries matching the given subjects.
Efficiently fetches multiple counters in a batch operation. Returns a stream of entries.
Note: Subjects should be exact subjects, not patterns. Non-existent counters are silently skipped in the results.