Skip to content
Plugins (190)

memcached

stable

Encode and decode the Memcached text protocol, and simulate a local in-memory cache with TTL support via the MemcacheLocal class.

use plugin memcached
18 functions Database
Runs with the native Zolo host

Plugin snippets can be edited in the Playground, but native capabilities are executed locally with the Zolo CLI.

/ filter jk navigate Esc clear
Functions (18)
  1. encode_set Encode a SET command string
  2. encode_get Encode a GET command string
  3. encode_delete Encode a DELETE command string
  4. encode_incr Encode an INCR command string
  5. encode_decr Encode a DECR command string
  6. encode_add Encode an ADD command string
  7. encode_replace Encode a REPLACE command string
  8. encode_append Encode an APPEND command string
  9. encode_prepend Encode a PREPEND command string
  10. encode_multi_get Encode a multi-key GET command string
  11. encode_touch Encode a TOUCH command string
  12. encode_cas Encode a CAS (check-and-set) command string
  13. encode_flush_all Encode a FLUSH_ALL command string
  14. encode_stats Encode a STATS command string
  15. encode_version Encode a VERSION command string
  16. parse_value_response Parse a VALUE response line
  17. parse_stat_line Parse a STAT response line
  18. MemcacheLocal — in-memory cache class MemcacheLocal is a local, in-process key-value store that mirrors the Memcached API.

From import to result

  1. 01Import only what you use
  2. 02Copy the smallest example
  3. 03Run it with the Zolo CLI

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"]}")

Global index

Find your way through Zolo

Try an idea

Start here

9 results

9 results

en