views:

135

answers:

2

I'm setting up cacheing for a CakePHP application and having a really hard time getting it to talk to Memcache.

I've written the caching code while testing against the File based caching engine, and all the logic is solid. When I watch debug traffic, caches hit, miss, and expire properly. I wanted to switch over to Memcache because the Filesytem based cache doesn't really offer any performance improvement, but I can't seem to get it to work at all.

As soon as I change the Caching engine in Cache::config to Memcache, I get the following error:

Warning (512): Cache not configured properly. Please check Cache::config(); in APP/config/core.php [CORE/cake/libs/configure.php, line 663]

This happens with a simple configuration call, like:

Cache::config('default', array(
    'engine' => 'Memcache'
    ));

Or a more complicated one, like the default one suggested in core.php:

Cache::config('default', array(
        'engine' => 'Memcache', //[required]
        'duration'=> 3600, //[optional]
        'probability'=> 100, //[optional]
        'prefix' => Inflector::slug(APP_DIR) . '_', //[optional]  prefix every cache file with this string
        'servers' => array(
        '127.0.0.1:11211' // localhost, default port 11211
        ), //[optional]
        'compress' => false, // [optional] compress data in Memcache (slower, but uses less memory)
        ));

Cache.check is set to true, eg Configure::write('Cache.check', true); and Cache.disable is not set. Again, all the caching logic works great as long as I'm using 'engine' => 'File'.

I get the same error whether memcached is running or not. This error seems to encompass a ton of different problems - extensive searching has revealed a huge variety of potential solutions to problems with this error message. I've double checked that my tmp directories exist (per http://ryan.ifupdown.com/2009/08/05/warning-512-cache-not-configured-properly-please-check-cacheconfig-in-appconfigcore-php-corecakelibsconfigure-php-line-663/), I'm running on Ubuntu, so there are no pathing issues (which have been a problem in the past, per https://trac.cakephp.org/ticket/4433 and http://cakephp.1045679.n5.nabble.com/cake-1-2-problem-with-cache-td1302563.html). I am running a relatively old version of Cake (1.2.5), but I tried upgrading and had the same error message (along with a million other upgrade-related errors that I don't really have time to hunt down.)

Is there any way to get more detailed information out of Cake about what's actually going wrong? I get the feeling that this error is just a catch-all error for anything that goes wrong in the cache class initialization, and there are tons of potential problems. Changing the debug level up to 3 doesn't reveal anything more. If I could get more info I might be able to do something, but I'm totally flying blind. I'm a mostly-novice Cake user, so any general guidance about best practices for dealing with bizarre Cake bugs would be much appreciated!

A: 

Usually this means that the memcache server isn't actually running. Can you make sure that the server is running locally, and on port 11211 (which is the default)?

JoeyP
Yep, double checked both of those things. I'm not seeing anything in the memcached log, though - should I be? Would normal attempts to connect to it trigger log messages? Are there other convenient memcached clients I could use to make sure memcache is functioning properly?
drewww
This is a really good script that emulates APC's stats script for memcache. http://livebookmark.net/journal/2008/05/21/memcachephp-stats-like-apcphp/. It will also tell you weather memcache is running or not.
JoeyP
Ah, yeah, that's exactly what I was looking for. It shows all systems normal - connects to memcache fine. Not sure what to make of this...
drewww
Okay, so I think I've isolated the problem. It looks like it can't find the Memcache class. The test file you recommended worked fine because it connects directly to the memcache server socket, and doesn't use a PHP API at all. But if I try to use the Memcache classes, it fails. I'm not used to managing libraries and paths in PHP, but at least now I think I have a potential direction to work in. Thanks for your help!
drewww
Ah,run "sudo pecl install memcache", assuming you're on linux
JoeyP
Did that, no luck. I didn't think this would matter so I didn't mention it earlier, but I'm using nginx and fast_cgi, and I'm starting to suspect that the real issue is that the fast_cgi process isn't loading php.ini or something, and that I need to help encourage those processes to recognize the presence of a new library. Still trying to work out how to do that, though.
drewww
A: 

You need to install the Memcache lib for PHP. You should be able to install it using the "php5-memcache" package.

giulianob
I have that package installed already, but it's still not recognizing it. I've seen some mentions of installing it via pear, which I'm going to try tomorrow...
drewww
Did you enable it properly? I'm pretty sure your problem is related to not having the package installed because I just ran into the same problem myself. Check your /etc/php5/conf.d/memcache.ini and make sure the extension=memcache.so line isn't commented out.
giulianob
It's there and not commented out. :/ Going to try the pear route now.
drewww
I have no idea what happened or changed, but after uninstalling/reinstalling pear + unbuntu packages, I finally got the classes working and the Cake Cache class initializing properly. I'm still not sure precisely what was wrong, but redoing the installation of the php parts of it seemed to help.
drewww
Basically if you didn't see memcache listed in your phpInfo() output then you would get the problems with CakePHP. I analyzed their code and the first thing they do is check to see if the PHP Memcache class exists, if not, then returns error.
giulianob
Yeah, but the weird thing was it was showing up in phpinfo(). I think there might have been some confusion between "memcache" and "memcached" which are apparently both php libraries that provide memcache access, but the 'd' version is newer and better maintained? I might have installed both and there was a conflict or something, but I was trying lots of different fixes and am unfortunately not 100% sure which did it.
drewww