Home  >  Article  >  Backend Development  >  Detailed explanation of integration and use of PHP and Memcached

Detailed explanation of integration and use of PHP and Memcached

WBOY
WBOYOriginal
2023-06-25 13:04:40940browse

With the development of web applications, more and more applications will use complete memory caching systems to provide better performance and higher scalability. In many cases, Memcached becomes the preferred solution. This article will introduce the integrated use of PHP and Memcached, as well as some related usage tips.

Memcached is a high-performance in-memory object caching system that can be used to accelerate dynamic web applications. Memcached was originally created by Brad Fitzpatrick with its primary purpose being for his LiveJournal social networking website. Memcached is designed as a distributed caching system, so it is scalable and high-performance.

Using Memcached in PHP

It is easy to integrate PHP applications and Memcached through the PECL extension in PHP. The following is a simple example that uses PHP to implement the basic get and set operations of Memcached:

<?php
//连接Memcached服务器
$memcached = new Memcached();
$memcached->addServer("localhost", 11211);

//存储数据
$memcached->set("key1", "value1");

//获取数据
$value = $memcached->get("key1");
echo $value;
?>

In the above example, we first use the Memcached object to connect to a Memcached server, and then use set() The function stores a key-value pair in the cache, and finally uses the get() function to obtain the key value from the cache. Very simple and intuitive.

Using Memcached in Applications

In real applications, the most common way to use Memcached is to use it for caching and improving performance of data, such as database query results, templates and others Computationally intensive calculation results, etc. Memcached also supports it as a locking system to control concurrent requests to avoid conflicts.

The following is an example of using Memcached to cache MySQL query results:

<?php
//连接Memcached服务器
$memcached = new Memcached();
$memcached->addServer("localhost", 11211);

//查询MySQL数据库
$results = mysqli_query($con, "SELECT * FROM users WHERE status=1");

//循环结果
while($row = mysqli_fetch_array($results)) {
   //将结果存储到缓存中
   $memcached->set($row['user_id'], $row);
}

//关闭MySQL连接
mysqli_close($con);

//获取结果
$user_id = 123;
$result = $memcached->get($user_id);
?>

In the above example, we first connected to the Memcached server and queried the MySQL database to store the results in the cache. Then we get a specific result. If the result has been cached, it will be returned immediately, avoiding the need to query the result repeatedly.

In actual applications, when using Memcached to cache huge data objects, we recommend using Memcached's distributed mechanism. This will be covered in the following section.

Some tips for using Memcached

The following are some tips for using Memcached to improve performance and reliability:

  1. Utilizing Memcached as a locking system

When concurrent requests access shared resources at the same time, race conditions may occur. In this case, Memcached can be used as a distributed locking system to avoid conflicting concurrent requests. For example, if we want to update a counter, we can use Memcached as the locking system.

<?php
//连接Memcached服务器
$memcached = new Memcached();
$memcached->addServer("localhost", 11211);

//以自增方式计数
$key = "counter";
$counter = $memcached->increment($key);

//获取计数器结果
echo "Counter: ".$counter;
?>

In the above example, we used the Memcached object to connect to the Memcached server and used the increment() function to increment the counter value by 1. This avoids competition for the counter from multiple concurrent requests.

  1. Utilizing options and expiration times

Memcached not only stores data, but you can also use additional options to control the behavior of cached data. Using the expiration time option, you can determine how long each key-value pair is stored, thus speeding up cache rotation.

<?php
//连接Memcached服务器
$memcached = new Memcached();
$memcached->addServer("localhost", 11211);

//设置过期时间
$memcached->set("key1", "value1", 3600);

//获取具有过期时间的键值对
$value = $memcached->get("key1");

//除了过期时间之外,可以使用其他选项
$memcached->set("key1", "value1", 3600, 0);
$memcached->set("key1", "value1", 0, 100);
?>

In the above example, we first use the set() function to store the key-value pair and set an expiration time. Then we use the get() function to get this key-value pair. If the data in the cache has expired, a null value is returned.

  1. Taking advantage of Memcached’s scalability

One of the design goals of Memcached is its scalability. This is achieved by embedding the Memcached service into an internal cluster to leverage multiple computers to provide caching services simultaneously.

<?php
//连接Memcached服务器
$memcached = new Memcached();
$memcached->addServers(array(
   array("server1", 11211, 33),
   array("server2", 11211)
));

//保存数据到Memcached服务器
$memcached->set("key1", "value1");

//从Memcached服务器获取数据
$value = $memcached->get("key1");
?>

In the above example, we use the addServers() function to set up multiple Memcached servers to distribute requests to multiple servers. We then use the set() and get() functions to store and retrieve the data into the server. This distribution mechanism is one of Memcached's most powerful scalability mechanisms.

Conclusion

Memcached is a high-performance in-memory object caching system that can be used to accelerate dynamic web applications. PHP extensions make it easy to integrate PHP applications into Memcached, improving application performance and scalability. Using some techniques of Memcached can help us better use the Memcached service and improve the performance and reliability of the application.

The above is the detailed content of Detailed explanation of integration and use of PHP and Memcached. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn