Drupal Memcache Accessing caches from Slabs and Deleting using keys.

What is  Memcache:

Memcached is an open source, distributed memory object caching system that alleviates database load to speed up dynamic Web applications. 
Memcached stores data according to a key. Data can be of various sizes so you can store small bits (like 1 record pulled from the database) or you can store huge chunks of data (like hundreds of records, or entire finished html pages).

Memcache in Drupal:

In Drupal cache data are stored in Cache tables(Tables prefix has starts with cache and followed by type of table Ex:cache_CACHE_TYPE). In increase caching mechanism in Drupal, Memcache server used to store cache table instead of Database.

Memcache https://www.drupal.org/project/memcache is a module used to setup memcache in Drupal.

List all Memcached records:

Fetch all records from memcache server. 

$memcache = new Memcache; // Default class.
$memcache->connect(SERVER_IP, PORT_NO) or die ("Could not connect to memcache server");
$list = array();
$allSlabs = $memcache->getExtendedStats('slabs');
$items = $memcache->getExtendedStats('items');
foreach($allSlabs as $server => $slabs) {
	foreach($slabs AS $slabId => $slabMeta) {
		$cdump = $memcache->getExtendedStats('cachedump',(int)$slabId);
		foreach($cdump AS $keys => $arrVal) {
			if (!is_array($arrVal)) continue;
			foreach($arrVal AS $k => $v) {             	 
				$data[] = $k;
			}
		}
	}
}  
// Print Records.
print_r($data);

Clearing particular memcache:
The dmemcache_key() used to delete memcache record. Parameters are Unique key and cache type.  

$memcache_key_path = dmemcache_key($key, $memcache_type_path); 

Example:
Clearing particular node path cache from Memcache server. 

$nid = NID;
global $base_root, $base_path, $base_url, $_memcache_statistics;
$node = node_load($nid);
$url = array();
$url = 'node/'. $nid;
$memcache_type_path = 'cache_path';
$memcache_obj_path = dmemcache_object($memcache_type_path);
$memcache_key_path = dmemcache_key($url, $memcache_type_path);
Category: