Enabling Memcached in MAMP4

Make it Speedy!

Categories: Development, WordPress

I do all of my Laravel and WordPress locally using MAMP. I like to keep my development process as simple as possible and MAMP lets me do just that. But sometimes you need just a little bit more than what comes out of the box. Caching is an important part of that development cycle and for that I like to use Memcached.

Getting Memcached running with MAMP is a lot more straightforward than it used to be but there are still a few gotchas.

First of all, you will need to install Memcached and since OS X doesn’t have a package manager I like to use [Homebrew]https://brew.sh/). Run brew install memcached.

Next you will need to enable Memcached in your php.ini located /Applications/MAMP/bin/php/php7.x,x/conf/php.ini, x.x being the version of PHP that you are running.

Search for ; Extensions and add or uncomment the following.

extension=igbinary.so
extension=memcached.so

Now that MAMP setup you can run it as a daemon in the background from the terminal.

memcached -d

Restart MAMP and you’re off to the races!

Bonus

In order to use Memcached with Laravel simply open the config/cache.php file and modify your Memcached settings ( in my case default worked fine ).

'memcached' => [
    [
        'host' => '127.0.0.1',
        'port' => 11211,
        'weight' => 100
    ],
],

Now you could do something like this:

if (Cache::has('key')):
    return Cache::get('key');
else:
    $data = ['some' => 'data'];
    Cache::put('key', $data, 15);
    return $data;
endif;

If our key is set then we can pull from the cache and return the value, otherwise add it to cache and save it for 15 minutes. Using caching effectively will allow your applications to be tuned and performance will go up. Testing and improving performance with WordPress is especially important due to its bloated and often clunky implementations and different ideologies.