5

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)
11
  • These are the relevant runtime configuration options: php.net/manual/en/memcached.configuration.php. Compare your php.ini file between the CLI options and your webserver's options. Commented May 21, 2018 at 21:20
  • @Mike I only find "extension=extension=/usr/local/php-memcached/modules/memcached.so", and "memcache.serializer = igbinary",no other setting about memcached Commented May 22, 2018 at 22:40
  • Is that the same as your webserver's php.ini? Commented May 23, 2018 at 3:31
  • except memcached.sess_lock_wait is not set, other setting is the same . I already add "memcached.sess_lock_wait=150000" into php.ini then restart php-fpm,but have no effect. Commented May 23, 2018 at 16:40
  • Well looking at your phpinfo file it looks like memcached is enabled for the web server. Try creating a Minimal, Complete, and Verifiable example. In the code above you are extending API, but haven't included that code. Commented May 23, 2018 at 16:54

2 Answers 2

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, ...);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I try to use public ip, still can't fix the problem.
Try with 127.0.0.1 instead? 敬錞潘
0

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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.