Memcache is great for storing slow queries that return small data sets or a lot, it is a high-performance, distributed memory object caching system, primarily intended for fast access to cached results of data store queries.
Basically, memcache allows you to store any form of data in a ‘temporary cache’ so whenever you perform a database query, instead of just connecting to the database and getting the data, you check memcache to see if the data is already stored in the cache. If you do not get any data, you can then go to the database and execute the query.
Before using memcache, make sure you check the time your normal query executes and compare it with a cached version. Sometimes, memcache may be a bit slower running high yield queries especially if the database is already caching items.
Once you have memcache installed on your server, you can start caching stuffs! You can download memcache here memcached.org/
Here are some of the functions you might use in memcache
get() : gets the value for a key
set() : sets a key with a given value
add() : adds to the cache, only if it doesn’t exist
replace() : sets in the cache only if the key already exists
flush() : removes all keys and cached data
Lets look at a simple example
// new Memcache instance $memch = new Memcache; // connecting to our server on port 11211 $memch->connect('127.0.0.1', 11211) or die ("Could not connect"); //now we will create a key to save our data $agekey = "UserAge"; // the data contained in the $agekey variable $agedata = "24"; $memch->set($agekey, $agedata, true, 200); // Store the result of the query for 200 seconds // This will get the data back from memcache $result = $memch->get($agekey); echo $result; //this will return 24
The code above shows a simple implementation of memcache and how to store and receive data from memcache. Now let’s do a real life example with SQL queries.
Let’s say we have a query to retrieve data from the database, how do we implement memcache on that? In the code below, I’ll explain how to use memcache to retrieve results and what to do when the result is not cached.
//Lets assume we have this sql query that retrieves the ages of 20 people we want to execute $sql = "SELECT * FROM Members WHERE age > 16 LIMIT 20"; // we will create the hash key for that query $agekey = md5($sql); // next we connect to memcache server $memch = new Memcache; $memch->connect('127.0.0.1', 11211) or die ("Could not connect"); //we need to findout if we already have the ages cached $result = $memch->get($agekey); // If we find the ages in memcache, return the cache result if(!empty($result)) return $result; // if we cant find the cached result, we connect to the db mysql_connect("localhost", "username", "password") or die(mysql_error()); mysql_select_db("agedb") or die(mysql_error()); // Run the SQL query on the db $result = mysql_query($sql) or die(mysql_error()); // Next we save the result to memcache so that we can find it next time $memch->set($agekey, $result, true, 200); //remember I'm storing it in memcache for 200 seconds or I use //$memch->set($agekey, $result, false, 0); to store it for a very long time //then we return the result return mysql_fetch_array( $result );
It’s that easy. I hope you now have an idea of how memcache works and how to use it. Google “memcache” if you want to learn more about it. Remember, Facebook uses Memcache aggressively.
For full codes Go To Source: StackArena