Preface
Recently, I met memcache again at work. Everyone should know that memcache is an efficient distributed memory object caching system. It can support the placement of various data (arrays, objects, basic data types) of php in the memory it manages. This article will introduce to you the basic methods of php to operate memcache cache in detail. Without further ado, let’s take a look at the detailed introduction.
1. Code usage
<?php //connect $mem = new Memcache; $mem->connect("127.0.0.1", 11211) or die ("Could not connect"); //Show version $version = $mem->getVersion(); echo "Memcached Server version: ".$version."<br>"; //Save data $mem->set('key1', 'This is first value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val ."<br>"; //Replace data $mem->replace('key1', 'This is replace value', 0, 60); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //Save array $arr = array('aaa', 'bbb', 'ccc', 'ddd'); $mem->set('key2', $arr, 0, 60); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //Serialize the array. During network transmission, in order to ensure that the data type is not lost, serialize it first and then happen. //$arr1=serialize($arr); $arr2=json_encode($arr1); print_r($arr2); //Delete data $mem->delete('key1'); $val = $mem->get('key1'); echo "Get key1 value: " . $val . "<br>"; //Clear all data $mem->flush(); $val2 = $mem->get('key2'); echo "Get key2 value: "; print_r($val2); echo "<br>"; //Close the connection $mem->close(); ?>
2. Detailed explanation of the function library:
- Memcache::add - Add a value, if it already exists, returns false
- Memcache::addServer - Add a server address to use
- Memcache::close - Close a Memcache object
- Memcache::connect - Create a Memcache object
- memcache_debug - Control debugging function! [4 F8 C2 e, X
- Memcache::decrement - Subtract the value in a saved key
- Memcache::delete - Delete a key value
- Memcache::flush - Clear all cached data
- Memcache::get - Get a key value
- Memcache::getExtendedStats - Get the running system statistics of all processes in the process pool
- Memcache::getServerStatus - Get the parameters for running the server
- Memcache::getStats - Returns some running statistics of the server
- Memcache::getVersion - Returns the version information of the running Memcache
- Memcache::increment - adds the value in a saved key
- Memcache::pconnect - Create a persistent connection object for Memcache
- Memcache::replace - Overwrite an existing key
- Memcache::set - Add a value, override if it already exists
- Memcache::setCompressThreshold - Compress data larger than a certain size
- Memcache::setServerParams - Modify server parameters at runtime
3. Distributed use:
Memcache function library is in PECL (PHP Extension Community Library). Its main function is to build a temporary storage area for large-capacity memory data. Its function is very obvious when distributed.
<?php //1. Instantiate a Memcache object $mem=new Memcache(); //2. Connect to the specified memcache // $mem->connect("127.0.0.1",11211); //If our website requires multiple memached cache systems, use distributed $mem->addServer("192.168.1.100",11211); $mem->addServer("192.168.1.200",11211); $mem->addServer("192.168.1.222",11211); //Create multiple memcache services using addServer and will automatically be placed into each server according to the load balancing algorithm. $mem->add("name","zs",0,100); ?>
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.