Memcached在web浏览器中不工作,但使用php命令工作
下面是testMemcached.php代码Memcached在web浏览器中不工作,但使用php命令工作,php,memcached,Php,Memcached,下面是testMemcached.php代码 <?php include_once "common.php"; include_once "api.php"; class TestMemcached extends API{ function impl(){ $m = $this->getMem(); $stats = $m->getStats(); var_dump($stats); $m->
<?php
include_once "common.php";
include_once "api.php";
class TestMemcached extends API{
function impl(){
$m = $this->getMem();
$stats = $m->getStats();
var_dump($stats);
$m->add("Key","test");
echo "Value:".$m->get("Key");
}
}
$api = new TestMemcached();
$api->go();
我不知道有什么区别
如何修复memcached在web浏览器中不工作的问题
我的环境:CentOS 7。LNMP
2018/05/23更新:
我使用telnet 127.0.0.1 11211
测试memcached函数
我发现添加
和设置
不起作用
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set test testValue
ERROR
add test testValue
ERROR
get test
END
这是我在phpinfo中的memcached设置。
我使用下面的getResultCode()
代码查找一些错误
这是我的测试结果输出
MemcachedFunction ResultCode ErrorDescription
stats 3 MEMCACHED_CONNECTION_FAILURE
set 3 MEMCACHED_CONNECTION_FAILURE
add 47 MEMCACHED_SERVER_TEMPORARILY_DISABLED
get 47 MEMCACHED_SERVER_TEMPORARILY_DISABLED
fetchAll 16 MEMCACHED_NOTFOUND
我的测试代码在这里。输出在注释中
<?php
include_once 'vendor/autoload.php';
$m = new Memcached();
$m->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
$m->addServer("localhost","11211");
$stats = $m->getStats();
echo "stats ".$m->getResultCode()."<br>"; // stats 3
var_dump($stats); // bool(false)
echo "<br>";
$m->set("Key","test");
echo "set ".$m->getResultCode()."<br>"; // set 3
$m->add("Key","test");
echo "add ".$m->getResultCode()."<br>"; // add 47
echo "Value:".$m->get("Key")."<br>"; // Value:
echo "get ".$m->getResultCode()."<br>"; // get 47
var_dump($m->fetchAll()); // bool(false)
echo "<br>";
echo "fetchAll ".$m->getResultCode()."<br>"; // fetchAll 16
var_dump($m->getAllKeys()); // bool(false)
我曾经遇到过类似的问题。
在我的例子中,使用ip地址而不是“localhost”是有效的
$cache_server_host = '12*.45*.***.***';// Your server's ip address here.
$cache_server_port = 11211;
$cache_obj = NULL;
$is_cache_available = FALSE;
try {
if (class_exists('Memcache')) {
$cache_obj = new Memcache;
$is_cache_available = $cache_obj->connect($cache_server_host, $cache_server_port);
};
}
catch (Exception $e) {}
if (!empty($is_cache_available)) {
// Ok to use the cache;
// i.e.- $cache_obj->set($key, $val, ...);
}
以下是相关的运行时配置选项:。比较CLI选项和Web服务器选项之间的php.ini文件。@Mike我只发现“extension=extension=/usr/local/php memcached/modules/memcached.so”和“memcache.serializer=igbinary”,除了没有设置memcached.sess\u lock\u wait之外,没有其他与Web服务器的php.ini相同的memcachedIs设置,其他设置相同。我已经将“memcached.sess\u lock\u wait=150000”添加到php.ini中,然后重新启动php fpm,但没有任何效果。尝试创建一个新的。在上面的代码中,您正在扩展API
,但尚未包含该代码。我尝试使用公共ip,但仍然无法解决此问题。是否改用127.0.0.1?敬錞潘
$cache_server_host = '12*.45*.***.***';// Your server's ip address here.
$cache_server_port = 11211;
$cache_obj = NULL;
$is_cache_available = FALSE;
try {
if (class_exists('Memcache')) {
$cache_obj = new Memcache;
$is_cache_available = $cache_obj->connect($cache_server_host, $cache_server_port);
};
}
catch (Exception $e) {}
if (!empty($is_cache_available)) {
// Ok to use the cache;
// i.e.- $cache_obj->set($key, $val, ...);
}