Aaron Scherer's Blog

Symfony Reverse Proxy: Memcached

Introduction

user

Aaron Scherer


Symfony2, php, Caching, PHP, programming

Symfony Reverse Proxy: Memcached

Posted by Aaron Scherer on .
Featured

Symfony2, php, Caching, PHP, programming

Symfony Reverse Proxy: Memcached

Posted by Aaron Scherer on .

The Background

Basically, we wanted to be able to use a simple Reverse Proxy (not Varnish, not Squid), that used a Memcache installation, instead of the file system.

The Solution - Short

I wrote a new MemcachedStore class that AppCache now uses instead. The biggest downside is, the configs for the servers isnt used by the AppCache piece.

The Solution - Long

Heres the MemcachedStore class. Right now, its set up to use my AequasiMemcachedBundle symfony bundle at the moment, but it wouldnt be that hard to change to just using Memcached or Memcache.

Place this file in your app directory.

Then, in app/AppCache.php, do the following:

First:

Require the MemcachedStore class at the top

<?php 

require_once __DIR__ . '/AppKernel.php';  
require_once __DIR__ . '/MemcachedStore.php';

class AppCache  
{
    // ...

Second:

Overwrite the createStore() function

<?php  
//...
class AppCache  
{
    // ...
    public function createStore()
    {
        $servers = [
            [ 'localhost', 11211 ]
        ];

        return new MemcachedStore( [
            'enabled'      => true,
            'debug'        => true,
            'persistentId' => serialize( $servers )
        ], $servers );
    }
    // ...
}

Third:

?!?$?

Fourth:

Profit.

Extra Stuff

If you want to create a service to get to this particular store, you would have to create a $cache variable in your app/AppKernel, and overwrite your app/AppCache's handle() method to set the kernel's $cache variable before calling parent::handle.

user

Aaron Scherer

http://aaronscherer.me