memcache API reference
A client for a memcached server, speaking its classic text protocol over the net system library. Store with an expiration (set / add), read (get), remove (delete), count atomically (incr / decr), and re-arm a key's expiry (touch). memcached is a volatile cache - keys expire on their exptime and the server evicts under memory pressure - so it suits sessions, rate limits, and derived data, not a system of record. exptime is seconds (0 = never expire, until evicted). A protocol error (ERROR / CLIENT_ERROR / SERVER_ERROR) throws a catchable memcache.MemcacheError (kind "memcache") with the reply and the server's text. The value block is framed over bytes by the server's byte count, so a value whose byte length differs from its rune length (any non-ASCII UTF-8 text) round-trips byte-exact. kvStore wraps a session as a kvstore.Store, the backend session and ratelimit take. Needs the default jennifer binary (uses net).
Import with import "memcache.j" as memcache;. See the memcache guide for prose and examples.
Contents
| Name | What it is |
|---|---|
add | Store value at key only if the key is absent. |
cas | Check-and-set: store value at key only if its CAS token still matches casId (from a prior gets) - i.e. |
connect | Open a session to the memcached server. |
decr | Atomically subtract delta from the counter at key (not below 0). |
delete | Remove key. |
get | Return the string value of key, or "" when the key is absent / expired. |
getBytes | Return the raw bytes value of key, or empty bytes when absent / expired (the binary counterpart to... |
getMulti | Fetch several keys in one round-trip (multi-key get) as a map of string to string; a missing / expired ... |
gets | Read key with its CAS token (gets), for a check-and-set update. |
incr | Atomically add delta to the counter at key. |
kvStore | A kvstore.Store over this session (a distributed backend for kvstore, session and ratelimit):... |
quit | End the session and close the connection. |
set | Store value at key with an exptime-second TTL, replacing any existing value. |
setBytes | Store a raw bytes value at key, byte-for-byte (the binary counterpart to set). |
touch | Re-arm key's expiry to exptime seconds. |
Item | struct: One value read by gets: its string value, the server's CAS token (an opaque version counter), and whether... |
MemcacheError | struct: A failed memcached operation. |
Options | struct: Connection settings (plaintext; memcached's text protocol has no auth / TLS). |
Session | struct: An open memcached connection. |
Functions
add
memcache.add(
session as Session,
key as string,
value as string,
exptime as int)Store value at key only if the key is absent. The atomic build block for locks and "create if new".
session- the open session
key- the key to write
value- the value to store
exptime- the TTL in seconds (0 = never expire, until evicted)
- Returns
bool - whether it was stored (false when the key already exists)
- Throws
MemcacheError - kind "memcache" on an unexpected reply
cas
memcache.cas(
session as Session,
key as string,
value as string,
exptime as int,
casId as int)Check-and-set: store value at key only if its CAS token still matches casId (from a prior gets) - i.e. only if no one else changed it meanwhile. The optimistic-concurrency primitive: gets, compute a new value, cas, and retry on "exists". Returns "stored" (success), "exists" (someone else changed it - retry), or "not_found" (the key is gone / expired).
session- the open session
key- the key to write
value- the new value
exptime- the TTL in seconds (0 = never expire, until evicted)
casId- the CAS token from a prior
gets - Returns
string - "stored", "exists", or "not_found"
- Throws
MemcacheError - kind "memcache" on an unexpected reply
connect
memcache.connect(opts as Options)Open a session to the memcached server.
opts- the connection settings
- Returns
Session - the open session
decr
memcache.decr(session as Session, key as string, delta as int)Atomically subtract delta from the counter at key (not below 0).
session- the open session
key- the counter key
delta- the amount to subtract
- Returns
int - the new value, or -1 when the key is absent
delete
memcache.delete(session as Session, key as string)Remove key.
session- the open session
key- the key to remove
- Returns
bool - whether the key existed
- Throws
MemcacheError - kind "memcache" on an unexpected reply
get
memcache.get(session as Session, key as string)Return the string value of key, or "" when the key is absent / expired.
session- the open session
key- the key to read
- Returns
string - the value, or "" when absent / expired
getBytes
memcache.getBytes(session as Session, key as string)Return the raw bytes value of key, or empty bytes when absent / expired (the binary counterpart to get). It never UTF-8-decodes, so a value stored with setBytes round-trips exactly.
session- the open session
key- the key to read
- Returns
bytes - the value, or empty bytes when absent / expired
getMulti
memcache.getMulti(session as Session, keys as list of string)Fetch several keys in one round-trip (multi-key get) as a map of string to string; a missing / expired key is simply absent from the map, so maps.has distinguishes it. Cheaper than N separate gets.
session- the open session
keys- the keys to read
- Returns
map of string to string - the found key -> value pairs
gets
memcache.gets(session as Session, key as string)Read key with its CAS token (gets), for a check-and-set update. The returned Item carries the value, the cas token, and whether the key was found; pass its cas to cas so the store only succeeds if no one else changed the value meanwhile.
session- the open session
key- the key to read
- Returns
Item - the value, CAS token, and found flag
incr
memcache.incr(session as Session, key as string, delta as int)Atomically add delta to the counter at key.
session- the open session
key- the counter key
delta- the amount to add
- Returns
int - the new value, or -1 when the key is absent
kvStore
memcache.kvStore(session as Session)A kvstore.Store over this session (a distributed backend for kvstore, session and ratelimit): kvstore.set / get / delete / touch / incrWindow then run on the server.
session- the open session
- Returns
kvstore.Store - a memcache-backed store
quit
memcache.quit(session as Session)End the session and close the connection.
session- the open session
set
memcache.set(
session as Session,
key as string,
value as string,
exptime as int)Store value at key with an exptime-second TTL, replacing any existing value.
session- the open session
key- the key to write
value- the value to store
exptime- the TTL in seconds (0 = never expire, until evicted)
- Throws
MemcacheError - kind "memcache" on a non-STORED reply
setBytes
memcache.setBytes(
session as Session,
key as string,
value as bytes,
exptime as int)Store a raw bytes value at key, byte-for-byte (the binary counterpart to set). A serialized blob, a compressed payload, or an image round-trips exactly; read it back with getBytes, not get (the string reader throws on a non-UTF-8 value).
session- the open session
key- the key to write
value- the raw value to store
exptime- the TTL in seconds (0 = never expire, until evicted)
- Throws
MemcacheError - kind "memcache" on a non-STORED reply
touch
memcache.touch(session as Session, key as string, exptime as int)Re-arm key's expiry to exptime seconds.
session- the open session
key- the key to re-arm
exptime- the new TTL in seconds (0 = never expire, until evicted)
- Returns
bool - whether the key existed
- Throws
MemcacheError - kind "memcache" on an unexpected reply
Structs
Item
One value read by gets: its string value, the server's CAS token (an opaque version counter), and whether the key was found. Pass cas to cas for a check-and-set that only stores if no one else changed the value meanwhile.
def struct Item {
value as string,
cas as int,
found as bool
};value- the value ("" when not found)
cas- the CAS token to hand to
cas(0 when not found) found- whether the key was present
MemcacheError
A failed memcached operation. Every error of kind "memcache" is one, so its fields are safe to read once the kind matches.
def struct MemcacheError {
kind as string,
message as string,
reply as string,
text as string
};kind- always
"memcache" message- the server's error line, or what went wrong
reply- the server's error reply:
ERROR(an unknown command),CLIENT_ERROR(the request was malformed) orSERVER_ERROR(the server failed, e.g. out of memory: retry later); "" for another failure text- the server's text after the reply word, "" when none
Options
Connection settings (plaintext; memcached's text protocol has no auth / TLS).
def struct Options {
host as string,
port as int
};host- the server host
port- the server port
Session
An open memcached connection.
def struct Session {
conn as net.Conn,
timeoutMs as int
};conn- the underlying socket
timeoutMs- per-read idle timeout in milliseconds (0 disables it)