What is memcache & nbsp; memcache is a high-performance distributed memory object cache system, by maintaining a unified and huge hash table in the memory, it can be used to store data in various formats, including images, videos, files, and database retrieval results. Memcache is danga. c
What is memcache?
??????? Memcache is a high-performance distributed memory object cache system. by maintaining a unified and huge hash table in the memory, memcache can be used to store data in various formats, including image, video, file, and database retrieval results. Memcache is a danga.com project that was first designed for LiveJournal. it was initially developed to accelerate LiveJournal access and was later used by many large websites. At present, many people around the world use this cache project to build their own websites with high load, to share the pressure on the database.
Why traverse
?? Currently, more and more companies and websites are using memcache. Memcache Client operations generally only provide get, set, and other simple operations, which are very efficient. ?? Although memcache is a key-value storage system, in some cases, we may need to traverse the data of memcache.
How do I traverse memcache ??? Stats command
? Memcache stats commands include:
Traverse through commands
Through these stats commands, we can traverse the content stored in memcache. OK. Now we connect directly to memcache through telnet and use these commands to complete related operations.
Telnet to the memcache server of 192.168.15.225 (Lan testing machine)
Execute the stats items command. you can see many items lines.
Execute the stats cachedump 3 0 command. 3 indicates the number following items in the figure above. 0 indicates that all data is displayed. if it is 1, only one item is displayed.
Is the result after execution, and the string after the item is key
With the key listed above, we can traverse all the data. next we will retrieve a piece of data, and the key is Uc! ULh data.
Here, you may understand how to traverse the memcache data.
Code implementation
?????? The following is a piece of php code for traversing memcache data. for other languages, refer to the code for self-implementation.
connect($host,$port); $items=$mem->getExtendedStats (‘items’); $items=$items["$host:$port"]['items']; foreach($items as $key=>$values){ $number=$key;; $str=$mem->getExtendedStats ("cachedump",$number,0); $line=$str["$host:$port"]; if( is_array($line) && count($line)>0){ foreach($line as $key=>$value){ echo $key.'=>'; print_r($mem->get($key)); echo "\r\n"; } } }?>Extended functions
???? This allows you to query the data of a key with a certain prefix of memcache or the key of some values. Even implement the like function of the database. Note: the operations to traverse memcache are not as efficient as the get operations of memcache.