Memcached study note 4 -- memcache extension operation memcached

Source: Internet
Author: User

1,

Install and configure the memcache extension Library

Find the php. ini file and add it:

Extendsion = php_memcache.dll

And copy the corresponding DLL file to the ext directory.

 

2,

Use PHP to perform curd operations on memcahced (view manual)

 

3,

Programming Test

Used to test addition and Acquisition

// Instantiate the memcache object $ M = new memcache (); // connect to the specified memcached, the first parameter: IP address, the second parameter memcached port number $ M-> connect ('2017. 0.0.1 ', 11211); // Add data to MEM/* Add ('key', 'value', 'Compression or not', 'retention Time') compression or not: 0 indicates no compression, constant memcache_compressed storage time: in seconds, the value type can be boolean, null, numeric (integer and floating point type), string type, array type, object, it cannot be a resource type (a resource is stored and put into mem as a Boolean value. The data type is lost, so it cannot be resource-based data). If it is an array, use the ① serialization function (serialize and unserialize) for operations ② or use json_encode ()/json_decode () the function is converted to a JSON object and then stored in MEM */$ user = array (Array ('u1' => 'admin1', 'ag1' => 23, 'email '=>' [email protected] '), array ('u2' => 'admin2', 'ag1' => 25, 'email '=>' [email protected] '),); // serialize the array first /*Why serialization:During network transmission, to ensure that data types are not lost, the data is serialized and then sent, or converted to a JSON object * // $ user = serialize ($ user ); $ user = json_encode ($ user); If (! $ M-> set ('user', $ user, memcache_compressed, 60) {echo "Please note that the key value is unique" ;}else {echo "added successfully ";} echo "<HR/>"; // obtain the key value.
// $ User = unserialize ($ M-> get ('user'); $ user = json_decode ($ M-> get ('user ')); $ user = object2array ($ user); dump ($ user );

Conclusion: among all PHP variable types, except the resource type, other variable types can be put into memcached.

Analyze the following question: when is mem suitable?

What are the scopes of cookie, session, and memcached?

Cookie in their respective browsers (clients) --- associate with a browser

Therefore, we can use cookies to do the following:

① Save the websites that the user has visited

② What products have you visited the website?

Session on the server side (the server is associated with some files (the session is placed in the file) One session Association (the user opens the browser and ends when the user closes) --- private space

Tracking the behavior of a user

All memcached browsers can access the public space.

If a browser corresponds to a user, all operations of different users can access memcached at the same time.

  

If our website requires multiple memcached cache systems

$ M = new memcache ();

$ M-> addserver ('192. 168.1.101 ', 192 );

$ M-> addserver ('192. 168.1.102 ', 192 );

$ M-> addserver ('192. 168.1.103 ', 192 );

$ M-> addserver ('192. 168.1.104 ', 192 );

Here we use the load balancing algorithm to select a memcached server.

$ M-> Add ('user', $ user, memcache_compressed, 60 );

Note:

When we add a memcached group, we do not know which memcached server exists in the data. This process is automatically adjusted by the addserver and balanced by ourselves.

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.