Using in memory caching on PHP. The easy way.

With memcached you can store array and other data in server memory. For example, you can use memcache to store data from a mysql_query in memory to expire some time in future. Using memcache you connect to database or other resource only when needed.

I’m using memcache to store mysql querys to reduce connection on database, but, you can use to store ldap, imap and any other data type. But careful with complex objects like XML, some times the object get corrupted, becouse XML use very complex in-memory data structures.

Here is two function use to simple put and get data in memcached daemon:

// connect to a memcache server on localhost
if( class_exists('Memcache') ){
	$memcache = new Memcache;
	if(!@$memcache->connect('localhost', 11211)){
		unset($memcache); // no valid memcache connection
	}else{
		define("memcache",true); // enable memcache use
	}
}
 
// store data on cache memory for five minutes (default ins 300 seconds)
function cache_set($key,&$data,$ttl=300){
	global $memcache;
        // here is the trick, set timestamp in the future.
	$ttl = strtotime("+{$ttl} seconds");
	if(!defined("memcache")) return false;
	return $memcache->Set($key, $data, false, $ttl);
}
// get pointer for data in memory, if expired return false
function &cache_get($key){
	global $memcache;
	if(!defined("memcache")) return false;
	return $memcache->Get($key);
}
?>

And, here how to use the code above:

<?php
 
$my_data = cache_get("my_key");
 
// if the cache has expired, fetch data from database
if( $my_data === false ){
      // connect to mysql and fetch data
      $sql = "select * from mysql.users";
      $tmp_resource = mysql_query($sql) or die(mysql_error());
      while( $tmp_row = mysql_fetch_assoc($tmp_resource) ){
            $tmp_data[] = $tmp_row;
      }
      // now, store tmp_data on memory cache for five minutes (300 seconds)
      cache_set("my_key",$tmp_data,300);
      // link tmp_data with my_data variable
      $my_data =& $tmp_data;
}else{
      // do noting, my_data has been loaded from memory cache
}
print_r($my_data);
?>

How to enable memcache on you phpinstalation.

First, you need to install the service, on Microsoft Windows, you can install the “memcached for Win32″, this port is easy to install and configure. On Linux Centos you can simple use yum, if you have online repository configure, use this command: yum install memcached or if not, you can install manual following tips on ”
Memcached: How do you install memcached? (CentOS 64 bit, Linux, Redhat, Fedora)”. After installation check if service is running, on Microsoft Windows check on taskmanager for the service memcached or on Linux simple type in console: ps ax | grep memcache and check the process.

After installing memcached on you system, you need to enable de php_memcached.dll extension on you Microsoft Windows installation or php_memcachd.so on Linux. Restart Apache and test function.

  • Share/Bookmark

Post a Comment

Your email is never shared. Required fields are marked *

*
*