我会给你指导而不是完整的文档/教程。
在尝试安装 Memcached 之前,您应该使用MySQLi或PDO而不是 mysql_* 函数。查看本文档中的警告消息。
您应该安装 Memcached及其PHP 扩展。
完成后,通过调用函数phpinfo()确保它已启用。如果没有 Memcached,则安装失败。
这是一个明确的代码片段
$mc = new Memcached();
$mc->addServer('localhost', 11211); // add a new server to the pool
// you will want to vary this key according to the query
// if two different queries use the same key your functions will return unexpected data
$cacheKey = 'a-custom-key-that-defines-the-data-within-it';
// how long will it take for the cache to expire in seconds : an hour
$cacheLifetime = 3600;
$data = $mc->get($cacheKey); // we get the data from cache
// check if Memcached was able to retrieve the data
if ($mc->getResultCode() === Memcached::RES_FAILURE) {
// if for some reasons the data is not there we get it and reinject into memcached
$data = mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `active` = 1"), 0);
$mc->set($cacheKey, $data, $cacheLifetime); // save the data in the defined $cacheKey
}
return $data;