Memcached Java

Make Memcached Java super easy with Redis Labs

Redis Labs’ products and services enable running Memcached buckets in a highly available and auto-scalable manner, with predictable top performance.

Redise Pack lets you install an enterprise grade Memcached cluster in your environment of choice, whether an on-premises data-center or your preferred cloud platform. It gives you full control of your data and configuration – no clustering or sharding knowledge required!

Memcached Cloud is a fully managed cloud service for hosting and running Memcached datasets in a highly available and scalable manner, with predictable and stable top performance. It provides a storage engine for standard Memcached, as well as in-memory replication and instant auto-failover within the same data center or across data centers. The service completely frees developers from dealing with nodes, clusters, scaling, data persistence issues or failure recovery.

Using Memcached with Java

In order to use Memcached with Java you will need a Java Memcached client. In the following sections, we will demonstrate the use of spymemcached, a simple, asynchronous, single-threaded Memcached client written in Java.

Installing spymemcached

To use spymemcached, begin by adding the following repository to Maven:

<repositories>
    <repository>
      <id>spy</id>
      <name>Spy Repository</name>
      <layout>default</layout>
      <url>http://files.couchbase.com/maven2/</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
</repositories>

Next, declare the following Maven dependency:

<dependency>
  <groupId>spy</groupId>
  <artifactId>spymemcached</artifactId>
  <version>2.11.6</version>
  <scope>provided</scope>
</dependency>

Even if you’re not using Maven, you can download the latest spymemcached .jar files directly from The Maven Central Repository.

Opening a Connection to Memcached Using spymemcached

The following code creates a connection to Memcached using spymemcached:

import java.io.IOException;
import net.spy.memcached.*;

public class spymemcachedExample {
    public static void main(String[] args) {
        AuthDescriptor ad = new AuthDescriptor("PLAIN", 
            new PlainCallbackHandler("username", "password"));

        try {
            MemcachedClient mc = new MemcachedClient(
            new ConnectionFactoryBuilder()
                .setProtocol(ConnectionFactoryBuilder.Protocol.BINARY)
                .setAuthDescriptor(ad).build(),
            AddrUtil.getAddresses(new String[] { "hostname:port" }));
        } catch (IOException e) {
            // handle exception
        }
    }
}

To adapt this example to your code, make sure that you replace the following values with those of your bucket:

  • In line 7, replace username with your bucket’s username
  • In line 7, replace password with your bucket’s password
  • In line 14, the argument should be your bucket’s endpoint

Reading and Writing Data with spymemcached

Once connected to Memcached, you can start reading and writing data. The following code snippet writes the value bar to the Memcached key foo, reads it back, and prints it:

// open a connection to Memcached
...

mc.set("foo", 0, "bar");
String value = mc.get("foo");
System.out.println(value);

The output of the above code should be:

$ java spymemcachedExample
bar

For more information on using Redis Labs’ products and services with Java please see the Howto page.