Simple application of Memcache

Source: Internet
Author: User

Nowadays the internet is rising, the major websites are faced with a big data flow problem, how to improve the website access speed, reduce the operation of the database; as a PHP developer, we can generally think of a method of page static processing, anti-theft chain, CDN content distribution accelerated access, MySQL database optimization index, Set up Apache server cluster, there are now popular various distributed cache technology: such as Memcached/redis;
1. What is memcached?
A.memcached is a high-performance distributed memory object caching system for dynamic Web applications to mitigate database load. It improves the speed of dynamic, database-driven Web sites by caching data and objects in memory to reduce the number of times a database is read. Memcached is based on a hashmap that stores key/value pairs. Its daemon (daemon) is written in C, but the client can write in any language and communicate with the daemon through the memcached protocol.
B.memcached key is usually a string, the value cannot be duplicated; value can be put into strings, arrays, values, objects, booleans, binary data, and picture video
C.memcached default service port is 11211
2.PHP using memcached steps
<1> Preparation: Reference configuration
<2> began to practice, memcached mainly have crud operations (that is, create, read, update, delete value operations, specifically can consult the manual), below a simple set of values, and then read the value of the operation
A. Setting the value page

[PHP]Plain Text view copy code ?
01020304050607080910111213141516 <?phpheader("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache();  //连接Memcache服务器if(!$mem--->connect("127.0.0.1")) {    echo "连接Memcache服务器失败!";} //设置,‘myword‘参数代表键key,‘hello world‘代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set(‘myword‘,‘hello world‘,MEMCACHE_COMPRESSED,50)){    echo "设置值成功!";} ?>



Note: If the value is stored in memory for more than 30 days, use a timestamp to set 100 days: such as time () +3600*24*100; set 0 means never expire

B. Read Value page

[PHP]Plain Text view copy code ?
0102030405060708091011121314151617 <?phpheader("Content-type:text/html;charset=utf-8");$mem = new Memcache();  if(!$mem--->connect("127.0.0.1")) {    echo "连接Memcache服务器失败!";} //读取键myword值$value = $mem->get(‘myword‘);if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}



C. Examples of deletions and updates:

[PHP]Plain Text view copy code ?
0102030405060708091011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 <?phpheader("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache();  //连接Memcache服务器if(!$mem--->connect("127.0.0.1")) {    echo "连接Memcache服务器失败!";} //设置,‘myword‘参数代表键key,‘hello world‘代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set(‘myword‘,‘hello world‘,MEMCACHE_COMPRESSED,50)){    echo "设置值成功!";} //读取键myword值$value = $mem->get(‘myword‘);if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;} //更新键值$mem->replace(‘myword‘,‘hello everybody!‘); $value = $mem->get(‘myword‘);if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;}  //删除键myword值$mem->delete(‘myword‘); $value = $mem->get(‘myword‘);if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;} //关闭$mem->close();  ?>



Note: There are many methods under the Mem object that can be read through the manual.

<3> multiple memcached server settings, in fact, a little bit more than a memcached server change, is to add multiple memcached server through the method Addserver added to the connection pool, so after Setup, CRUD operations, Internally, the appropriate algorithm is used to balance the corresponding server and perform the corresponding operation.

[PHP]Plain Text view copy code ?
0102030405060708091011121314151617181920212223242526272829 <?phpheader("Content-type:text/html;charset=utf-8");//创建Memcache对象$mem = new Memcache(); //添加多台memcached服务器$mem->addserver(‘192.168.0.1‘,11211); $mem->addserver(‘192.168.0.2‘,11211);$mem->addserver(‘192.168.0.3‘,11211);$mem->addserver(‘192.168.0.4‘,11211);  //设置,‘myword‘参数代表键key,‘hello world‘代表存放的值,MEMCACHE_COMPRESSED代表压缩内容,50代表存放时间,单位秒if ($mem->set(‘myword‘,‘hello world‘,MEMCACHE_COMPRESSED,50)){    echo "设置值成功!";} //读取键myword值$value = $mem->get(‘myword‘);if(!$value){    echo "读取失败!";}else{    echo "读取的值=".$value;} ?>




<4>memcache access is no user state, security needs to be considered, generally by placing in the intranet, and through the firewall to restrict the external network access to memcache port to achieve security
<5> by modifying php.ini, you can put the value of the session into the Memcache server
Session.save_handler = files changed to Session.save_handler = memcached
Session.save_path = "N; Mode;/path "changed to Session.save_path =" tcp://127.0.0.1:11211 "

Simple application of Memcache

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.