Memcached

Connect to Ignite using Memcached compatible client.

❗️

This is a legacy Apache Ignite documentation

The new documentation is hosted here: https://ignite.apache.org/docs/latest/

Ignite is Memcached compliant which enables users to store and retrieve distributed data from Ignite cache using any Memcached compatible client.

📘

Currently, Ignite supports only binary protocol for Memcached.

You can connect to Ignite using a Memcached client in one of the following languages:

Configuration

Ignite accepts Memcached requests on its REST client connector port (11211 by default).

For all Memcached operations except increment and decrement Ignite uses default cache (cache with name set to default). It needs to be explicitly created beforehand, e.g. with the following configuration:

<bean class="org.apache.ignite.configuration.CacheConfiguration">
    <property name="name" value="default"/>
    <property name="atomicityMode" value="ATOMIC"/>
    <property name="backups" value="1"/>
</bean>

Note that examples below use examples/config/example-cache.xml configuration file which declares the default cache.

For increment and decrement Memcached operations Ignite uses IgniteAtomicLong data structures. They need to be created beforehand, e.g. with the following code:

ignite.atomicLong("key", 10, true);

PHP

To connect to Ignite using a PHP client for Memcached, you need to download Ignite and -

1. Start Ignite cluster with cache configured. For example :

bin/ignite.sh examples/config/example-cache.xml

2. Connect to Ignite using Memcached client, via binary protocol.

// Create client instance.
$client = new Memcached();

// Set localhost and port (set to correct values).
$client->addServer("localhost", 11211);

// Force client to use binary protocol.
$client->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

// Put entry to cache.
if ($client->add("key", "val"))
    echo "Successfully put entry in cache.\n";

// Check entry value.
echo("Value for 'key': " . $client->get("key") . "\n");

Java

To connect to Ignite using a Java client for Memcached, you need to download Ignite and -

1. Start Ignite cluster with cache configured. For example:

bin/ignite.sh examples/config/example-cache.xml

2. Connect to Ignite using Memcached client, via binary protocol.

MemcachedClient client = null;

try {
    client = new MemcachedClient(new BinaryConnectionFactory(),
            AddrUtil.getAddresses("localhost:11211"));
} catch (IOException e) {
    e.printStackTrace();
}

client.set("key", 0, "val");

System.out.println("Value for 'key': " + client.get("key"));

Python

To connect to Ignite using a Python client for Memcached, you need to download Ignite and -

1. Start Ignite cluster with cache configured. For example:

bin/ignite.sh examples/config/example-cache.xml

2. Connect to Ignite using Memcached client, via binary protocol.

import pylibmc

client = pylibmc.Client (["127.0.0.1:11211"], binary=True)

client.set("key", "val")

print "Value for 'key': %s"%client.get("key")

Ruby

To connect to Ignite using a Ruby client for Memcached, you need to download Ignite and -

1. Start Ignite cluster with cache configured. For example:

bin/ignite.sh examples/config/example-cache.xml

2. Connect to Ignite using Memcached client, via binary protocol.

require 'dalli'

options = { :namespace => "app_v1", :compress => true }

client = Dalli::Client.new('localhost:11211', options)

client.set('key', 'value', nil, :raw => true)

value = client.get('key')