开发者

cakePHP memcache logic not working

开发者 https://www.devze.com 2023-03-27 10:32 出处:网络
My problem is that memcache logic inside my cakePHP application is not working on my local system, ever since I set it up here, by taking code from existing setup of teammates. I have checked that Mem

My problem is that memcache logic inside my cakePHP application is not working on my local system, ever since I set it up here, by taking code from existing setup of teammates. I have checked that Memcached service is running on my system and phpinfo() shows memcache section enabled.

But, things like these are not working -

$this->Memcache->set($key,$value);

CakePHP uses its wrapper for Memcache, v. 0.3.

If I debug like this -

echo "<pre>";
echo "checking";
error_reporting(-1); 
$this->Memcache->set($key,$countryNetworkWiseReportData,3600);
echo "finished";
exit;

I get -

checkingfinished

Strict standards: Non-static method Cache::write() should not be called statically, assuming $this from incompatible context in D:\cake1.2\cake\libs\configure.php on line 690

... and similar strict standard notifications for Cache::getInstance() etc.

But note the notifications appear after "checkingfinished", so I am confused if that is actually relevant.

I tried the command -

telnet 127.0.0.1:11211

Which gives -

Connecting To 127.0.0.1:11211...Could not open connection to the host, on port 23: Connect failed

Also tried -

telnet localhost:11211 (in case firewall issues prevent connection to 127.0.0.1)

But got the same error.

I also tried this script called memcache.php. I put $arr= "127.0.0.1:11211"; in the code and I got this result on my system -

cakePHP memcache logic not working

What do I interpret from this data? I am getting a Warning in the Start time section - Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the ....

So far, it appears to me that connection to the default port 11211 is due to some reason not allowed in my cakePHP project code. But again, how then memcache.php is able to connect to the memcache server and able to display those data.

Further Details

Thanks to Tudor Constantin, doing telnet 127.0.0.1 11211 does not throw any error anymore, shows an empty screen... which I guess is fine.

I checked further in code, we have model functions which have logic like this -

if(!$this->memcacheCommon())
{
    $this->log('Error in memcache', LOG_DEBUG);
    die('Error in memcache');
}

//like in others system, in my system too, it passes above condition

$memcachedata = $this->Memcache->get($key);
//does not return data in my system, because $this->Memcache->set($key,$data); never sets the data correctly.

if(got data from memcache)
{
    //Return that data
}
else
{
    //Get data from database
    $this->Memcache->set($key,$data); //does not the data in my setup - set() function returns false
}

So, I went inside the set() function of class CakeMemcache(), and there is this line in the end

return @$this->_Memcache_cluster_new->set($key, $var, 0, time()+$expires);

this returns false, and I don’t know what to debug from here.

One more thing that is confusing me is memcacheCommon() inside app_model.php has these lines –

$this->Memcache = new CakeMemcache();
$this->Memcache->_connect();

And inside CakeMemcache()->_connect(), there are these lines –

$this->_Memcache_standalone =& new Memcache();
$this->_Memcache_cluster_new =& new Memcache();

I am not sure what exactly do they do.

Will appreciate any pointers... thanks

More Details

I have somehow lost the earlier memcache.php file, using which I got the above graphical memcache usage display (had posted the pic of the output I got, above). I later downloaded memcache-3.0.6 from http://pecl.php.net/package/memcache/3.0.6 and tried running the example.php and memcache.php files which are present inside the archive.

Firstly, running example.php gives me following error -

Notice: memcache_connect(): Server localhost (tcp 11211) failed with: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has 开发者_Python百科failed to respond.

Warning: memcache_connect(): Can't connect to localhost:11211, A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

Connection to memcached failed

I checked that memcached service is running (in windows services list). Also telnet localhost 11211 does not give any error and shows an empty command window.

Secondly, running memcache.php and entering the credentials, gives me the following, on clicking all the tabs (Refresh Data, View Host stats, variables ) -

Notice: Use of undefined constant values - assumed 'values' in path\to\file\memcache.php on line 57
Cant connect to:v:a

Line 57 was -

function get_host_port_from_server($server){
    $values = explode(':', $server);
    if (($values[0] == 'unix') && (!is_numeric( $values[1]))) {
        return array($server, 0);
    }
    else {
        return values;   //line 57
    }
}

How come that was values, I downloaded it from http://pecl.php.net/package/memcache/3.0.6. After I changed it to return $values; I got ->

Cant connect to:mymemcache-server1:11211

. I don't exactly remember how earlier I was able to get that memcache server graph (I posted the pic above).

Thirdly, on command line, after connecting via telnet, command stats gives the following -

STAT pid 1584
STAT uptime 2856
STAT time 1315981346
STAT version 1.2.1
STAT pointer_size 32
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 1
STAT total_connections 3
STAT connection_structures 2
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT bytes_read 7
STAT bytes_written 0
STAT limit_maxbytes 67108864
END


As I can see, you are on a windows machine, so the telnet command is:

telnet 127.0.0.1 11211

Notice the space instead of colon. This might help you debug the memcache connection


Yay! I found the solution!! It started working after I changed localhost to 127.0.0.1 in the /app/config/fcore.php file.

Made the following changes -

# Memcache server constants
define('MEMCACHE_SERVER', 'localhost:11211');
define('MEMCACHE_SERVER_CLUSTER', 'localhost:11211');
define('MEMCACHE_SERVER_CLUSTER_NEW', 'localhost:11211');

to

# Memcache server constants
define('MEMCACHE_SERVER', '127.0.0.1:11211');
define('MEMCACHE_SERVER_CLUSTER', '127.0.0.1:11211');
define('MEMCACHE_SERVER_CLUSTER_NEW', '127.0.0.1:11211');

Oh, I suffered so much due to this and I think I could not, in my life, fix this.

Luckily, our team setup a new repository for the project with 127.0.0.1 instead of localhost and when I took checkout memcache started working and later I realized this was the reason.

0

精彩评论

暂无评论...
验证码 换一张
取 消