Curd with the memcache Dynamic runtime Operation memcached Service

Source: Internet
Author: User

Refer to the PHP reference manual for specific studies:

Several ways to manipulate memcached:


BOOL Memcache::add (String $key, mixed $var [, int $flag [, int $expire]])

If the quote expire is set to 0 , it never expires . just memcache without restarting, it will always be in Mem in )

Exprie directly to the number of seconds, the maximum 30*3600*24

If you want to keep time longer than () + days *3600*24

mem1.php

<?php//Create a Mem object instance $mem=new Memcache; if (! $mem->connect ("127.0.0.1", 11211)) {die (' Connection failed! ');} Add//1. Add a string of/*if ($mem->set (' Key1 ', "Beijing", memcache_compressed,60)) {echo ' Add ok ';} *///2. Add a numeric/*if ($mem->set (' Key1 ', 100,memcache_compressed,60)) {echo ' Add ok ';} *///3. Add an array//In the Add array is, as needed. Wish the serial number put in,//serialize<=>unserialize, if necessary, can also Json_encode <=> json_decode$arr=array ("BJ", ' TJ '); Mem->set (' Key1 ', $arr, Memcache_compressed,time () +31*3600*24)) {echo ' Add array ok99111 ';} 4. Add Object/*class dog{public $name;p ublic $age;p ublic function __construct ($name, $age) {$this->name= $name; $this- >age= $age;}} $dog 1=new Dog (' Puppy '), if ($mem->set (' Key1 ', $dog 1,memcache_compressed,60)) {echo ' Add object OK ';} *///5. Add a null Boolean value, NULL is NULL, Boolean converts to String 0/*if ($mem->set (' Key1 ', false,memcache_compressed,60)) {echo ' adds Boolean ok ';} *///6. The resource type is put in./* $con =mysql_connect ("127.0.0.1", "root", "root"), if (! $con) {die (' connection database failed ');} Var_dump ($con); echo "<br/>"; Adding a resource type variable to 0 currently does not support if ($mem->set (' Key1 ', $con, Memcache_compressed,60) {echo ' Add resource ok ';} *///Query $val= $mem->get (' Key1 '); Var_dump ($val);//modify//can use Replaceif ($mem->replace ("Key11", ' Hello ', memcache_ compressed,60) {echo ' replace OK ';} Else{echo ' Replace no OK ';} Delete echo "<br/>", if ($mem->delete (' key14 ')) {echo ' key14 delete ';} Else{echo ' KEY14 does not exist ';}

When you remove an object from another file, there is a note that the corresponding php5.2 version will prompt for errors,
This version of php5.3 will prompt for incomplete information, and the workaround is to declare the class definition

mem2.php

<?php//this file to operate the Memcached service//Create a Mem object instance $mem=new Memcache;     if (! $mem->connect ("127.0.0.1", 11211)) {die (' Connection failed! ');} In the other file to remove the object, there is a note that the corresponding php5.2 version will prompt the error,//php5.3 This version will prompt incomplete information, the workaround is to declare the class definition can class Dog{public $name;p ublic $ Age;public function __construct ($name, $age) {$this->name= $name; $this->age= $age;}} $dog = $mem->get (' Key1 '); Var_dump ($dog);

test.php Description serilize and json_encode usage :

Icon:


When to use serilize (save objects directly to a file) when to use json_encode [Ajax mates ]

<?php

/*class dog{public $name;p ublic $age;p ublic function __construct ($name, $age) {$this->name= $name; $this->age=$ Age;}} $dog 2=new Dog (' Little Yellow ', 40);//I want to save this object to disk. ->serilizefile_put_contents ("D:/my.log", Serialize ($dog 2)); Echo ' Save ok! '; $dog =unserialize (file_get_contents ("D:/my.log")), echo "<br/>", Echo $dog->name; $arr =array (' city1 ' = " BJ ", ' City ' =" TJ "); File_put_contents (" D:/my2.log ", Json_encode ($arr)); */$host =" 127.0.0.1:1134 "; List ($ip, $port) = Explode (":", $host); echo $ip. " = = = ". $port;



Curd with the memcache Dynamic runtime Operation memcached Service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.