XQuery/Using the Memcached Module

Motivation

edit

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

edit

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

  <module uri="http://exist-db.org/xquery/memcached" 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 Loaded

edit

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

Summary of Functions

edit

Functions to Create and Shutdown memcache clients

edit

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 pairs

edit

Sample Code

edit

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

Creating a Memcache Client

edit

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

import module namespace cache="http://exist-db.org/xquery/cache";
import module namespace mcache="http://exist-db.org/xquery/memcached";

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

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

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

Adding Mulitple Servers

edit

If you want use one client for more than 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 Port

edit

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

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

Setting Values in the Memcached

edit

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="http://exist-db.org/xquery/cache";
import module namespace mcache="http://exist-db.org/xquery/memcached";

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

return
   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="http://exist-db.org/xquery/cache";
import module namespace mcache="http://exist-db.org/xquery/memcached";

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

return
   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".

Debugging

edit

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

References

edit

setting up memcached on Windows