Basic Method example for operating memcache cache using php, memcache example
Preface
Recently, I encountered memcache in my work. We should all know that memcache is an efficient distributed memory object cache system that supports various php Data (arrays, objects, the basic data type is stored in the memory it manages. This article will give you a detailed introduction to the basic methods for operating memcache cache in php. 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 ("cocould not connect "); // display version $ version = $ mem-> getVersion (); echo "Memcached Server version :". $ version. "<br>"; // Save the 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 "Ge T key1 value :". $ val. "<br>"; // Save the 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, to ensure that the data type is not lost, serialize the array first, occur again. // $ 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 description of the function library:
- Memcache: add-add a value. if it already exists, false is returned.
- Memcache: addServer-add an available server address
- Memcache: close-close a Memcache object
- Memcache: connect-create a Memcache object
- Memcache_debug-control debugging function! [4 F8 C2 e, X
- Memcache: decrement-deletes the value of a saved key.
- Memcache: delete-delete a key value
- Memcache: flush-Clear All cached data
- Memcache: get-get a key value
- Memcache: getExtendedStats-obtains the running system statistics of all processes in the process pool.
- Memcache: getServerStatus-get the parameters of the running server
- Memcache: getStats-return some running statistics of the server
- Memcache: getVersion-returns the version information of the running Memcache.
- Memcache: increment-adds the value of a saved key.
- Memcache: pconnect-creates a persistent connection object for Memcache.
- Memcache: replace-overwrite an existing key
- Memcache: set-Add a value. if it already exists, overwrite it.
- Memcache: setCompressThreshold-compresses data larger than a certain size
- Memcache: setServerParams-Modify server parameters at runtime
3. Distributed usage:
The Memcache function Library is used in the PECL (PHP Extension Community Library) to set up a temporary storage area for large-capacity memory data. Its role is evident in distributed systems.
<? Php // 1. instantiate a Memcache object $ mem = new Memcache (); // 2. connect to the specified memcache // $ mem-> connect ("127.0.0.1", 11211); // If your 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. Based on the server Load balancer algorithm, the addServer will automatically add $ mem-> add ("name", "zs ", 0,100);?>
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.