Memcache is the module provides the handy procedural and object oriented interface to memcached highly effective caching demon, which was especial designed to decrease the database load in dynamic websites.
Apart from decreasing the database load it also provides the session handler(memcache).
Memcached is the open source distributed memory object caching system it helps you to speedup web applications by reducing the database interactions.
The system uses the libmemcached library to provide an API for communicating with Memcached servers, by the way Memcached uses a different port 11211, rather than using the default port i.e 80 used for the web server.
Memcached has an extra letter “d” at the end, the naming convention is followup of Memcache, in some applications the piece of work that does the background work is called “deamon”, Memcache module utilizes this daemon directly, Memcache module provides the handy procedural and object oriented interface to Memcached.
Enough for Memcached, lest come back to Memcache, now lets see how to use Memcache. Before we use it first we have install it, I will take the example of ubuntu linux.
Steps for Installation
- First of all update the linux.
sudo apt-get update
- Now install the demon “memcached”.
sudo apt-get install memcached
- Next install the php-pear, it will help to store the cache
sudo apt-get install php-pear
- Install the build-essential.
sudo apt-get install build-essential
- Finally install the memcache.
PECL (PHP Extension Community Library)
sudo pecl install memcache
Press enter for yes, when system prompts
“Enable memcache session handler support? [yes] :”
- When you are finished installation of memcache with PECL, add “memcached” to memcache.ini
echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini
Hurry installation is finished, now you can check that it has been installed by searching for it.
ps aux | grep memcache
How Memcache works?
Before we go for using the memcache, lets take detour for a while to see that, “how memcache works?”.
When we send request to the server, at first attempt server tries to search the data in cache, before it goes and start searching the data in database, if the data is not found in the cache it, it goes by the usual way to retrieve data from the database and saves the data in the cache for certain amount of time and replies the desired data, by caching the requested data, future requests for the same data will not have to go through all the processes to retrieve the data from the database and hence access it through cache and save the time and resources.
How to use Memcache?
Now the real work begins, we will use a simple PHP script in order to explain the Memcache, have a look at the below script is simple and easy, it will give you a broad idea of the working of the script, then we will do autopsy of the script in order to understand actually how Memcache is helps to save the time and resources.
<?PHP
$meminstance = new Memcache();
$meminstance->pconnect('localhost', 11211);
mysql_connect(“localhost”, “test”, “testing123”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());
$query = “select id from example where name = ‘new_data'”;
$querykey = “KEY” . md5($query);
$result = $meminstance->get($querykey);
if (!$result) {
$result = mysql_fetch_array(mysql_query(“select id from example where name = ‘new_data'”)) or die(‘mysql error’);
$meminstance->set($querykey, $result, 0, 600);
print “got result from mysql\n”;
return 0;
}
print “got result from memcached\n”;
return 0;
?>
In the first line only you will see that an object has been created for the memcache, if you have not installed the memcache properly or for any reason if it is not working, then this line will throw the error, so please make sure sure that memcache is installed and working properly.
Now next up to 5th line of code I don’t think that these lines need to explained, in the sixth line, we have encrypted the SQL query in order to create the key so that the matching data can be retrieved from the database or if already in the cache we can retrieve it from the there or either we can also store it into the cache, this is exactly what the above PHP script is doing.
Now lets see the next line, here we are trying to fetch the maching data from the cache using the same that we have created above, actually programmer is trying his luck, to find if matching data is already in the cache.
Now in the next line in the if condition, programmer if checking if the get function of the memcache object returns any thing, if not then the data will be retrieved from the database and saved to the cache using the set function of the memcache object.
Conclusion
Till now you might have got the pretty clear picture on how the memcache works, by seeing this simple program you might be curious to know that, just it it?, no my friends there are few more functions like “delete”, “flush”, “add” etc. and couple of configuration settings like, max_failover_attempts, hash_function, save_handler, save_path and few more.
I will try to write more on functions and configurational settings soon, till then you can go through below URLs for more information on functions and configuration settings.
- http://www.php.net/manual/en/class.memcache.php
- http://www.php.net/manual/en/memcache.ini.php