How to use memcache

What’s up! This time I’m going to show you how to use memcache.
Memcache is basically an extension for php which allows you to store a variable, or an array temporarily in memory. It’s basically like sessions and cookies. But the difference is that you can actually set the memory which it will use. When I say memory, its not the space that can be consumable in the hard drive. I’m talking about the RAM.
If you have learned the basics of computer, you know that the RAM is faster than the hard disk. That is when it comes to reading and writing data. And that leads us to the conclusion that we can actually use memcache to optimize the performance of the system that were going to develop using php. The most common thing that we can think of optimizing is the fetching of data from the database.

 

Installation

There’s an awesome guy who actually made the life of the folks who wants to use memcache on windows easier. And here’s a link to his blog, come back here once you successfully get memcached to run on windows:

http://shikii.net/blog/installing-memcached-for-php-5-3-on-windows-7/

 

Usage

PHP.net has actually a tutorial on memcache:

http://us3.php.net/memcache

After reading all of that and trying the examples, its just a trivial matter how you’re going to do it to optimize the performance of your application.

But I’m writing this for the sake of those who can’t still figure out what to do.

As usual, you need to include your database configuration file;

require_once('db_config.php');

Declare an object of the memcache class:

$memcache = new Memcache;

Then connect to our memcache server. The default port used by memcache is port 11211.

$memcache->connect('localhost', 11211) or die ("Could not connect");

Next, call up the memcache get() function. This will fetch all of the data associated with the key. In this example the key will contain the data from our database.

$geto = $memcache->get('key');

Declare the variables where we will assign the title($header), and the data which is being held by memcache($assign)

$assign = '';
$header = '';

Then write an if statement which will check if something is stored in memcache. We assigned it earlier to the variable $geto so we only need to check if that variable is not empty. If its not, then we copy its contents to the variable $assign. If its empty, then we fetch data from our table. And assign the fetched data to the key. This key is being assigned to the variable $geto, which later on serves as a basis if were going to perform a database query everytime we refresh the page. This method significantly reduces server load if there are lots of data stored in the database.

if(!empty($geto)){
	$assign = $geto;
	$header = 'This is from memcache';
}else{
	$bus = $db->get_results("SELECT * FROM tbl_businesstaxpayer"); 
	$memcache->set('key', $bus, false, 10);  //10 seconds
	
	$assign = $bus;
	$header = 'This is from database';
}

And don’t forget to specify the amount of time your script is going to want to fetch data from the one stored in the memcache. This can range from 1 second to 20 minutes. But it all depends on how many users are going to use your system, or how much recent the data displayed in front of the users should be.

Lastly, loop through the contents of the $assign variable. Remember that this variable either has the contents of the memcache or the data that has been recently fetch from your database. That’s why I placed the header to have a visual indicator of what data I’m exactly looking at.

<?php if(!empty($assign)){ ?>
	<?php echo $header; ?>
	<hr/>
	<table border="1">
		<tr>
		    <th>Tradename</th>
	                    <th>Address</th>
		    <th>orgType</th>
		</tr>
		<?php foreach($assign as $b){ ?>
		<tr>
		    <td><?php echo $b->strTradeName; ?></td>
		    <td><?php echo $b->strAddress; ?></td>
		    <td><?php echo $b->orgType; ?></td>
		</tr>
	    <?php } ?>
	</table>
<?php } ?>

You can also check if the data is actually fetched from memcache. Try to add data on the table you are fetching from while your script still fetches from the memcache. And see for yourself that it doesn’t contain the last table row that you have created.

 

Conclusion

Memcache is a nice tool to reduce server load and improving performance on your applications.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s