KV Class
Full-featured key-value store backed by SoliKV. Supports strings, counters, lists, sets, and hashes with Redis-compatible commands via REST API.
Configuration
KV shares the same SoliKV connection as Cache:
SOLIKV_RESP_HOST- SoliKV host (default:localhost)SOLIKV_RESP_PORT- SoliKV RESP port (default:6380)SOLIKV_TOKEN- Optional token
# Programmatic configuration
KV.configure("my-solikv-host", "my-token")
Note: Unlike Cache, KV operates on raw keys without any prefix.
Basic Operations
KV.set(key, value, ttl?)
Store a value. Optionally set TTL in seconds.
KV.set("user:1", { "name": "Alice" })
KV.set("temp", "data", 60) # expires in 60 seconds
KV.get(key)
Retrieve a value. Returns null if key doesn't exist.
let user = KV.get("user:1") # => { "name": "Alice" }
KV.delete(key)
Delete a key. Returns true if the key was removed.
KV.exists(key)
Check if a key exists. Returns Bool.
KV.keys(pattern?)
List keys matching a glob pattern. Default: "*"
let all = KV.keys() # all keys
let users = KV.keys("user:*") # keys starting with "user:"
KV.type(key)
Get the type of a key (string, list, set, hash, etc.).
KV.rename(key, newkey)
Rename a key.
TTL Operations
KV.ttl(key)
Get remaining TTL in seconds. Returns null if key doesn't exist or has no expiry.
KV.expire(key, seconds)
Set TTL on an existing key. Returns true if successful.
KV.persist(key)
Remove TTL from a key (make it persistent). Returns true if successful.
Counters
KV.incr(key) / KV.decr(key)
Atomically increment or decrement by 1. Creates the key with value 0 if it doesn't exist.
KV.incr("visits") # => 1
KV.incr("visits") # => 2
KV.decr("visits") # => 1
KV.incrby(key, amount) / KV.decrby(key, amount)
Increment or decrement by a specified amount. Returns the new value.
KV.incrby("score", 10) # => 10
KV.decrby("score", 3) # => 7
Lists
KV.lpush(key, ...values) / KV.rpush(key, ...values)
Push values to the head (left) or tail (right) of a list. Returns the new list length.
KV.rpush("queue", "job1")
KV.rpush("queue", "job2")
KV.lpush("queue", "priority")
KV.lpop(key) / KV.rpop(key)
Remove and return the first (left) or last (right) element.
KV.lrange(key, start, stop)
Get a range of elements. Use 0, -1 for all elements.
let all = KV.lrange("queue", 0, -1)
KV.llen(key)
Get the length of a list.
Sets
KV.sadd(key, ...members) / KV.srem(key, ...members)
Add or remove members from a set. Returns the number of elements added/removed.
KV.sadd("tags", "rust")
KV.sadd("tags", "soli")
KV.srem("tags", "rust")
KV.smembers(key)
Get all members of a set.
KV.sismember(key, member)
Check if a value is a member of a set. Returns Bool.
KV.scard(key)
Get the number of members in a set.
Hashes
KV.hset(key, field, value)
Set a field in a hash.
KV.hset("user:1", "name", "Alice")
KV.hset("user:1", "email", "[email protected]")
KV.hget(key, field)
Get a field value from a hash.
KV.hgetall(key)
Get all fields and values as a Hash.
let user = KV.hgetall("user:1")
# => { "name": "Alice", "email": "[email protected]" }
KV.hdel(key, ...fields)
Delete fields from a hash.
KV.hexists(key, field)
Check if a field exists in a hash. Returns Bool.
KV.hkeys(key) / KV.hlen(key)
Get all field names or the number of fields in a hash.
Server Commands
KV.ping()
Check connectivity. Returns "PONG".
KV.dbsize()
Get the total number of keys in the database.
KV.flushdb()
Delete all keys in the database. Use with caution.
KV.cmd(...args)
Run any raw SoliKV command.
KV.cmd("SET", "key", "value")
KV.cmd("GET", "key")
KV.cmd("EXPIRE", "key", 60)
Complete Example
# Rate limiting with counters
def check_rate_limit(ip)
let key = "rate:" + ip
let count = KV.incr(key)
if count == 1
KV.expire(key, 60) # 60 second window
end
count <= 100 # 100 requests per minute
end
# Job queue with lists
KV.rpush("jobs", JSON.stringify({ "type": "email", "to": "[email protected]" }))
let job = KV.lpop("jobs")
# User sessions with hashes
KV.hset("session:abc", "user_id", "123")
KV.hset("session:abc", "role", "admin")
KV.expire("session:abc", 3600)
let session = KV.hgetall("session:abc")
# Feature flags with sets
KV.sadd("features:beta", "user:1")
KV.sadd("features:beta", "user:2")
if KV.sismember("features:beta", "user:1")
println("User has beta access")
end