An Oracle blog about Oracle Coherence

Getting Started With The Coherence Memcached Adaptor


Coherence 12c (12.1.3) adds support for Memcached clients to directly store data a in Coherence cluster using the Binary Memcached protocol. This post outlines how to configure the Coherence Memcached Adaptor and includes a simple PHP example to show how Memecached clients can connect to a Coherence cluster.

The Memcached adaptor is configured as a proxy service that runs in the Coherence cluster.  This is highlighted in the example cache configuration below:

<?xml version="1.0"?>
<cache-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Note a couple of things:

  1. The <cache-name> element under <memcached-acceptor> defines the distributed cache for the Memcached acceptor.  All Memcached clients connecting to this acceptor will access this cache.
  2. You can optionally enable client authentication by setting the <memcached-auth-method>. Currently the only supported method is SASL (Simple Authentication and Security Layer) plain authentication.
  3. The <address-provider> element defines the socket-address that the Memcached Adapter will listen on for connections.  It is defined in the Coherence operational configuration file.

Below is an example operational configuration file that shows the setup of an address-provider:

<?xml version='1.0'?>
      <address-provider id="memcached-addr-provider">
    <destination system-property="tangosol.coherence.log">stdout</destination>
    <severity-level system-property="tangosol.coherence.log.level">9</severity-level>

The address provider with id memcached-addr-provider defines the socket-address reference. 


For authentication, you can write a custom IdentityAsserter implementation and declare it in the operational configuration file. This is shown above but the relevant snippet is shown below:


The Memcached Adapter calls the IdentityAsserter implementation and passes the com.tangosol.net.security.UsernameAndPassword object as a token.

A sample IdentityAsserter is shown below:

package memcached;
import com.tangosol.net.Service;
import com.tangosol.net.security.IdentityAsserter;
import com.tangosol.net.security.UsernameAndPassword;
import javax.security.auth.Subject;
public class PasswordIdentityAsserter implements IdentityAsserter {
  public Subject assertIdentity(Object oToken, Service service)
  throws SecurityException {
    if (oToken instanceof UsernameAndPassword) {
      UsernameAndPassword secToken = (UsernameAndPassword) oToken;
      String sPwd = new String(secToken.getPassword());
      if (secToken.getUsername().equals("username")
          && sPwd.equals("password")) {
        return new Subject();
    throw new SecurityException("Access denied");

PHP Memcached client

There are 2 very popular PHP memcached clients – Memcache and Memcached. Memcached is the newer one and supports both the Binary Memcached protocol and SASL authentication. To enable Memcached and SASL, add the following to the php.ini file:

    memcached.use_sasl = 1

The following PHP code uses the PHP Memcached client library to connect to a Coherence cluster, add an entry and then retrieve it again.

  $m = new Memcached();
  $m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
  $m->setSaslAuthData("username", "password");
  if (!count($m->getServerList())) {
    // List of Coherence proxy service nodes
    $m->addServer('', 11211);
  $m->set('test', 'test_string');
  print_r('Cache value: ' . $m->get('test'));

Putting it all together

This is easy to try out for yourself.  First download and install Coherence, PHP and the PHP Memcached client library. Then in a test dir create an operational configuration file called tangosol-coherence-override.xml with the operational configuration details outlined above. Similarly create a cache configuration file called coherence-cache-config.xml with the specified cache configuration for Memcached clients. Finally, in the test dir create a dir called memcached and in it create a file called PasswordIdentityAsserter.java with the code listing above.

On the PHP side just save the example PHP code above to a file called test.php. Now we are ready to test everything. 

To startup Coherence we will need to refer to the Coherence libraries. To make this easier define an environment variable for the location of the Coherence installation called COH_HOME. Below we will assume a Linux/OSX environment but its very similar on Windows.

Build the PasswordIdentityAsserter by running the following command in your test dir:

javac –cp $COH_HOME/lib/coherence.jar memcached/PasswordIdentityAsserter.java

Now start a Coherence cache server to store the Memcached client data:

java -cp .:$COH_HOME/lib/coherence.jar com.tangosol.net.DefaultCacheServer

Lastly test that the PHP client can connect, store and read data securely via the Coherence Memcached Adaptor:

php test.php
The output should be:

    [] => 12.1.3
Cache value: test_string

This shows that the value inserted into Coherence by the PHP client, test_string, has successfully been read back.

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.