How PHP uses the memcached command

Source: Internet
Author: User
Tags server memory

This article pig mastiff and everyone to share how PHP use memcached command, made a summary, hope to help everyone.

Portal: http://www.php.net/manual/zh/book.memcached.php
Windows does not have an memcached extension, only the memcache extension. There is a big difference between the two. So the proposal is still done on Linux.

<?php $mem = new Memcached ();    Add a server resource $mem->addserver (' 127.0.0.1 ', ' 11211 '); Add multiple, distributed storage, the third parameter is the weight value/* $servers = Array (' 127.0.0.1 ', 11211, x), array (' 127.        0.0.2 ', 11211, 67),);    $res = $mem->addservers ($servers);    *//settings: Key value Expiration time (seconds) $mem->set (' name ', ' Huangyuxin ', 5);    Note: The maximum life cycle can be set to 60*60*24*30 30 days//And then add timestamp time () +60*60*24*31 (31 days)//Get value $value = $mem->get (' name '); Add a value if this key is present, false $result = $mem->add (' name ', ' Zhangsan ');//Append: Key value appended to an already existing merit, not present or false//setoption    This sentence must be added, or not appended to the//prepend front append//If the Memcached::opt_compression constant is turned on, this operation fails and raises a warning because appending data after compressing the data may result in the decompression.    $mem->setoption (Memcached::opt_compression, false);    $mem->append (' name ', ' haha '); $value = $mem->get (' name ');    This is the value of the lost element, two parameters, the second parameter determines the number of the minus, the default is 1, increment is added $mem->set (' age ', 12, 30);    $mem->decrement (' age ');    $mem->decrement (' age ', 2);$value = $mem->get (' age ');    Delete element $mem->delete (' age ');    $mem->delete (' age ', 60);         /* Note: The server rejects the Add and replace commands for this key during this period.    Because of this time period, the element is put into a delete queue indicating that it cannot get the value through the Get command, but the add and replace commands are also removed from the server memory (indicating that the element will be immediately deleted and subsequent storage commands for the key will succeed) */    Delete multiple $mem->add (' age ', 12, 60);    $mem->add (' name ', ' Huangyuxin ', 60);    $res = $mem->deletemulti (Array (' age ', ' name ');    void: Flush does not really release the memory of an existing element, but instead gradually deposits new elements to reuse those memory.    $mem->flush (10);//10 seconds to clear the element//Get all Keys $mem->getallkeys (); /* memcached::getdelayed () sends a request to the Memcached server to retrieve the corresponding elements of multiple keys specified by the key. This method does not wait for a response but returns immediately. When you need to collect element values, adjust Memcached::fetch () or Memcached::fetchall ().    If With_cas is set to True, the CAs token for each element is requested at the same time.    */$m->set (' int ', 99);    $m->set (' array ', array (11, 12));    $m->getdelayed (Array (' int ', ' array '), true);    Var_dump ($m->fetchall ());    Get information on multiple values $mem->set (' age ', 12, 60);    $mem->set (' name ', ' Huangyuxin ', 60); $res = $mem->getmulti (Array (' age ', ' name '));    Set multiple keys $items = Array (' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' key3 ' = ' value3 ',);    $mem->setmulti ($items);    $res = $mem->get (' Key1 ');//value//Return system constant Var_dump ($mem->getoption (memcached::opt_compression)); Returns the result of the last operation describing the message $mem->add (' A ', ' bar '); First time should succeed Echo $mem->getresultmessage (), "\ n"; SUCCESS//view on which server this key is $mem->add (' A ', ' bar ');    First time should succeed $res = $mem->getserverbykey (' a '); Array (3) {["Host"]=> string (9) "127.0.0.1" ["Port"]=> Int (11211) ["Weight"]=> int (0)}//Return to Server list Var_du    MP ($mem->getserverlist ());    Returns the server Status Var_dump ($mem->getserverlist ());    Server version Print_r ($mem->getversion ());    Determines whether a persistent link $res = $mem->ispersistent ();    Memcached::replace () and Memcached::set () are similar, but if the server does not have a key, the operation will fail.    $m->set (' hh ', ' aaaa ');    $m->replace (' hh ', ' bbbb '); $res = $m->get (' HH ');    Remove all cache servers from the list of known servers and reset go home.    $mem->resetserverlist ();    Reset the life cycle of a key $m->set (' aaaa ', ' aaaa ', 600);    $m->touch (' AAAA ', 5);    $value = $m->get (' aaaa ');    Close the Open link $m->quit (); Var_dump ($value);

The following suffix is bykey generally used for multiple memcached server, master the above commands, the following basic will be used.

Touch->touchbykeysetmulti->setmultibykeygetmulti->getmultibykeyreplace->replacebykeyappend-> Appendbykeyprepend->prependbykeygetserverbykeygetdelay->getdelayedbykey increment-> Incrementbykeydecrement->decrementbykeyadd->addbykeyget->getbykeydelete->deletemultibykey
$m->addbykey (' Specify server ', ' key ', ' value ')

Wait more than action equals death.

Portal: http://www.php.net/manual/zh/book.memcached.php
Windows does not have an memcached extension, only the memcache extension. There is a big difference between the two. So the proposal is still done on Linux.

<?php $mem = new Memcached ();    Add a server resource $mem->addserver (' 127.0.0.1 ', ' 11211 '); Add multiple, distributed storage, the third parameter is the weight value/* $servers = Array (' 127.0.0.1 ', 11211, x), array (' 127.        0.0.2 ', 11211, 67),);    $res = $mem->addservers ($servers);    *//settings: Key value Expiration time (seconds) $mem->set (' name ', ' Huangyuxin ', 5);    Note: The maximum life cycle can be set to 60*60*24*30 30 days//And then add timestamp time () +60*60*24*31 (31 days)//Get value $value = $mem->get (' name '); Add a value if this key is present, false $result = $mem->add (' name ', ' Zhangsan ');//Append: Key value appended to an already existing merit, not present or false//setoption    This sentence must be added, or not appended to the//prepend front append//If the Memcached::opt_compression constant is turned on, this operation fails and raises a warning because appending data after compressing the data may result in the decompression.    $mem->setoption (Memcached::opt_compression, false);    $mem->append (' name ', ' haha '); $value = $mem->get (' name ');    This is the value of the lost element, two parameters, the second parameter determines the number of the minus, the default is 1, increment is added $mem->set (' age ', 12, 30);    $mem->decrement (' age ');    $mem->decrement (' age ', 2);$value = $mem->get (' age ');    Delete element $mem->delete (' age ');    $mem->delete (' age ', 60);         /* Note: The server rejects the Add and replace commands for this key during this period.    Because of this time period, the element is put into a delete queue indicating that it cannot get the value through the Get command, but the add and replace commands are also removed from the server memory (indicating that the element will be immediately deleted and subsequent storage commands for the key will succeed) */    Delete multiple $mem->add (' age ', 12, 60);    $mem->add (' name ', ' Huangyuxin ', 60);    $res = $mem->deletemulti (Array (' age ', ' name ');    void: Flush does not really release the memory of an existing element, but instead gradually deposits new elements to reuse those memory.    $mem->flush (10);//10 seconds to clear the element//Get all Keys $mem->getallkeys (); /* memcached::getdelayed () sends a request to the Memcached server to retrieve the corresponding elements of multiple keys specified by the key. This method does not wait for a response but returns immediately. When you need to collect element values, adjust Memcached::fetch () or Memcached::fetchall ().    If With_cas is set to True, the CAs token for each element is requested at the same time.    */$m->set (' int ', 99);    $m->set (' array ', array (11, 12));    $m->getdelayed (Array (' int ', ' array '), true);    Var_dump ($m->fetchall ());    Get information on multiple values $mem->set (' age ', 12, 60);    $mem->set (' name ', ' Huangyuxin ', 60); $res = $mem->getmulti (Array (' age ', ' name '));    Set multiple keys $items = Array (' key1 ' = ' value1 ', ' key2 ' = ' value2 ', ' key3 ' = ' value3 ',);    $mem->setmulti ($items);    $res = $mem->get (' Key1 ');//value//Return system constant Var_dump ($mem->getoption (memcached::opt_compression)); Returns the result of the last operation describing the message $mem->add (' A ', ' bar '); First time should succeed Echo $mem->getresultmessage (), "\ n"; SUCCESS//view on which server this key is $mem->add (' A ', ' bar ');    First time should succeed $res = $mem->getserverbykey (' a '); Array (3) {["Host"]=> string (9) "127.0.0.1" ["Port"]=> Int (11211) ["Weight"]=> int (0)}//Return to Server list Var_du    MP ($mem->getserverlist ());    Returns the server Status Var_dump ($mem->getserverlist ());    Server version Print_r ($mem->getversion ());    Determines whether a persistent link $res = $mem->ispersistent ();    Memcached::replace () and Memcached::set () are similar, but if the server does not have a key, the operation will fail.    $m->set (' hh ', ' aaaa ');    $m->replace (' hh ', ' bbbb '); $res = $m->get (' HH ');    Remove all cache servers from the list of known servers and reset go home.    $mem->resetserverlist ();    Reset the life cycle of a key $m->set (' aaaa ', ' aaaa ', 600);    $m->touch (' AAAA ', 5);    $value = $m->get (' aaaa ');    Close the Open link $m->quit (); Var_dump ($value);

The following suffix is bykey generally used for multiple memcached server, master the above commands, the following basic will be used.

Touch->touchbykeysetmulti->setmultibykeygetmulti->getmultibykeyreplace->replacebykeyappend-> Appendbykeyprepend->prependbykeygetserverbykeygetdelay->getdelayedbykey increment-> Incrementbykeydecrement->decrementbykeyadd->addbykeyget->getbykeydelete->deletemultibykey
$m->addbykey (' Specify server ', ' key ', ' value ')

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.