Memcached (Object cache)

Last updated 3rd June 2021

Objective

Memcached is a simple in-memory object store well-suited for application level caching.

See the Memcached for more information.

Both Memcached and Redis can be used for application caching. As a general rule, Memcached is simpler and thus more widely supported while Redis is more robust. Web PaaS recommends using Redis if possible but Memcached is fully supported if an application favors that cache service."

Supported versions

Grid
1.4
1.5
1.6

Relationship

The format exposed in the $PLATFORM_RELATIONSHIPS environment variable:

{
    "service": "memcached16",
    "ip": "169.254.34.86",
    "hostname": "3sdm72jgaxge2b6aunxdlzxyea.memcached16.service._.eu-3.platformsh.site",
    "cluster": "rjify4yjcwxaa-master-7rqtwti",
    "host": "memcached.internal",
    "rel": "memcached",
    "scheme": "memcached",
    "type": "memcached:1.6",
    "port": 11211
}

Usage example

In your .platform/services.yaml:

cachemc:
    type: memcached:1.6

Now add a relationship in your .platform.app.yaml file:

relationships:
    memcachedcache: "cachemc:memcached"

You will need to use memcached type when defining the service

```yaml

.platform/services.yaml

service_name: type: memcached:version ```

and the endpoint memcached when defining the relationship

```yaml

.platform.app.yaml

relationships: relationship_name: “service_name:memcached” ```

Your service_name and relationship_name are defined by you, but we recommend making them distinct from each other.

If you are using PHP, configure the relationship and enable the PHP memcached extension in your .platform.app.yaml. (Note that the memcached extension requires igbinary and msgpack as well, but those will be enabled automatically.)

runtime:
    extensions:
        - memcached

For Python you will need to include a dependency for a Memcached library, either via your requirements.txt file or a global dependency. As a global dependency you would add the following to .platform.app.yaml:

dependencies:
    python:
       python-memcached: '*'

You can then use the service in a configuration file of your application with something like:

package examples

import (
     "fmt"
     "github.com/bradfitz/gomemcache/memcache"
     psh "github.com/platformsh/config-reader-go/v2"
     gomemcache "github.com/platformsh/config-reader-go/v2/gomemcache"
)

func UsageExampleMemcached() string {

     // Create a NewRuntimeConfig object to ease reading the Web PaaS environment variables.
     // You can alternatively use os.Getenv() yourself.
     config, err := psh.NewRuntimeConfig()
     checkErr(err)

     // Get the credentials to connect to the Solr service.
     credentials, err := config.Credentials("memcached")
     checkErr(err)

     // Retrieve formatted credentials for gomemcache.
     formatted, err := gomemcache.FormattedCredentials(credentials)
     checkErr(err)

     // Connect to Memcached.
     mc := memcache.New(formatted)

     // Set a value.
     key := "Deploy_day"
     value := "Friday"

     err = mc.Set(&memcache.Item{Key: key, Value: []byte(value)})

     // Read it back.
     test, err := mc.Get(key)

     return fmt.Sprintf("Found value <strong>%s</strong> for key <strong>%s</strong>.", test.Value, key)
}
package sh.platform.languages.sample;

import net.spy.memcached.MemcachedClient;
import sh.platform.config.Config;

import java.util.function.Supplier;

import sh.platform.config.Memcached;

public class MemcachedSample implements Supplier<String> {

    @Override
    public String get() {
        StringBuilder logger = new StringBuilder();

        // Create a new config object to ease reading the Web PaaS environment variables.
        // You can alternatively use getenv() yourself.
        Config config = new Config();

        // Get the credentials to connect to the Memcached service.
        Memcached memcached = config.getCredential("memcached", Memcached::new);

        final MemcachedClient client = memcached.get();

        String key = "cloud";
        String value = "platformsh";

        // Set a value.
        client.set(key, 0, value);

        // Read it back.
        Object test = client.get(key);

        logger.append(String.format("Found value %s for key %s.", test, key));

        return logger.toString();
    }
}  
<?php

declare(strict_types=1);

use Platformsh\ConfigReader\Config;

// Create a new config object to ease reading the Web PaaS environment variables.
// You can alternatively use getenv() yourself.
$config = new Config();

// Get the credentials to connect to the Memcached service.
$credentials = $config->credentials('memcached');

try {
    // Connecting to Memcached server.
    $memcached = new Memcached();
    $memcached->addServer($credentials['host'], $credentials['port']);
    $memcached->setOption(Memcached::OPT_BINARY_PROTOCOL, true);

    $key = "Deploy day";
    $value = "Friday";

    // Set a value.
    $memcached->set($key, $value);

    // Read it back.
    $test = $memcached->get($key);

    printf('Found value <strong>%s</strong> for key <strong>%s</strong>.', $test, $key);

} catch (Exception $e) {
    print $e->getMessage();
}
import pymemcache
from platformshconfig import Config


def usage_example():

    # Create a new Config object to ease reading the Web PaaS environment variables.
    # You can alternatively use os.environ yourself.
    config = Config()

    # Get the credentials to connect to the Memcached service.
    credentials = config.credentials('memcached')

    try:
        # Try connecting to Memached server.
        memcached = pymemcache.Client((credentials['host'], credentials['port']))
        memcached.set('Memcached::OPT_BINARY_PROTOCOL', True)

        key = "Deploy_day"
        value = "Friday"

        # Set a value.
        memcached.set(key, value)

        # Read it back.
        test = memcached.get(key)

        return 'Found value <strong>{0}</strong> for key <strong>{1}</strong>.'.format(test.decode("utf-8"), key)

    except Exception as e:
        return e

Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community

In accordance with the 2006/112/CE Directive, modified on 01/01/2015, prices exclude VAT. VAT may vary according to the customer's country of residence.