Speaking of the relationship between PHP's Memcache & Memcached extensions, do you understand it?

Speaking of the relationship between PHP's Memcache & Memcached extensions, do you understand it?

[51CTO.com original article] Memcached is a free, open source, high-performance, distributed memory cache system. Many WEB programmers should be very familiar with it. Many WEB programmers often use it to cache data in the database to improve the performance of websites or applications. As a popular language for website development, PHP certainly supports Memcached. However, when we open the official PHP manual, we find an interesting situation, that is, PHP has two extensions that provide similar functions, namely Memcache and Memcached extensions. What is the relationship between these two extensions? And are the two extensions the same? With these questions, let's analyze them one by one.

Some basic concepts

In fact, Memcached is a C/S application, so there are two basic concepts:

  • Memcached server. It is the application end that actually provides data caching. This end is an independent process and opens corresponding ports for Memcached clients to perform operations such as adding, deleting, modifying, and querying data.
  • Memcached client. As long as it can communicate with the Memcached server and complete the corresponding data operation functions, we can call it a Memcached client. For example, the two PHP extensions mentioned in this article can both be called clients.

Related knowledge and operating principles of the two extensions

In the pecl official extension library, we can see that the birthday of the Memcache extension is February 26, 2004, and the birthday of the Memcached extension is January 29, 2009. Haha, looking at these two birthdays, what do you think of?

From the birthdays above, we can see that Memcache was born first, and Memcached was born later. Let's take a look at the definitions of the two:

  • Memcache extension. This extension provides both procedural and object-oriented approaches.
  • Memcached. This extension uses the libmemcached library to communicate with the Memcached server.

After reading the definitions of the two extensions, it is becoming more and more interesting. We seem to be getting closer to the truth, but there is only one step left, which is what libmemcached is. Let's continue to think about this question and continue to open the official documentation of Memcached. We can easily find that libmemcached is actually the official client provided by Memcached. In other words, the Memcached extension of PHP is actually a secondary encapsulation extension. This extension stands on the shoulders of the official client extension, so it certainly provides more functions. On the other hand, the Memcache extension is just a set of Memcached extension libraries implemented by PHP itself. Having said that, I believe many PHP programmers should understand why we no longer advocate the use of the Memcache extension. The following figure shows the working principles of the two extensions.

There is also a special extension in PHP

The above analysis shows two extensions. In fact, the situation of these two extensions is not unique in PHP. There is another knowledge point in PHP that is the same as this situation, which is Mysqlnd and libmysqlclient that we are going to talk about next.

Through the previous analysis, it is not difficult to think that libmysqlclient is actually the MySQL programming client officially provided by MySQL database, and Mysqlnd is actually the MySQL programming client implemented by PHP itself. It is not secondary developed based on libmysqlclient, but is written in C language.

In fact, Memcached not only supports PHP, but also supports C, C++, Java, MySQL, Python, Nodejs, etc. Through the above Memcache extension, we know that as long as we follow the protocol it provides, it is easy to implement the Memcached client of your own programming language. Having said so much, I don’t know if you have completely understood the relationship between the two. If you still don’t understand, you can interact in the comments. You can also join the group discussion ( QQ group number 312724475 )

[51CTO original article, please indicate the original author and source as 51CTO.com when reprinting on partner sites]

<<:  The seventh episode of the Aite Tribe Clinic: What function was used to write the King of Pesticide Welfare Bureau?

>>:  Deep learning: preconceptions, limitations, and the future

Recommend

The third AIDS patient was "cured"! Can humans finally conquer AIDS?

On February 15, American researchers reported the...

How much is a carnival in RMB? Detailed list of 2021 Douyin gift prices!

People who often watch Douyin live broadcasts are...

How to set product prices so that users feel that the products are cheap?

99% of marketing has probably encountered these p...

Amazon's entry into Tmall leaves three major suspense

Amazon quietly entered Tmall, seemingly not wanti...

Is O blood type really a universal blood type?

Produced by: Science Popularization China Author:...

Event promotion and operation: 8 hot creative forms of H5!

It cannot be denied that, on the basis of guarant...

A fever can boil 700 million pots of water! Is there any hope?

Imagine that when you are in the vast universe an...