When we think of storage in a web application our first thought is usually a traditional database like MySQL. This is great for long term storage and data analysis, but theres a better option for many short-term needs: memcached. Its a gre
when we think of storage in a web application our first thought is usually a traditional database like mysql. this is great for long term storage and data analysis, but there’s a better option for many short-term needs: memcached. it’s a great choice for saving bits of information between page requests and increasing performance. in this introduction we’ll show you how to start using memcached with php.
Memcached is simply a server with an interface to let you store things in memory. This can run on the same machine as your web server, but scalability comes from distributing instances across multiple servers. All you need is the memcached daemon running and PHP provides a simple interface with a PECL library. On a Debian-based Linux system this is as easy as:
$ sudo apt-get install memcached $ sudo service memcached start $ sudo pecl install memcached $ sudo service httpd restart
Now we should mention there are technically two PHP libraries available to work with memcached. The older library is called “memcache” and is lacking in some features. The newer “memcached” library uses libmemcached and is generally preferred.
The first step in PHP is to connect to the server. Connections can be persisted across requests, which is nice for performance. Then add to the server list as needed. In this case we’ll use a locally running instance on the default port:
function get_memcached() {
// Set a persistent connection ID
$mc = new Memcached('webapp');
// Set a short timeout (in milliseconds) so if the server goes down
// it doesn't take down our site with it
$mc->setOption(Memcached::OPT_CONNECT_TIMEOUT, 1000);
if ( !$mc->getServerList() ) {
if ( !$mc->addServer('localhost', 11211) ) {
error_log('Could not add memcached server.');
return false;
}
}
return $mc;
}
Now you can read and write PHP variables to memcached with simple functions based on keys you define. They’ll be serialized and deserialized automatically. You can’t write resources like database connections or result sets, but you can turn those result sets into arrays and store those.
$mc->set('list', array(1, 2, 3));
$list = $mc->get('list');
Let’s say we want to store a list of recently visited URLs for each logged in user. We could use sessions but that wouldn’t work across devices and it’ll disappear as soon as the session is cleared. We could use a database but that’s slow for this kind of data that’s most likely not critical to our system. Using memcached this is easy:
$user_id = 123; // The current user's ID
$recents = array();
if ($mc = get_memcached()) {
// Get any stored in memcached
$recents = $mc->get("recents-$user_id");
if (!$recents) {
$recents = array();
}
$recents[] = $_SERVER['REQUEST_URI'];
$recents = array_unique($recents);
$recents->set("recents-$user_id", $recents);
}
print_r($recents);
Now let’s really boost your web application by caching database results. Database queries are usually the biggest bottleneck in server processing, so avoiding duplicate queries by caching results in memory can provide a huge performance gain. The simplest method is to query and store just with primary keys. Typically it’s best that the cached value be deleted when the database record is updated so a user never sees any out-of-date values.
function store_product($id, $name, $price) {
// Create or update the product in the database
$db = get_db();
$qry = $db->prepare('REPLACE INTO product (id, name, price) VALUES (:id, :name, :price)');
$qry->bindParam(':id', $id);
$qry->bindParam(':name', $name);
$qry->bindParam(':price', $price);
$qry->execute();
// Remove outdated values from cache if it's there
if ($mc = get_memcached()) {
$mc->delete("product-$id");
}
}
function get_product($id) {
$product = null;
// First check the cache
if ($mc = get_memcached()) {
$product = $mc->get("product-$id");
}
if (!$product) {
// Not in the cache; check the db
$qry = $db->prepare('SELECT * FROM product WHERE id = $id');
$qry->bindParam(':id', $id);
if ($qry->execute()) {
$product = $qry->fetch();
// Cache for future use
if ($mc) {
$mc->set("product-$id", $product);
}
}
}
return $product;
}
As with any technology choice there are limitations and things to watch out for:
There’s more you can do with memcached:
increment and decrement methods are useful for keeping quick counters between requests.Give memcached a try. In the right scenarios it’s a very simple and effective solution to maximize your web app performance.
Read the full article at: Improving Web App Performance With Memcached


每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
C++高性能并发应用_C++如何开发性能关键应用
Java AI集成Deep Java Library_Java怎么集成AI模型部署
Golang后端API开发_Golang如何高效开发后端和API
Python异步并发改进_Python异步编程有哪些新改进
C++系统编程内存管理_C++系统编程怎么与Rust竞争内存安全
Java GraalVM原生镜像构建_Java怎么用GraalVM构建高效原生镜像
Python FastAPI异步API开发_Python怎么用FastAPI构建异步API
C++现代C++20/23/26特性_现代C++有哪些新标准特性如modules和coroutines
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号