- Set __construct () to private so that the external new is not allowed
- Instantiates an object inside the class. External can be obtained on the line
classMemextendsMemcache{privatestatic$mem_instancenull; privatefunction__construct(){parent::__construct(); } privatefunction__clone(){} publicstaticfunctionget_mem_ins(){if(nullself::$mem_instance){ self::$mem_instancenew Memcache; } returnself::$mem_instance; }}
- Memcache operation
Php//Connection$mem=NewMemcache;$mem -Connect"Db.nowamagic.net",12000);//Save Data$mem -Set(' Key1 ',' This is first value ',0, -);$val=$mem -Get' Key1 '); Echo"Get key1 value:".$val."
";//Replace data$mem -Replace' Key1 ',' This is replace value ',0, -);$val=$mem -Get' Key1 '); Echo"Get key1 value:".$val."
";//Save array$arr=Array(' AAA ',' BBB ',' CCC ',' DDD ');$mem -Set(' Key2 ',$arr,0, -);$val 2=$mem -Get' Key2 '); Echo"Get key2 value:"; Print_r ($val 2); Echo"
";//Delete data$mem -Delete' Key1 ');$val=$mem -Get' Key1 '); Echo"Get key1 value:".$val."
";//Clear All data$mem -Flush ();$val 2=$mem -Get' Key2 '); Echo"Get key2 value:"; Print_r ($val 2); Echo"
";//Close connection$mem -Close ();?>
- Output
valueisvaluevalueisvaluevalue: Array ( [0] => aaa [1] => bbb [2] => ccc [3valuevalue:
Copyright Notice: Knowledge is taken to the people, to the people! Welcome reprint, Reprint please attach this article link, not regularly updated articles!
The above introduces the PHP memcache single case, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.