rss feed Twitter Page Facebook Page Github Page Stack Over Flow Page

Memcached

Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Install

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install php5-memcache memcached php5-memcached
sudo service memcached start

Config

You can change the memcached config file for better server tuning:

sudo vi /etc/memcached.conf

Then, modify the config to have optimal settings:

modify -m 64 to -m 128

Restart memcached service

sudo /etc/init.d/memcached restart

PHP Module

The module should be enabled by default, if not you can modify:

sudo vi /etc/php5/apache2/conf.d/20-memcached.ini

Un-comment the next line to enable the module:

extension=memcached.so

Or you can customize it. Here's a good example: https://github.com/php-memcached-dev/php-memcached/blob/master/memcached.ini

Restart Apache

sudo /etc/init.d/apache2 restart

Test

Here's a simple script that will allow testing memcached:

<?php
  $mc = new Memcached();
  $mc->addServer("127.0.0.1", 11211);

  $result = $mc->get("test_key");
  if($result) {
    echo $result;
  }
  else {
    echo "No data on Cache. Please refresh page pressing F5";
    $mc->set("test_key", "test data pulled from Cache!") or die ("Failed to save data at Memcached server");
  }