class

NATS::KV::Client

Inherits Reference / Object

Constructors

new(nats : NATS::Client)
Source

Instance methods

create(bucket : String, key : String, value : String | Bytes) : Int64 | Nil

Create a key in the given bucket with the specified value. On success, create returns the new revision number for the key. If the key already exists, the value will not be set and nil is returned.

if revision = kv.create("my-bucket", "my-key", "my-value")
  # created
else
  # key already existed and value was not set
end
Source
create_bucket(name : String, description : String = "", *, max_value_size : Int32 | Nil = nil, history : Int = 1, ttl : Time::Span | Nil = nil, max_bytes : Int64 | Nil = nil, storage : JetStream::StreamConfig::Storage = :file, replicas : Int32 = 1, discard_new_per_key : Bool = false, placement : JetStream::StreamConfig::Placement | Nil = nil, allow_msg_ttl : Bool | Nil = nil) : Bucket

Create a NATS::KV::Bucket to store key/value mappings in with the specified name. Options are:

  • storage: where to store the data for this bucket, either :file or :memory
  • max_value_size: the largest number of bytes a key/value entry can hold
  • history: how many revisions of a key to retain
  • ttl: how long until a value or revision expires
  • max_bytes: maximum size of this bucket on disk or in memory
  • replicas: how many NATS nodes to store this bucket's data on
Source
delete(bucket : String, key : String)
Source
delete(bucket : Bucket)
Source
delete_bucket(bucket : String)
Source
each_key(bucket : String, pattern : String = ">", &) : Nil
Source
get(bucket : String, keys : Enumerable(String), *, revision : Int | Nil = nil, ignore_deletes = false) : Array(Entry | Nil)
Source
get(bucket : String, key : String, *, revision : Int | Nil = nil, ignore_deletes : Bool = false) : Entry | Nil

Get the KV::Entry for the given key in bucket, or nil if the key does not exist.

Source
get_bucket(name : String) : Bucket | Nil

Get the Bucket with the given name, or nil if that bucket does not exist.

Source
history(bucket : String, key : String) : Array(Entry)

Get all of the currently retained history for the given key in the given bucket name. Note that some of the history could have expired due to Bucket#ttl or Bucket#history.

kv.set "config", "name", "1"
kv.history("config", "name")
Source
keys(bucket : String, pattern : String = ">") : Set(String)

Get all of the keys matching pattern for the given bucket name.

Source
list_buckets(pattern : String)
Source
purge(bucket : String, key : String)
Source
put(bucket : String, key : String, value : String | Bytes, ttl : Time::Span | Nil = nil) : Int64

Assign value to key in bucket, returning an acknowledgement or error if the key could not be set. The key will expire after ttl if it is provided.

Source
set(bucket : String, key : String, value : Data, ttl : Time::Span | Nil = nil)

Assign value to key in bucket without waiting for acknowledgement from the NATS server. The key will expire after ttl if it is provided.

Source
update(bucket : String, key : String, value : String | Bytes, revision : Int) : Int64 | Nil

Update a bucket's key with the specified value only if the current value is the specified revision. If this revision is the latest, the update is not performed and this method returns nil.

if revision = kv.update(bucket, key, value, revision)
  # updated
else
  # outdated revision
end
Source
watch(bucket : String, key : String, *, ignore_deletes = false, include_history = false)
Source