Memcached (Object cache)
Back to home
On this page
Memcached is a simple in-memory object store well-suited for application level caching.
See the Memcached documentation 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. Upsun recommends using Redis if possible but Memcached is fully supported if an application favors that cache service.
Supported versions
You can select the major and minor version.
Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.
- 1.6
- 1.5
- 1.4
Relationship reference
Example information available through the PLATFORM_RELATIONSHIPS environment variable
or by running upsun relationships.
Note that the information about the relationship can change when an app is redeployed or restarted
or the relationship is changed.
So your apps should only rely on the PLATFORM_RELATIONSHIPS environment variable directly rather than hard coding any values.
{
"service": "memcached16",
"ip": "169.254.228.111",
"hostname": "3sdm72jgaxge2b6aunxdlzxyea.memcached16.service._.eu-3.upsunapp.com",
"cluster": "rjify4yjcwxaa-master-7rqtwti",
"host": "memcached.internal",
"rel": "memcached",
"scheme": "memcached",
"type": "memcached:1.6",
"port": 11211
} Usage example
1. Configure the service
To define the service, use
the memcached type:
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: memcached:<VERSION>Note that changing the name of the service replaces it with a brand new service and all existing data is lost. Back up your data before changing the service.
2. Add the relationship
To define the relationship, use the memcached endpoint
:
applications:
# The name of the app container. Must be unique within a project.
<APP_NAME>:
# Relationships enable access from this app to a given service.
relationships:
<RELATIONSHIP_NAME>: "<SERVICE_NAME>:memcached"
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: memcached:<VERSION>You can define <SERVICE_NAME> and <RELATIONSHIP_NAME> as you like, but it’s best if they’re distinct.
With this definition, the application container (<APP_NAME>) now has access to the service via the relationship <RELATIONSHIP_NAME>.
For PHP, enable the extension for the service:
applications:
# The name of the app container. Must be unique within a project.
<APP_NAME>:
# PHP extensions.
runtime:
extensions:
- memcached
# Relationships enable access from this app to a given service.
relationships:
<RELATIONSHIP_NAME>: "<SERVICE_NAME>:memcached"
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: memcached:<VERSION>For Python, include the proper dependency:
applications:
# The name of the app container. Must be unique within a project.
<APP_NAME>:
# Build dependencies per runtime.
dependencies:
python:
python-memcached: '*'
# Relationships enable access from this app to a given service.
relationships:
<RELATIONSHIP_NAME>: "<SERVICE_NAME>:memcached"
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: memcached:<VERSION> Example Configuration
App and Service configuration
applications:
# The name of the app container. Must be unique within a project.
myapp:
# Relationships enable access from this app to a given service.
relationships:
memcachedcache: "cachemc:memcached"
services:
# The name of the service container. Must be unique within a project.
cachemc:
type: memcached:1.6 Use in app
To use the configured service in your app, add a configuration file similar to the following to your project.
applications:
# The name of the app container. Must be unique within a project.
myapp:
# The location of the application's code.
source:
root: "myapp"
# Other options...
# Relationships enable an app container's access to a service.
relationships:
memcachedcache: "cachemc:memcached"
services:
# The name of the service container. Must be unique within a project.
cachemc:
type: memcached:1.6This configuration defines a single application myapp, whose source code exists in the directory <PROJECT_ROOT>/myapp, and has been provided access to the service (cachemc) via the relationship memcachedcache.
From this, myapp can retrieve access credentials to the service through the environment variable PLATFORM_RELATIONSHIPS.
That variable is a base64-encoded JSON object, but can be decoded at runtime (using the built-in tool jq) to provide more accessible environment variables to use within the application itself:
# Decode the built-in credentials object variable.
export RELATIONSHIPS_JSON=$(echo $PLATFORM_RELATIONSHIPS | base64 --decode)
# Set environment variables for individual credentials.
export CACHE_HOST=$(echo $RELATIONSHIPS_JSON | jq -r ".memcachedcache[0].host")
export CACHE_PORT=$(echo $RELATIONSHIPS_JSON | jq -r ".memcachedcache[0].port")
# Surface a Memcached connection string for use in app.
export CACHE_URL="${CACHE_HOST}:${CACHE_PORT}"The above file โ .environment in the myapp directory โ is automatically sourced by Upsun into the runtime environment, so that the variable CACHE_URL can be used within the application to connect to the service.
Note that CACHE_URL, and all Upsun-provided environment variables like PLATFORM_RELATIONSHIPS, are environment-dependent. Unlike the build produced for a given commit, they can’t be reused across environments and only allow your app to connect to a single service instance on a single environment.
A file very similar to this is generated automatically for your when using the upsun ify command to migrate a codebase to Upsun.
Accessing Memcached directly
To access the Memcached service directly you can use netcat as Memcached doesn’t have a dedicated client tool. Assuming your Memcached relationship is named cache, the host name and port number obtained from PLATFORM_RELATIONSHIPS would be cache.internal and 11211. Open an SSH session and access the Memcached server as follows:
netcat cache.internal 11211Note that the information about the relationship can change when an app is redeployed or restarted
or the relationship is changed.
So your apps should only rely on the PLATFORM_RELATIONSHIPS environment variable directly rather than hard coding any values.