LoveDuckie

SilverStripe: Integrating Memcached Caching with Docker

SilverStripe: Integrating Memcached Caching with Docker

SilverStripe: Integrating Memcached Caching with Docker

SilverStripe: Integrating Memcached Caching with Docker

Updated 3 years ago
2 Minute(s) to read
Posted 3 years ago Updated 3 years ago 2 Minute(s) to read 0 comments

In this blog post, I am going to demonstrate how you can hook up an existing memcached server instance with SilverStripe. The purpose for this is so that you can in-memory based caching, instead of the default alternative that is available which is a file-based OpCache solution that SilverStripe uses as the fallback.


Configuration

There is already existing documentation on the SilverStripe pages for configuring memcached, but it makes use of the YAML-based configuration engine along with SilverStripe's type injector for instantiating the type. For some reason, this configuration sample doesn't work despite being a convincing solution.

Instead, for my solution, I will be making use of the _config.php file for writing the code explicitly for instantiating the memcached caching interface adapter. It looks something like this.

<?php
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Cache\MemcachedCacheFactory;

$cacheClient = new Memcached();
$cacheClient->addServer('your-memcached-server-hostname-goes-here', 11211);
$cacheFactory = new MemcachedCacheFactory($cacheClient);

Injector::inst()->registerService($cacheFactory, "SilverStripe\\Core\\Cache\\CacheFactory");
Injector::inst()->registerService($cacheFactory, "CacheFactory");
Injector::nest();

You can alternatively find the Gist snippet for this configuration here. Afterwards, all you need to do is simply navigate to /dev/build?flush=all, and it should automatically work providing that you have correctly configured your memcached server.

What does it do?

Basically this configuration updates the class manifest of the Injector service, to say that should a type be instantiated named "CacheFactory" or more specifically SilverStripe\Core\Cache\CacheFactory, you should the instantiate the MemcachedCacheFactory instead. All we're doing in the snippet above is instantiating the adapter, or configuration for memcached with the relevant properties, and then ensuring that it is used everywhere in the Silverstripe project.

The injector is globally present service that is responsible for instantiating objects of every detectable type in the project.

Refer to the section below if you need further information on running this with Docker.


Docker

If you're using Docker, you might also appreciating using this docker-compose configuration. This is how best to describe a memcached service if you are intending on using it with your SilverStripe instance.

version: '3.9'

services:
  memcached:
    container_name: '${SERVICE_NAME}-memcached'
    image: memcached:1.6.9-alpine
    restart: always
    ports:
      - "11211:11211"
    environment:
      MEMCACHED_MEMUSAGE: 256
      MEMCACHED_THREADS: 8

Using this docker-compose configuration is as simple as running the following command.

#!/bin/bash
docker-compose up memcached

And that's it. Leave a comment if you need any more information.


Programming Languages:

PHP


Comments

Comments