Integrate memcached into application

0
Your rating: None

Memcached specially used for query caching, content caching and session storage. It is a very good option for increasing performance and scalability on any website.
Installation For Linux
We require an instance of memcached for storing our data. Unix/Linux user can download the source directly from  Danga Site and follow the instructions there in for installation. However, some distributions (like Ubuntu and CentOS) have memcached in their repositories, so you can use the native package installer like apt or yum to install memcache.
Once you have memcached installed, you need a way to talk to it. This is where the  client APIs  come into picture. We will be using the PHP API, so here I'll show you how to install the PHP memcached extenstion. The easiest way to do this is using the 'pecl' command:
pecl install memcache
this will download, compile, and install the extension. Just add the line 'extension=memcache.so' to php.ini file and  move the memcache.so file into the extensions/ directory which is usually located at /usr/local/lib/php/extensions.
Integrate memcached into our application
First connect to our memcached server:
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
Once we have got a connection, here is an example how to implement memcache:
$sql = "select * from name where id=1";
$query = mysql_query($sql) or die(mysql_error()." : $sql");
$result = mysql_fetch_object($query);
$lastname = $result->lastname;
The above query fetches the 'lastname'' field from our name table. Now, if the data in the lastname field does not change very often, we can definitely do caching.
So to integrate memcached you need to implement this following code:
//write query
$sql = "select * from name where id=1";
//create an index key for memcache
$key = md5('query'.$sql);
//lookup value in memcache
$result = $memcache->get($key);
//check if we got something back
if($result == null) {
//fetch from database
$query = mysql_query($sql) or die(mysql_error()." : $sql");
if(mysql_num_rows($query)> 0) {
$result = mysql_fetch_object($query);
//store in memcache
$memcache->set($key,$result,0,3600);
}
}
$lastname = $result->lastname;
So first checks to see if we can find whatever we are looking for in memcache, and if we can't find it, we fetch it from the database and use the result to populate the cache. In this example, I stored the entire $result object in cache and set its expiration to 3600 seconds (1 hour). The third flag in the set() function deals with whether to compress the data or not.
 
 
 

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.

Cumulus Tag Cloud

Kiran Says

SEO Architect,SEO Specialist reflect my values.

By enduring black hat methods the SEO industry is setting itself up for washout and sleepwalking into oblivion.

Jobs in India