如何使用php程序操作我们的memcached服务 curd.
Steps to prepare for the job.
(1) Copy the Php_memcache.dll file to PHP ext.
? Different versions of PHP use the same version of Php_memcache.dll
(2) Modify the php.ini file, load the Php_memcache.dll (the file is encapsulated a bunch of functions)
; Load Php_memcache.dll File
Extension=php_memcache.dll
(3) Restart Apache
(4) We write the program to complete the curd operation.
可以用 telnet 查看是否成功
When fetching an object, be aware that the class you want to fetch is declared earlier. Just like the session.
mem1.php
$mem= new Memcache ();if($mem->connect (' 127.0.0.1 ',11211)){Echo' Connection succeeded
';}Else{Echo' Connection failed
';} Increase//1. Add a string/*if($mem->add (' Key1 ',' Shanghai ', memcache_compressed, -)){Echo' Add success
';}Else{Echo' Add failed
';} *///2. Add a number/*if($mem->add (' Key1 ', -, memcache_compressed, -)){Echo' Add success
';}Else{Echo' Add failed
';} *///3Add an array//when adding an array, as needed, you want to deposit after serialization. serialize//can also Json_encode ()/* if required$arr= Array (' AAA ',' BBB ',' CCC ');if($mem->add (' Key1 ',$arr, memcache_compressed, -)){Echo' Add success
';}Else{Echo' Add failed
';} *///4. Add Object/*class dog{Public$name; Public$age; Public Function __construct ($name,$age) {$this->name =$name;$this->age =$age; }}$do 1= New Dog (' Millet ', -);if($mem->add (' Key1 ',$do 1, memcache_compressed, -)){Echo' Add object succeeded
';}Else{Echo' Add object failed
';} *///5. Add null and Boolean values/*if($mem->add (' Key1 ', null,memcache_compressed, -)){Echo' Add null succeeded
';}Else{Echo' Add null failed
';} *//*if($mem->add (' Key1 ',false, memcache_compressed, -)){Echo' added Boolean value succeeded
';}Else{Echo' Adding a Boolean value failed
';} *///6. Resource type$key 1= Mysql_connect (' localhost ',' Root ',' 123456 ');if(!$key 1) {Die (' failed to connect to database ');}if($mem->add (' Key1 ',false, memcache_compressed, -)){Echo' Add Resource succeeded
';}Else{Echo' Add Resource value failed
';} Inquire$val=$mem->get (' Key1 ')//If it is a resource, even if it is saved successfully, it will be taken out0, so memcache does not support storing resources var_dump ($val);Echo' $val = ',$val,'
';//Modify/delete
mem2.php
//在这个文件去 memcache 对象$mem = new Memcache();classDog{public$name; public$age; publicfunction__construct($name,$age) {$this->name = $name; $this->age = $age; }}if ($mem->connect('127.0.0.1',11211)){ echo'连接成功
';}else{ echo'连接失败
';}//在另外文件取对象$dog = $mem->get('key1');var_dump($dog);
index.php
!--? php class Dog { public $name ; Public $age ; Public function __construct ( $name , $age ) { $this ->name = $name ; $this ->age = $age ; }} $dog 2 = new Dog ( ' small ha ' , + ); //Save this object to disk--serialize, the string can be transmitted online //Traditional methods //file_put_contents ('./dog.log ', $dog 2);//cannot be saved, is empty //Serialization file_put_contents ( './dog.log ' , serialize ( $dog 2 )); //Can be saved, and can be deserialized when taken out Echo ' 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); }); });
The above describes the 4 PHP operation Memcached1, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.