memcached.lsp

Module index



Module: memcached

Author: Jeff Ober
Version: 0.3
Original location: Source: memcached.lsp
Package definition: memcached.qwerty

Interface to libmemcached (http://tangent.org/552/libmemcached.html) (updated for newlisp 10)

This module is a work-in-progress. Currently implemented functions work (or at least they appear to). The full range of functionality is not nearly implemented, but it works well enough to allocate, connect, get/set keys, and disconnect/deallocate.

External libraries

libmemcached
memcached
libevent (required by memcached)

Version history

0.3 • updated for newlisp 10

0.2 • cleaned up some functions • added get-keys

0.1 • development release

example:
 (memcached:init)
 (memcached:add-server "localhost" 8080)
 (memcached:set-key "foo" "bar" 30)
 (memcached:get-key "foo") ; within 30 seconds
 => "bar"
 (sleep 30000) ; wait 30 seconds
 (memcached:get-key "foo") ; after 30+ seconds
 => nil
 (memcached:disconnect)


- § -

memcached:init

syntax: (memcached:init)

Initializes the memcached module.



- § -

memcached:disconnect

syntax: (memcached:disconnect)

Disconnects from all servers and deallocates libmemcached structures.



- § -

memcached:add-server

syntax: (memcached:add-server str-host int-port)
parameter: str-host - the hostname; required
parameter: int-port - the host port; required

Adds a server to be used as a source of cached data. Returns true or nil, depending on whether the server was successfully added or not.

example:
 (memcached:add-server "localhost" 8000)
 => true


- § -

memcached:result

syntax: (memcached:result)

Returns the result or error from the last operation.



- § -

memcached:set-key

syntax: (memcached:set-key str-key expr-value [int-expiration])
parameter: str-key - unique key to store expr-value under; required
parameter: str-expr - value to store under str-key; required
parameter: int-expiration - seconds until str-key will expire; optional

Sets str-key to str-expr on the memcached server. str-expr will be serialized using string. Keys that already exist are overwritten. Returns true for success, nil for failure.

example:
 (memcached:set-key "foo" "bar" 30) ; sets "foo" to "bar" for 30 seconds
 => true


- § -

memcached:get-key

syntax: (memcached:get-key str-key)
parameter: str-key - the key to retrieve; required

Retrieves the value associated with str-key from the memcached server. If the key does not exist or has expired, evaluates to nil. Otherwise, the string value is returned.

example:
 (memcached:set-key "foo" '("bar" "baz" "bat") (* 60 60))
 => true
 
 (memcached:get-key "foo")
 => "(\"bar\" \"baz\" \"bat\")"
 
 (let ((res (memcached:get-key "foo")))
   (if res (eval-string (string "'" res)))) ; evaluate quoted
 => ("bar" "baz" "bat")


- § -

memcached:get-keys

syntax: (memcached:get-keys list-keys)
parameter: list-keys - a list of strings

Fetches an association list of '(key value) pairs from the server. Invalid or expired values return nil.

example:
 (memcached:set-key "foo" "bar" 300)
 => "bar"
 (memcached:set-key "baz" "bat" 300)
 => "bat"
 (memcached:set-key "asdf" "qwerty" 300)
 => "qwerty"
 
 (memcached:get-keys '("foo" "baz" "asdf")
 => (("foo" "bar") ("baz" "bat") ("asdf" "qwerty"))
 
 (memcached:get-keys '("foo" "invalid" "expired"))
 => (("foo" "bar") ("invalid" nil) ("expired" nil))

- ∂ -

Artful Code

generated with newLISP  and newLISPdoc