Home >Backend Development >PHP Tutorial >Principles and tutorials on connecting and manipulating Memcached with PHP_PHP Tutorial
Memcahced is an open source distributed memory object caching system. Slightly larger projects often use Memcached to reduce the burden on the database and thereby speed up the response speed of web applications. There are a lot of installation methods and usage introductions about Memcached on the Internet. What this article wants to say is that memcached is actually very simple and not as mysterious as imagined. We can simply understand it as a cache server application, just like you installed a It is the same as Mysql. After it is installed, you can use it by connecting it with your account password and IP address.
The homepage briefly introduces the principle of memcached
The first time a user sends a request, the PHP program will write the accessed data to the Memcached system while accessing the db database.
As shown in the figure, the user sends a req request, and the application sends a data request to the database. While the database returns the data to the application, it caches the data to the Memcached server.
The second time a user request arrives, the cache of the Memcached server will be read directly instead of the content in the database, thereby reducing the burden on the server.
This picture shows that in the second request, the application reads data directly from Memcached (Mc for short).
Let’s share the basic usage of memcached through an example (similar to a development example of a friendly link). I believe that through this example, you can clearly understand this stuff.
The following case assumes that you have installed the memcached service. If not, please refer to this site:
What is memcached? How to use memcache?
Install memcache under windows system
(1) Create a new database
The database table used in the example contains an auto-incremented id, a title and a link field:
CREATE TABLE demos( id INT PRIMARY KEY AUTO_INCREMENT, title VARCHAR(300), link VARCHAR(300), );
(2) Program part (the comments of the program will make it easy for you to understand the use of memcached)
<?php include('db.php'); $memcache = new Memcache; $memcache->connect('localhost', 11211) or die ("Could not connect"); //缓存服务器中,都是键值对,这里我们设定唯一的键 $key = md5('www.crazyant.net'); $cache_result = array(); //根据键,从缓存服务器中获取它的值 $cache_result = $memcache->get($key); //如果存在该键对应的值,说明缓存中存在该内容 if($cache_result){ //那我们直接取出缓存的内容就可以了 $demos_result=$cache_result; } else { //如果缓存中没有该键对应的值数据,说明请求是第一次到达 //首先,我们需要从数据库中取出该值 $v=mysql_query("select * from demos order by id desc"); while($row=mysql_fetch_array($v)){ //取出的内容就是我们需要的 $demos_result[]=$row; } //最后,将这次从数据库取出的内容,放到Memcached缓存服务器,这里就是缓存的精髓 $memcache->set($key, $demos_result, MEMCACHE_COMPRESSED, 1200); } //前面的所有操作,最终返回了我们需要的数据 foreach($demos_result as $row){ echo '<a href='.$row['link'].'>'.$row['title'].'</a>'; } ?>
The following is the code file db.php used to connect to the database
<?php $mysql_hostname="localhost"; $mysql_user="username"; $mysql_password="password"; $mysql_database="database"; $bd=mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong"); mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong"); ?>