: This article describes how to operate memcached1 in 4PHP. if you are interested in the PHP Tutorial, refer to it.
How to use the php program to operate our memcached service curd.
Step.
(1) copy the php_memcache.dll file to ext of php.
? Different versions of php use different versions of php_memcache.dll.
(2) modify the php. ini file and load php_memcache.dll (this file encapsulates a bunch of functions)
; Load the php_memcache.dll file
Extension = php_memcache.dll
(3) restart apache
(4) write a program to complete the curd operation.
You can use telnet to check whether the operation is successful.
When getting objects, note that the class to be retrieved is declared before. Same as session
Mem1.php
Connect ('1970. 0.0.1 ', 127) {echo' connection successful
';} Else {echo' connection failed
';} // Add // 1. add a string/* if ($ mem-> add ('key1', 'Shanghai', MEMCACHE_COMPRESSED, 30) {echo 'added
';} Failed to add else {echo'
';} * // 2. add a number/* if ($ mem-> add ('key1', 100, MEMCACHE_COMPRESSED, 30) {echo' added successfully
';} Failed to add else {echo'
';} * // 3. add an array // when adding an array, you can save the serialization as needed. Serialize // You can also use json_encode ()/* $ arr = array ('AAA', 'BBB ', 'CCC') as needed '); if ($ mem-> add ('key1', $ arr, MEMCACHE_COMPRESSED, 30) {echo 'added successfully
';} Failed to add else {echo'
';} * // 4. add an object/* class Dog {public $ name; public $ age; public function _ construct ($ name, $ age) {$ this-> name = $ name; $ this-> age = $ age ;}}$ do1 = new Dog ('Xiaomi ', 18); if ($ mem-> add ('key1', $ do1, MEMCACHE_COMPRESSED, 30) {echo 'object added successfully
';} Else {echo' failed to add the object
';} * // 5. add null and Boolean values/* if ($ mem-> add ('key1', null, MEMCACHE_COMPRESSED, 30) {echo 'added null successfully
';} Else {echo' failed to add null
';} * // * If ($ mem-> add ('key1', false, MEMCACHE_COMPRESSED, 30) {echo' Boolean value added successfully
';} Failed to add a Boolean value for else {echo'
';} * // 6. resource type $ key1 = mysql_connect ('localhost', 'root', '123'); if (! $ Key1) {die ('database connection failed');} if ($ mem-> add ('key1', false, MEMCACHE_COMPRESSED, 30) {echo 'Resource added successfully
';} Else {echo' failed to add resource value
';} // Query $ val = $ mem-> get ('key1'); // if the resource is saved successfully, the value is 0, therefore, memcache does not support resource storage var_dump ($ val); echo '$ val =', $ val ,'
'; // Modify // delete
Mem2.php
Name = $ name; $ this-> age = $ age ;}} if ($ mem-> connect ('192. 0.0.1 ', 127) {echo' connection successful
';} Else {echo' connection failed
';} // Get the object $ dog = $ mem-> get ('key1'); var_dump ($ dog) in another file );
Index. php
Name = $ name; $ this-> age = $ age; }}$ dog2 = new Dog ('Ha, 18 ); // save this object to the disk-> serialize, the string can be transmitted online // Traditional Method/file_put_contents ('. /dog. log', $ dog2); // it cannot be saved. it is null // serialized file_put_contents ('. /dog. log', serialize ($ dog2); // it can be saved. during retrieval, echo can also be deserialized to save OK.
'; $ Str = file_get_contents ('./dog. log'); var_dump ($ str); var_dump (unserialize ($ str ));
'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
'). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script
The above introduces 4 PHP operations on memcached1, including some content, and hope to be helpful to friends who are interested in PHP tutorials.