memcached
stableEncode and decode the Memcached text protocol, and simulate a local in-memory cache with TTL support via the MemcacheLocal class.
use plugin memcached::{encode_set, encode_get, encode_delete, …} Functions (18)
- encode_set Encode a SET command string
- encode_get Encode a GET command string
- encode_delete Encode a DELETE command string
- encode_incr Encode an INCR command string
- encode_decr Encode a DECR command string
- encode_add Encode an ADD command string
- encode_replace Encode a REPLACE command string
- encode_append Encode an APPEND command string
- encode_prepend Encode a PREPEND command string
- encode_multi_get Encode a multi-key GET command string
- encode_touch Encode a TOUCH command string
- encode_cas Encode a CAS (check-and-set) command string
- encode_flush_all Encode a FLUSH_ALL command string
- encode_stats Encode a STATS command string
- encode_version Encode a VERSION command string
- parse_value_response Parse a VALUE response line
- parse_stat_line Parse a STAT response line
- MemcacheLocal — in-memory cache class MemcacheLocal is a local, in-process key-value store that mirrors the Memcached API.
Encode a SET command string
Produces a Memcached text-protocol set command string ready to send over a TCP socket. flags is an arbitrary integer stored with the item; exptime is the TTL in seconds (0 = no expiry).
use plugin memcached::{encode_set}
let cmd = encode_set("session:abc", "user_data", 0, 3600)
print(cmd)
Encode a GET command string
Produces a get <key>\r\n command string.
use plugin memcached::{encode_get}
let cmd = encode_get("session:abc")
print(cmd)
Encode a DELETE command string
Produces a delete <key>\r\n command string.
use plugin memcached::{encode_delete}
let cmd = encode_delete("session:abc")
print(cmd)
Encode an INCR command string
Produces an incr <key> <amount>\r\n command string for atomically incrementing a numeric value stored at key.
use plugin memcached::{encode_incr}
let cmd = encode_incr("page_views", 1)
print(cmd)
Encode a DECR command string
Produces a decr <key> <amount>\r\n command string. The server floors the result at 0.
use plugin memcached::{encode_decr}
let cmd = encode_decr("credits", 5)
print(cmd)
Encode an ADD command string
Like encode_set but produces an add command, which only stores the item if the key does not already exist on the server.
use plugin memcached::{encode_add}
let cmd = encode_add("lock:resource", "1", 0, 30)
print(cmd)
Encode a REPLACE command string
Produces a replace command, which only stores the item if the key already exists on the server.
use plugin memcached::{encode_replace}
let cmd = encode_replace("config:theme", "dark", 0, 0)
print(cmd)
Encode an APPEND command string
Produces an append command that adds value to the end of an existing item's data.
use plugin memcached::{encode_append}
let cmd = encode_append("log:today", " another entry")
print(cmd)
Encode a PREPEND command string
Produces a prepend command that adds value to the beginning of an existing item's data.
use plugin memcached::{encode_prepend}
let cmd = encode_prepend("log:today", "ENTRY: ")
print(cmd)
Encode a multi-key GET command string
Produces a single get command for multiple keys, which allows batching lookups in one round trip.
use plugin memcached::{encode_multi_get}
let cmd = encode_multi_get(["user:1", "user:2", "user:3"])
print(cmd)
Encode a TOUCH command string
Produces a touch command to update the expiry time of an existing item without changing its value.
use plugin memcached::{encode_touch}
let cmd = encode_touch("session:abc", 7200)
print(cmd)
Encode a CAS (check-and-set) command string
Produces a cas (check-and-set) command. The server only stores the new value if cas_unique matches the token returned by a prior gets command, enabling optimistic concurrency.
use plugin memcached::{encode_cas}
let cmd = encode_cas("counter", "42", 0, 0, 12345)
print(cmd)
Encode a FLUSH_ALL command string
Returns the flush_all\r\n command string that invalidates all items on the server.
use plugin memcached::{encode_flush_all}
let cmd = encode_flush_all()
print(cmd)
Encode a STATS command string
Returns the stats\r\n command string that requests server statistics.
use plugin memcached::{encode_stats, parse_stat_line}
let cmd = encode_stats()
print(cmd)
Encode a VERSION command string
Returns the version\r\n command string that requests the server's version string.
use plugin memcached::{encode_version}
let cmd = encode_version()
print(cmd)
Parse a VALUE response line
Parses a VALUE <key> <flags> <bytes> response line into {key, flags, bytes, data}. The optional second argument provides the data payload from the following line.
use plugin memcached::{parse_value_response}
let resp = parse_value_response("VALUE session:abc 0 9", "user_data")
print(resp["key"])
print(resp["bytes"])
Parse a STAT response line
Parses a STAT <name> <value> line into {name, value}. Use when processing the multi-line response to a stats command.
use plugin memcached::{parse_stat_line}
let entry = parse_stat_line("STAT curr_items 42")
print("{entry["name"]} = {entry["value"]}")
MemcacheLocal is a local, in-process key-value store that mirrors the Memcached API.
MemcacheLocal is a local, in-process key-value store that mirrors the Memcached API. It supports TTL, increment/decrement, and multi-get, and is ideal for testing or caching within a single process.
Constructor: MemcacheLocal.new() → MemcacheLocal
Methods: set(key, value, ttl?), get(key), delete(key), incr(key, amount), decr(key, amount), flush(), keys(), multi_get(keys), count(), exists(key), stats()
use plugin memcached::{MemcacheLocal}
let cache = MemcacheLocal.new()
cache.set("user:1", "Alice", 60)
cache.set("user:2", "Bob", 60)
let name = cache.get("user:1")
print(name)
cache.incr("hits", 1)
cache.incr("hits", 1)
print(cache.get("hits"))
let results = cache.multi_get(["user:1", "user:2"])
print(cache.count())
let info = cache.stats()
print("items: {info["curr_items"]}, bytes: {info["bytes"]}")