PHP Memcached for simple database caching

Source: Internet
Author: User
Tags md5 memcached php memcached

Memcache Common methods


Memcache::add-adds a value that returns false if it already exists
memcache::addserver-Add a server address that is available for use
memcache::close-Close a Memcache object
memcache::connect-Creating a Memcache Object
Memcache::d ebug-control Debugging function
Memcache::d ecrement-to subtract a value from a saved key
Memcache::d elete-Delete a key value
memcache::flush-to clear all cached data
memcache::get-get a key value
memcache::getextendedstats-get run System statistics for all processes in the process pool
memcache::getserverstatus-getting parameters to run the server
memcache::getstats-returns some running statistics for the server
memcache::getversion-returns the version information of the running Memcache
memcache::increment-an addition to the value in a saved key
Memcache: Create a Memcache Persistent connection object:p connect-
Memcache::replace-r Overwrite an existing key
Memcache::set-adds a value that, if already present, overwrite
memcache::setcompressthreshold-compression of data larger than a certain size
memcache::setserverparams-modifying the server's parameters at run time

The following is a simple test code that accesses object data with identifier ' MyKey ' in the code

<?PHP//contains the Memcached class filerequire_once(' memcached-client.php '); //option Settings$options=Array( ' Servers ' =Array(' 192.168.1.1:11211 '),//memcached service address, port, multiple array elements can be used to represent multiple memcached services' Debug ' =true,//whether to turn on debug' Compress_threshold ' = 10240,//compression when more than a few bytes of data' Persistant ' =false //whether to use persistent connections); //Creating an Memcached object instance$MC=NewMemcached$options); //sets the unique identifier used by this script$key= ' MyKey '; //writing objects to the memcached$MC->add ($key, ' some random strings '); $val=$MC->get ($key); Echo"N".Str_pad(' $MC->add () ', 60, ' _ '). " N; Var_dump($val); //Replace a written object data value$MC->replace ($key,Array(' some ' = ' haha ', ' array ' = ' xxx ')); $val=$MC->get ($key); Echo"N".Str_pad(' $MC->replace () ', 60, ' _ '). " N; Var_dump($val); //delete an object in memcached$MC->delete ($key); $val=$MC->get ($key); Echo"N".Str_pad(' $MC->delete () ', 60, ' _ '). " N; Var_dump($val); ?>

In practical application, the result set of database query is usually saved to memcached, and the next access is obtained directly from memcached, and the database query operation is not done, which can alleviate the burden of the database to a great extent. Typically, the value after the SQL statement MD5 () is used as the unique identifier key. Below is an example of using memcached to cache a database query result set

<?PHP$sql= ' SELECT * from users '; $key=MD5($sql);//memcached Object Identifiersif( ! ($datas=$MC->get ($key)) ) { //If the cached data is not obtained in memcached, the recordset is obtained using a database query. Echo"N".Str_pad(' Read datas from MySQL. ', 60, ' _ '). " N; $conn=mysql_connect(' localhost ', ' test ', ' test '); mysql_select_db(' Test '); $result=mysql_query($sql);  while($row=Mysql_fetch_object($result)) $datas[] =$row; //saves the result set data obtained in the database to memcached for use on the next visit. $MC->add ($key,$datas); } Else { Echo"N".Str_pad(' Read datas from memcached. ', 60, ' _ '). " N; } Var_dump($datas); ?>

As you can see, after using memcached, you can reduce database connection, query operations, database load down, scripts run faster.

PHP Memcached for simple database caching

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.