Memcache
- 简介
- 安装/配置
- 预定义常量
- 范例
- Memcache — Memcache类
- Memcache::add — 增加一个条目到缓存服务器
- Memcache::addServer — 向连接池中添加一个memcache服务器
- Memcache::close — 关闭memcache连接
- Memcache::connect — 打开一个memcached服务端连接
- Memcache::decrement — 减小元素的值
- Memcache::delete — 从服务端删除一个元素
- Memcache::flush — 清洗(删除)已经存储的所有的元素
- Memcache::get — 从服务端检回一个元素
- Memcache::getExtendedStats — 缓存服务器池中所有服务器统计信息
- Memcache::getServerStatus — 用于获取一个服务器的在线/离线状态
- Memcache::getStats — 获取服务器统计信息
- Memcache::getVersion — 返回服务器版本信息
- Memcache::increment — 增加一个元素的值
- Memcache::pconnect — 打开一个到服务器的持久化连接
- Memcache::replace — 替换已经存在的元素的值
- Memcache::set — Store data at the server
- Memcache::setCompressThreshold — 开启大值自动压缩
- Memcache::setServerParams — 运行时修改服务器参数和状态
- Memcache 函数
- memcache_debug — 转换调试输出的开/关

User Contributed Notes 5 notes
xrobau at gmail dot com ¶
4 years ago
Note that the Memcache 3.0.8 module DOES NOT WORK WITH PHP 7 (or higher)
There is an open bug about this - see https://bugs.php.net/bug.php?id=72887 and over 1 year old at time of writing - however, it appears that development of this module has been abandoned and the MemcacheD module is now the only way to access Memcache through PHP.
If you are writing a NEW tool, and you want to use Memcache, use the MemcacheD library. If you are trying to repair an old tool, you'll either have to stick with PHP 5.6, or, spend the time reworking your code to use MemcacheD.
gabriel dot maybrun at demandmedia dot com ¶
7 years ago
GOTCHA: Recently I was tasked with moving from PECL memcache to PECL memcached and ran into a major problem -- memcache and memcached serialize data differently, meaning that data written with one library can't necessarily be read with the other library.
For example, If you write an object or an array with memcache, it's interpreted as an integer by memcached. If you write it with memcached, it's interpreted as a string by memcache.
tl;dr - You can't safely switch between memcache and memcached without a either a cache flush or isolated cache environments.
<?php
$memcache = new Memcache;
$memcacheD = new Memcached;
$memcache->addServer($host);
$memcacheD->addServers($servers);
$checks = array(
123,
4542.32,
'a string',
true,
array(123, 'string'),
(object)array('key1' => 'value1'),
);
foreach ($checks as $i => $value) {
print "Checking WRITE with Memcache\n";
$key = 'cachetest' . $i;
$memcache->set($key, $value);
usleep(100);
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {
print "Not compatible!";
var_dump(compact('val', 'valD'));
}
print "Checking WRITE with MemcacheD\n";
$key = 'cachetest' . $i;
$memcacheD->set($key, $value);
usleep(100);
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {
print "Not compatible!";
var_dump(compact('val', 'valD'));
}
}
rob4226 at yahoo dot com ¶
7 months ago
Thanks to nono303 on GitHub we can run this PHP extension and memcached on Windows! This is great for a dev environment to match a production unix server. I am using PHP 8.0 with this memcache extension on a Windows 10 box with a memcached server running on localhost and it is working great.
I can't post a link but the dll for this extension is at the repo: nono303/PHP-memcache-dll
And the actual memcached server for windows is at the repo: nono303/memcached
Lajos Veres ¶
4 years ago
3.0.8 is not really compatible with PHP 7, but this fork seems to be working:
https://github.com/websupport-sk/pecl-memcache
For new projects, memcacheD sounds safer, but this fork might help to keep alive some legacy projects.
Debian package manager chose this version: https://github.com/websupport-sk/pecl-memcache/commit/4991c2fff22d00dc81014cc92d2da7077ef4bc86
Based on this:
http://ppa.launchpad.net/ondrej/php/ubuntu/pool/main/p/php-memcache/ https://packages.debian.org/sid/php/php-memcache
Anonymous ¶
7 years ago
I ran Gabriel's test above and had different results. I did find differences in the objects. For instance:
'val' => string(7) "4542.32"
'valD' => double(4542.32)
But, I think this could be resolved. Fairly easily.
The main issue with the test was that one port is used. I ran another instance of memcached on a different port and changed the code slightly. I used this code and had better success:
$servers = array(array('localserv', 11212));
$memcache = new Memcache;
$memcacheD = new Memcached;
$memcache->addServer('localserv', 11211);
$memcacheD->addServers($servers);
$memcacheD->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$checks = array(
123,
4542.32,
'a string',
true,
array(123, 'string'),
(object)array('key1' => 'value1'),
);
foreach ($checks as $i => $value) {
print "Checking WRITE with Memcache\n";
$key = 'cachetest' . $i;
$memcache->set($key, $value);
usleep(100);
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {
print "Not compatible!";
var_dump(compact('val', 'valD'));
} else {
print "WAS COMPAT\n";
}
print "Checking WRITE with MemcacheD\n";
$key = 'cachetest' . $i;
$memcacheD->set($key, $value);
usleep(100);
$val = $memcache->get($key);
$valD = $memcacheD->get($key);
if ($val !== $valD) {
print "Not compatible!";
var_dump(compact('val', 'valD'));
} else {
print "WAS COMPAT\n";
}
}