Sunday, 2 September 2018

Memcached not working in web browser,but use php command working

I have testMemcached.php code below.

<?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();

I run testMemcached.php in the web browser. I get bool(false) Value:.

I run php -f testMemcached.php command then get the output below.

array(1) {
  ["localhost:11211"]=>
  array(24) {
    ["pid"]=>
    int(10218)
     ....(skip)
    ["version"]=>
    string(6) "1.4.15"
  }
}
Value:test

I don't know what the difference is and how to fix memcached not working in the web browser.

My environment:CentOS 7. LNMP.

2018/05/23 Update : I use telnet 127.0.0.1 11211 to test memcached function I found the add and set is not working.

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

This is my memcached setup from phpinfo below. This is my memcached setup from phpinfo

I use getResultCode() code below to find some error This is my test result output.

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 

My Test Code is here. Output is in comments.

<?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)



from Memcached not working in web browser,but use php command working

No comments:

Post a Comment