XQuery/Using the Memcached Module

Motivation
You want to setup a distributed caching system so that multiple systems can share information about what is in each of their caches. This module implements the memcached protocol.

Note: I have had problems getting the memcached module to work under eXist 2.1. Please let me know if you can get it to work.

Configuration
To enable the module you must take the following steps:

Step 1 change the include.module.memcached property in $EXIST_HOME/extensions/local.build.properties from false to be true include.module.memcached = true Step 2 uncomment the line in the $EXIST_HOME/conf.xml  Step 3 Step 4 Step 5
 * Run "build" to recompile
 * Restart the exist server
 * Start your memcached server (it is not part of the eXist system)

Testing the Module is Loaded
If you run the function util:get-module-info(' http://exist-db.org/xquery/memcached ') it should now return the module description.

Functions to Create and Shutdown memcache clients
There are two functions to create and remove a client: mcache:create-client($properties, $is-binary-indicator) and mcache:shutdown($client)

The function mcache:create-client returns a xs:long representing the client handle. All future references will use this handle.

Sample Code
The following example code was supplied by the module author, Evgeny Gazdovsky

Creating a Memcache Client
Before you need to create a client to memcached client for one or more memcached server(s).

Adding Mulitple Servers
If you want use one client for more then one server, you must add properties, like:

Changing the Memcached Port
If your memcached server is listen non standard port: 11211, you must add "port" property for this server:

Setting Values in the Memcached
For using same connection in different queries we are using cache module to store the client handle. If you want set (add, replace, delete) the stuff in memcached you can use script like:

This script will store string value "foo" for key "key" for 3600 seconds. All values except xs:base64Binary will be stored as strings. The xs:base64Binary will be stored as byte array. For storing the XML fragment you must serialize one before (see util:serialeze function). For getting data from memcached, use script like:

After time (3600 seconds in this example) will be expire and after that time the empty sequence will be returned for key "key".

Debugging
When you do a connect you should be able to see the following INFO on the console: 2012-03-10 11:16:15.741 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue The Rops is read operations and the Wops is the number of write operations.

2012-03-10 11:16:15.819 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@66a236

Upon shutdown the following will be in the log file:

2012-03-10 11:55:42.850 INFO net.spy.memcached.MemcachedClient: Shut down memcached client