http://www.developer.com/open/article.php/3901666/Getting-Started-with-Memcached-Distributed-Memory-Caching.htm
Wikipedia describes Memcached as a general-purpose distributed memory caching system, but what exactly does the term Memcached mean? Cache is memory used to store the most frequently used resources (e.g. browsers store every website visited during a session in cache), because accessing resources from a cache is faster than accessing them from a disk drive. So Memcached means "memorycached," which simply is caching resources in the memory. These resources can be data retrieved from API calls, database operations or even HTML pages. The data is stored in key/value pairs in the form of large hash tables. As distributed system is part of the Memcached definition, you can install Memcached on various servers to make a larger caching server. In this way, Memcached helps reduce database loads to a minimum, resulting in faster and more responsive Web applications. Figure 1 shows how Memcached works when used with database. Figure 1 will be familiar to anyone who has ever written a script that interacts with a database. Here is the step-by-step explanation of how it works when you want to pull certain data from the database: Apparently, Memcached is best implemented for queries that are triggered multiple times in a second and demand huge data as output. Access to Memcached data is faster than the access time to disk drives because the Memcached data is stored in temporary memory. You will find Memcached preinstalled on almost any production server. But if you are trying Memcached for the first time, the first step is to install the Memcached extension on your server. First, check whether Memcached is already installed on your system using this command: If Memcached is installed, the command will output the current version. Otherwise, it will return an error. The next step is to install Memcached. The following command installs Memcached on a CentOS Linux distribution. It will search the packages and install the latest version of Memcached if not install already. When should you use Memcached and when should you avoid it? By now you might have figured that Memcached was designed to shed the load from the database. But you should implement this cache system with a strategy to ease more expensive queries as well. You can also write a log that records the query execution time, which will then help you to analyze performance. For instance, suppose you operate an e-commerce site. You can cache a query that fetches the description, shipping options and availability of different products by firing a complex query (involving joins, etc.). So every time the product page is loaded the database query is skipped and the result is fetched from cache. Certainly, caching a query like this will improve the overall performance of the webpage. You would just need to update the product details when they change. Sometimes caching your queries is not a good idea, however. This may be the case when a database operation involves more update queries than fetch queries. So, every time the database is updated, you need to update the cache as well, which causes the overall performance of the Web application to drop. Hence, querying the database becomes a better solution than using a cache solution. If you follow how Memcached works, you might have noticed that the data that gets stored in the cache resides in the memory and the memory does not have any access-level control over it. But if your data is not crucial, you should not worry much about security. However, you can secure your cache data in the following ways if you need to: Let's see some Memcached code in action and learn how to make it work using the PHP code snippet below. It is a class that shows how to retrieve data from the cache. If that data does not exist, then it queries the database and stores it to cache for subsequent usage. To use this code in your development, make sure you have installed the Memcached PHP extension. In the latest version of PHP the extension is bundled with the PECL and PEAR packages. Memcached is a powerful tool that various Web applications such as Wikipedia, Flickr and Digg all use. If using Memcached can dramatically increase the performance of Web applications like these, what are you waiting for? Sachin Khosla is a Web developer, open source technology evangelist and writer. Read his blog at digimantra.com.
Getting Started with Memcached Distributed Memory Caching
September 1, 2010

Figure 1. How Memcached Works: Here is a diagram of how Memcached works when used with database.How Does Memcached Work?
How to Install Memcached?
memcached -h
yum install memcachedWhen -- and When Not -- to Use Memcached
Memcached and Security
Memcached Code in Action
<?phpclass MyCache{ private $cache; function __construct() { $this->cache = new Memcache(); // you can replace localhost by Memcached server IP addr and port no. $this->cache->connect('localhost', 10987); } function get_data($key) { $data = $this->cache->get($key); if($data != null) return $data; else { if($this->cache->getResultCode() == Memcached::RES_NOTFOUND) { //do the databse query here and fetch data $this->cache->set($key,$data_returned_from_database); } else { error_log('No data for key '.$key); } } }}$cache = MyCache();$cache->get_data('foo');?>Summary
About the Author