CryBase::CouchBase::Services::KV::Client
Inherits CryBase::CouchBase::Services::KV::Bucket < CryBase::CouchBase::Services::KV::ResponseReader < CryBase::CouchBase::Services::KV::RequestWriter < Reference < Object
Talks the Couchbase memcached binary protocol against a single KV
node over plaintext or TLS according to the endpoint. Composed from
RequestWriter, ResponseReader, and Bucket mixins.
On construct: connects to endpoint, performs the TLS handshake when
endpoint.tls?, sends HELLO (advertising
Constants::FEATURE_SELECT_BUCKET),
SASL_AUTH (PLAIN), and SELECT_BUCKET for bucket. Then exposes
get / set / delete / touch / counter operations against that
bucket.
endpoint = CryBase::CouchBase::Endpoint.new(
"node1", 11210, CryBase::CouchBase::Service::KV, false
)
kv = KV::Client.new(endpoint, "user", "pass", "default")
kv.set("hello", "world")
kv.get("hello") # => "world".to_slice
kv.delete("hello")
kv.close
For connection strings, use from_string:
kv = KV::Client.from_string("couchbase://user:pass@node1/default")
Out of scope (deliberate, can be layered on top later):
- CAS, flags, durability, observe, and other op modifiers
- reconnect / retry
Constructors
Builds a KV endpoint from uri, connects, authenticates, and selects a bucket. The first host in the connection string is used.
username, password, and bucket may be passed explicitly or
embedded as couchbase://user:pass@host/bucket. Query parameters
currently supported by this helper: tls_verify and tls_hostname.
kv = KV::Client.from_string("couchbases://user:pass@node1:11217/default?tls_verify=false")
Connects, optionally performs TLS, then performs HELLO, SASL PLAIN auth, and SELECT_BUCKET, leaving the client ready for KV operations.
tls_verify and tls_hostname apply only when endpoint.tls?.
Provide tls_context to use a custom CA or other OpenSSL settings;
when supplied, it is used as-is.
Raises:
IO::Error/Socket::Error— socket connect failedOpenSSL::SSL::Error— TLS handshake or certificate verification failedKV::AuthFailed— SASL auth or bucket selection deniedKV::Error— server returned any other non-success status
The socket is closed and the exception re-raised if any handshake step fails.
Instance methods
Atomically decrements the unsigned integer document at key by delta. Couchbase counters do not go below zero.
Deletes the document at key. Raises NotFound if absent.
kv.delete("hello")
Compatibility alias for get_as(key, type, expiry).
Fetches the document at key. When expiry is provided, fetches
and updates the document expiration atomically. Raises NotFound
if absent.
bytes = kv.get("user:42")
JSON.parse(String.new(bytes))
Fetches the document at key and decodes it as type.
Use this for values written with set from a type that includes
JSON::Serializable. String and Bytes are decoded without JSON.
When expiry is provided, Couchbase fetches the document and updates
its expiration atomically.
struct Profile
include JSON::Serializable
property name : String
property score : Int32
end
profile = kv.get_as("user:42", Profile)
Atomically increments the unsigned integer document at key by delta. If the key is missing, Couchbase creates it with initial and applies expiry.
Stores value at key with optional expiry (seconds, or unix timestamp if greater than 30 days). Returns the new CAS token.
cas = kv.set("hello", "world")
cas = kv.set("hello", "world".to_slice, expiry: 60_u32)
Stores value at key, encoding JSON::Serializable values as JSON
and all other non-raw values with to_s. Returns the new CAS token.
Use get_as(key, Type) to load JSON-backed objects back into their
original type. String and Bytes are stored unchanged by the raw
overload above.
struct Profile
include JSON::Serializable
property name : String
property score : Int32
end
cas = kv.set("user:42", Profile.new("ada", 42))
Updates the document expiration without changing its value. Returns the new CAS token.