skip to Main Content

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)

2

Answers


  1. I faced alittle similiar issue once.
    In my case using ip address instead of ‘localhost’ worked.

    $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, ...);
    }
    
    Login or Signup to reply.
  2. Yes, this issue is related with IP vs hostname. I faced the same issue with official Memcached docker . it was not working with ‘localhost’ or 127.0.0.1 but when tested with DNS name or container IP it worked.

    <?php
    
    /**
     * @license MIT License
     * @copyright maartendekeizer
     */
    
    $memcached = new Memcached();
    $memcached->addServer('memcached', 11211);
    
    $name = 'testkey';
    $ttl = 10;
    $data = sha1(time());
    
    $memcached->set($name, $data, $ttl);
    echo date('His') . ': key "' . $name . '" set to "' . $data . '" with ttl ' . $ttl . PHP_EOL;
    
    for ($i = 0; $i < ($ttl + 5); $i ++) {
      $res = $memcached->get($name);
      echo date('His') . ': key "' . $name . '" data is "' . $res . '" and that is ' . ($res == $data ? 'a match' : 'not a match') . PHP_EOL;
      sleep(1);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search