PHP Tutorials

How to Implement Memcached with PHP?

memcached with phpThe following is a guide on how to implement a memcached with PHP and at first we must explain what defines the memcache.

This is a high performing caching system, which distributes memory objects. In other words, it is a provision for users to store data into a web page, in a ‘temporary cache’. This saves the user the hassle of having to run database queries and getting data from the database. It there is a memcache available, a user can extract data from the memcache. This method lets user’s construct a highly optimized application.

The following are some functions that mainly utilize memcache:

  1. Get()- specifies given value to a key
  2. Set()- assigns  a specific value to a key
  3. Add()- adds value to non existing cache
  4. Replace()- sets information into the cache, if the key is already existent
  5. Flush()- erases all the data stored in keys and cache

Who Utilizes Memcache?

Currently, there are many websites tapping into the power of memcached. Some popular sites such as:

  1. Twitter
  2. Digg
  3. YouTube
  4. Facebook

are utilizing this powerful resource. This is a popular programming language and it is ready made in order to work quickly and efficiently. It is mostly associated with optimizing web applications. In most cases, programmers use memcached as:

  • A log that records users’ sessions. This is as opposed to using files in the case of PHP
  • A cache for viewing data, results of SQL queries and pages that are generated

How can PHP be installed?

There are three methods available. One has the option of installing from the source files, using a secure deb package or using the automatic process.

How can memcached be used with PHP?

Now after installing memcached, it is time to use it. The first step is creating MCache.php in the server. After this, the next step is to paste a code into the file. The following is the code:

Class BrianmemCache {
Public $ET = 90; // this is for setting the expiry time
Protected $isactive = false; /* Brianmemcache isactive */    this is for enabling the Brianmemcache
Protected $objCache = null;
Private function _construct() {
If  (class_exists(‘Brianmemcache”))  {
$this-> objCache = new Brianmemcache();
If(!$this->objCache->connect(‘Bosshost’,11211)) {
$this->isactive = true;

Private function getDetails()  {
//set the key and check the cache
$BrianmemcacheKey = md5(“SELECT * from user_info where user_id=’11’”);
$BrianmemcacheResult = $this-> objCache  ->get($cacheKey);
If  ($BrianmemcacheResult) {
Echo $BrianmemcacheResult[ ‘username’];
Echo $BrianmemcacheResult[‘email_id’];
} else {
Echo  $data[‘username’].”-“.$data[‘password’] .”-“.$data[“email_id’].”d’];

Private function setData()  {
$query=”SELECT * from user_info where user_id=’11’ “;
$result  =  msql_query ($query);
$row  =  msql_fetch_array ($result):
//Using MEMCACHE_COMPRESSED to store zlib compressed items
$data  =  $this->objCache->set($cacheKey, $row, 0, $this->ET);
// 1 minute result storing
Return $data:

// Cache server data deletion

Function deleteData($cacheKey)  {
$success_val  =  $this-> objCache  ->delete($cacheKey);
Return $success_val;

The second step entails creating another file, known as the connection.php. the following code is pasted on the file.

$hostname  = “localhost”;
$user = “root”;
$password = “ “;
$database = “webtwo”;
$conn  =  msql_connect(hostname, $user, $password)
Or die(“It didn’t work, buddy.”);
Mysql_select-db($database, $conn)  or die(: Ops ! ! Could not reach the database”);

This is the ideal example to illustrate the way memcache works. it enables  optimization of w applications and gives some great results.

Monitoring Memcached server

This is a very important aspect. Users must monitor the server, to ensure everything is running the right way. An effective tool for Memcache server monitoring is the One-file PHP script. This is a two in one tool, where it provides essential information about the memcache and does the monitoring. Something important to note is that software that specialize in monitoring of servers have an association with Memcached. This shows the power of this tool, its capabilities and reliability.

Uses of Memcached in PHP

PHP contain two built-in API that are object oriented. This allows access to the Memcached server.

Memcached requires new implementation as well as requiring an additional libememcached library. This in turn provides features, which include:

  • Optional use of binary protocols

Memcached is also not recommended for use in old libraries.

Wrap up

From the above article and examples, it is clear that Memcached is a powerful tool, full of immense capabilities. It was able to enhance the production of subpages, even when the pages are loaded with heavy applications. There was a hug increase in the buffer size, notwithstanding the size of RAM. This was with the help of several servers. Another interesting characteristic of Memcached is the server configuration. It may sound like a difficult task, but the instructions from the authors are simple to follow and understand. They have outlined the instructions in various writing languages, to make it easier for various users to unlock the potential of Memcache.

Leave a Reply

Your email address will not be published. Required fields are marked *