How to integrate Memcache In PHP

What is Memcached?

Memcached is a distributed memory caching system. It speeds up websites having large dynamic databases by storing database objects in Dynamic Memory to reduce the pressure on a server whenever an external data source requests a read. A Memcached layer reduces the number of times database requests are made.

Why Memcached?

It decreases the response time of your web pages, which in return enhances the overall customer`s experience. A better response time allows users to fetch data seamlessly.


$cache = new Memcache;
$cache->connect(`127.0.0.1`, 11211);

function getPosts()
{
    $cache_key = `getPosts`;
    $posts = $cache->get($cache_key);

    if ($posts === false) {
        $posts = DB::query(`SELECT * FROM posts`);
        $cache->set($cache_key, $posts);
    }

    return $posts;
}

7-August 2:32 PM 356 Views

 Prev question

Next question