Open main menu

Wikibooks β

XQuery/Using the Memcached Module

< XQuery



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.


To enable the module you must take the following steps:

Step 1 change the include.module.memcached property in $EXIST_HOME/extensions/ from false to be true

  include.module.memcached = true

Step 2 uncomment the line in the $EXIST_HOME/conf.xml

  <module uri="" class="org.exist.xquery.modules.memcached.MemcachedModule" />

Step 3

  • Run "build" to recompile

Step 4

  • Restart the exist server

Step 5

  • Start your memcached server (it is not part of the eXist system)

Testing the Module is LoadedEdit

If you run the function util:get-module-info('') it should now return the module description.

Summary of FunctionsEdit

Functions to Create and Shutdown memcache clientsEdit

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.

Functions to set and get key/value pairsEdit

Sample CodeEdit

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

Creating a Memcache ClientEdit

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

import module namespace cache="";
import module namespace mcache="";

let $properties := <properties><property name="host" value="localhost"/></properties>

let $client := mcache:create-client($properties, false())

return cache:put("memcached", "client", $client)

Adding Mulitple ServersEdit

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

let $properties :=
   <properties><property name="host" value="server1"/></properties>,
   <properties><property name="host" value="server2"/></properties>,
   <properties><property name="host" value="server3"/></properties>

Changing the Memcached PortEdit

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

let $properties :=
   <property name="host" value="localhost"/>
   <property name="port" value="your port value here"/>

Setting Values in the MemcachedEdit

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:

import module namespace cache="";
import module namespace mcache="";

let $client := cache:get("memcached", "client")

   if ($client)
      then mcache:set($client, "key", "foo", 3600)
      else ()

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:

import module namespace cache="";
import module namespace mcache="";

let $client := cache:get("memcached", "client")

   if ($client)
      then mcache:get($client, "key")
      else ()

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


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/,
  #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

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