DEV Community

Hakan Torun
Hakan Torun

Posted on

Memcached Installation and PHP Memcache Example

Web servers with high traffic and with high CRUD operations will be under heavy load. In such a case, if the code is written to use resources efficiently enough, one of the first needs to serve under heavy load is a cache mechanism.

What is Memcahced?

Memcached is a cost-effective, open-source, high-performance, distributed caching system written for the popular blogging service LiveJournal, and is often used in Web applications. It is a simple but powerful caching system. Today, Facebook is actively used and contributed to the development. Popular web applications and services (Wikipedia, Youtube, Flickr) are actively used also.

Memcached is an in-memory store. All caching is on memory. Therefore, critical data should not be stored only on memcached. Data on memcached is stored as key-value pairs. Memcached is a life saver, when Web applications that are particularly has high level database operation.

Memcached Installation

In Debian-based Linux distros;

apt-get install php-memcached
apt-get install memcached

In Fedora/RedHat based Linux distros;

yum install php-memcached
yum install memcached

With that commands memcached and PHP-memcached packages can be easily installed. Memcached works on TCP/IP. Communication with memcached can be provided between any softwares.

Starting Memcached Deamon

memcached -d -m 512 -l 127.0.0.1 -p 11211 -u nobody

This command will start a memcached service that uses a cache space of 512 MB and listens on the TCP and UDP port 11211. The cache space created when initializing the memcached service is fixed. Similar to memory management in operating system architectures, it will overwrite the first written data if the cache space is full. You can define a maximum 4GB cache space on x86 systems.

To get more details about the service after starting memcached;

echo "stats settings" | nc localhost 11211

By default, the memcached key lengths are 250 characters. Value can be a maximum of 1MB.

PHP Memcache Example

To connect to memcached with PHP;

$memcache = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Unable to connect to Memcached");

To write data on memcached;

//$exampleValue = 2;
//$exampleValue = "Example String";
$exampleValue = array('0','1','2','3');

$memcache->set('exampleKey', $exampleValue, false, 1800);
//The first parameter is the key, the second parameter value, the third parameter, memcached compression is true/False, the fourth parameter is the cache time in seconds

Get value of a key on memcached;

$exampleValue = $memcache->get('exampleKey');

In a sample scenario, a value that does not change very frequently in our web application is stored in the database. This value will need to be read from the database whenever the page is rendered or in each API call. Let us want to keep the cache with memcached instead of reading this value every time from the database.

//Database connections etc.

$memcache     = new Memcache;
$memcache->connect('127.0.0.1', 11211) or die ("Unable to connect to Memcached");

$exampleValue = $memcache->get('exampleKey');

if($exampleValue === true){
    return $exampleValue;
}
else{
    $exampleValue = $db->get_var(sprintf("SELECT exampleValue FROM %s WHERE id = '%d'",'exampleTable',1));
    if($exampleValue){
        $memcache->set('exampleKey', $exampleValue, false, 1800);
        return $exampleValue;
    }
    else{
        return false;
    }
}

In this way, the disk I/O operations will be reduced due to the database, accessing directly through the memory can be achieved performance gains. The correct implementation of Memcached, the correct selection of data to be cached on Memcached, and the pre-detection of processes that could create a race condition can reduce the load that may occur on Web servers.

Oldest comments (1)

Collapse
 
sadere profile image
Sadere

Its instruction for memcache, not for memcached