In today's era of the rise of the Internet, major websites are facing a big data flow problem. How to improve website access speed and reduce database operations; as PHP developers, the methods we can generally think of include static page processing, Anti-leeching, CDN content distribution to accelerate access, mysql database optimization and indexing, setting up apache server cluster (), and various distributed caching technologies that are now popular: such as memcached/redis;
1. What is Memcached?
a.Memcached is a high-performance distributed memory object caching system for dynamic web applications to reduce database load. It improves the speed of dynamic, database-driven websites by caching data and objects in memory to reduce the number of database reads. Memcached is based on a hashmap that stores key/value pairs. Its daemon is written in C, but the client can be written in any language and communicates with the daemon through the memcached protocol.
b. The key of Memcached is generally a string, and the value cannot be repeated; the value can be put into strings, arrays, values, objects, Boolean, binary data and pictures and videos
c.Memcached default service port is 11211
2. Steps to use Memcached with PHP
<1>Preparation: Download the Memcached service installation package: memcached-1.2.6-win32-bin.7z and the dll library to access the Memcached service: php_memcache.dll
www.memcached.org (It seems that the official website cannot be accessed, you can download it from other places)
<2> Unzip the package memcached-1.2.6-win32-bin.7z (you can unzip and copy it to the same directory as the web server), then operate cmd, enter the directory you just unzipped and install it with the command: memcached. exe -d install
<3> After installation (to determine whether the installation is complete, you can check whether there is memcached service in the service list), then start cmd with the command: memcached.exe -d start
The specific operations are as follows:
<4>After starting the memcached service, put the downloaded php_memcache.dll into the ext directory under the php5 directory of the web server
<5> Modify in php.ini, load the extension library php_memcache.dll, and then restart the apache server
<6> Start practicing. Memcached mainly has crud operations (that is, creating, reading, updating, and deleting value operations. You can refer to the manual for details). Let’s make a simple operation of setting the value and then reading the value
a. Setting value page
<!--?php header("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; setting 0 means it will never expire.
b.Read value page
<!--?php header("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; }
<!--?php header("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(); ?>
<7> The setting of multiple memcached servers is actually a little bit different than one memcached server. It is to add multiple memcached servers to the connection pool through the addserver method. After setting up, when crud operates, the internal The corresponding server will be connected to the corresponding server in a balanced manner through the corresponding algorithm and the corresponding operation will be performed.
<!--?php header("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; } ?>
<8> Access to memcache is user-free, and security needs to be considered. Security is generally achieved by placing it on the intranet and restricting external network access to the memcache port through a firewall
<9>By modifying php.ini, the session value can be put 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"