Skip to content
Jennifer Programming Language

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

NameWhat it is
addStore value at key only if the key is absent.
casCheck-and-set: store value at key only if its CAS token still matches casId (from a prior gets) - i.e.
connectOpen a session to the memcached server.
decrAtomically subtract delta from the counter at key (not below 0).
deleteRemove key.
getReturn the string value of key, or "" when the key is absent / expired.
getBytesReturn the raw bytes value of key, or empty bytes when absent / expired (the binary counterpart to...
getMultiFetch several keys in one round-trip (multi-key get) as a map of string to string; a missing / expired ...
getsRead key with its CAS token (gets), for a check-and-set update.
incrAtomically add delta to the counter at key.
kvStoreA kvstore.Store over this session (a distributed backend for kvstore, session and ratelimit):...
quitEnd the session and close the connection.
setStore value at key with an exptime-second TTL, replacing any existing value.
setBytesStore a raw bytes value at key, byte-for-byte (the binary counterpart to set).
touchRe-arm key's expiry to exptime seconds.
Itemstruct: One value read by gets: its string value, the server's CAS token (an opaque version counter), and whether...
MemcacheErrorstruct: A failed memcached operation.
Optionsstruct: Connection settings (plaintext; memcached's text protocol has no auth / TLS).
Sessionstruct: An open memcached connection.

Functions

add

jennifer
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

jennifer
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

jennifer
memcache.connect(opts as Options)

Open a session to the memcached server.

opts
the connection settings
Returns Session
the open session

decr

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
memcache.quit(session as Session)

End the session and close the connection.

session
the open session

set

jennifer
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

jennifer
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

jennifer
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.

jennifer
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.

jennifer
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) or SERVER_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).

jennifer
def struct Options {
    host as string,
    port as int
};
host
the server host
port
the server port

Session

An open memcached connection.

jennifer
def struct Session {
    conn as net.Conn,
    timeoutMs as int
};
conn
the underlying socket
timeoutMs
per-read idle timeout in milliseconds (0 disables it)