jmemcached

This is a Java implementation of the daemon (server) side of the memcached protocol. It implements a clone of the C version of the memcached daemon, but in pure Java.

It was originally written by Jehiah Czebotar but is now being maintained by me. It is open source under the Apache License, Version 2.0.

What would I use it for?

A distributed client/server style cache. Some people use memcached to reduce load on their database. It's a good general purpose network available cache to compliment any network available service.

Tell me more?

Ok.

  • It is implemented in, and requires at least Java 5.
  • It is protocol compatible with the C version of memcached. Existing clients, including the one for Java, should work without modification. Replacing the Java version with the C version (and vice versa) is trivial.
  • It can be embedded in your existing Java project. (For example hosted by a web application or by an OSGI bundle.)
  • ... or it can be used from the command line interface, with commands roughly switch compatible with the C version.
  • ... or it can be configured using your favourite dependency injection framework (like Spring, PicoContainer, etc.)
  • It uses Apache MINA for non-blocking, scalable (NIO) network I/O.
  • While it is somewhat slower than the C version, it is still quite fast, and actually scales better across multiple cores.
  • The cache portion of the project can be used independently of the daemon so that local process users can have quick low-overhead access to the cache while maintaining the client-server relationship for external clients.
  • The storage for the cache is abstracted/delegated so it should be possible to replace it with other cache implementations (such as OSCache, EHCache, etc.) if that is appropriate.

How to use it?

There are two ways; programmatically (embedded inside your application) or using the command-line (CLI) interface.

For programmatic example, here's a snippet from the source for the main class for the CLI:

        // create daemon and start it
        MemCacheDaemon daemon = new MemCacheDaemon();
        LRUCacheStorageDelegate cacheStorage = new LRUCacheStorageDelegate(max_size, max_bytes, 1024000);
        daemon.setCache(new Cache(cacheStorage));
        daemon.setAddr(addr);
        daemon.setIdleTime(idle);
        daemon.setPort(port);
        daemon.setVerbose(verbose);
        daemon.start();

Also take a look at the Javadoc for the project.

To use jmemcached from the command line, just run Java against the cli "-with-dependendencies" JAR.

The CLI accepts the following options:

 -V             Show version number
 -h,--help      print this help screen
 -i,--idle      disconnect after idle  seconds
 -l,--listen    Address to listen on
 -m,--memory    max memory to use in MB
 -p,--port      port to listen on
 -v             verbose (show commands)

How can I get it?

You can retrieve the compiled JARs from the Maven 2 repository: http://thimbleware.com/maven/com/thimbleware/

To make use of jmemcached in your project, the easiest way is to use Maven 2.

To do this using Maven, add the ThimbleWare repository:

    <repositories>
        <repository>
            <id>thimbleware.repo</id>
            <url>http://thimbleware.com/maven</url>
        </repository>
    </repositories>

And then add the dependency:

    <dependency>
        <groupId>com.thimbleware.jmemcached</groupId>
        <artifactId>jmemcached-core</artifactId>
        <version>0.4</version>
    </dependency>

Source code can be retrieved from the Mercurial repository.

What's next?

Missing features (when compared against the native [C] version):

  • delayed 'flush_all'
  • UDP protocol

I'd like to do the following:

  • Provide adapters for Spring configuration and servlet container (and maybe OSGi) hosting for the daemon.
  • Implement automatic replication to other memcached instances (C or Java versions) in a known configuration.
  • Allow the exposure of information about the cache via JMX (likely using Spring).
  • Performance test against other distributed cache solutions.
  • Improve performance even further.
  • Implement a transactional extension to the protocol (COMMIT, ROLLBACK), to allow for concurrent transactional operations (likely using the MVCC algorithm) on the cache.