21

I got memcached installed. This is from phpinfo():

enter image description here

But when using it like this:

private static function getZendCacheMemcachedObject()
{
    $frontendOpts = array(
        'caching' => true,
        'lifetime' => 3600,
        'automatic_serialization' => true
    );

    $backendOpts = array(
        'servers' =>array(
            array(
            'host'   => 'localhost',
            'port'   => 11211,
            'weight' => 1
            )
        ),
        'compression' => false
    );

    return Zend_Cache::factory('Core', 'Memcached', $frontendOpts, $backendOpts);
}

public function foo($id)
{
    $cache = self::getZendCacheMemcachedObject();
    $cacheKey = 'foo_'.$id;
    $xml = $cache->load($cacheKey);

    if (false === $xml) {
        $xml = $this->httpClient->foo();
        $cache->save($xml, $cacheKey);
    }

    return $xml;
}

I get this error:

The memcache extension must be loaded for using this backend

Any ideas?

hakre
  • 193,403
  • 52
  • 435
  • 836
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • FYI ZF outputs that error if `extension_loaded('memcache')` returns false, so something weird is going on with your configuration. – Tim Fountain Mar 20 '12 at 17:09
  • Well, the problem seems to be that Zend_Cache_Backend_Memcached is loading memcache library instead of memcached. Why is it called mecached when it uses memcache? Wtf. – Richard Knop Mar 20 '12 at 17:20
  • 1
    This might help: http://serverfault.com/questions/63383/memcache-vs-memcached - looking at my phpinfo, it's certainly 'memcache' that I have installed, whereas yours is listing 'memcached' – Tim Fountain Mar 20 '12 at 17:24

4 Answers4

41

PHP has two Memcached libraries with confusing names :

Your code needs the first one. Just do a simple pecl uninstall memcached and then pecl install memcache, modify your php.ini to include the appropiate .so and it should work.

capi
  • 1,453
  • 12
  • 10
  • 1
    Those names confuse me all the time. I never know whether I'm dealing with Memcache or Memcached or Libmemcached :P – Richard Knop Aug 22 '13 at 09:20
  • 2
    https://xkcd.com/1742/ would be great if you gave the full instructions. What do we add to the php.ini? – Isaac Dec 04 '17 at 20:34
2

for the PHP library that you have installed, it looks like the easiest solution would be to use a different backend - if your zend framework version allows it:

Zend_Cache_Backend_Libmemcached (http://doczf.mikaelkael.fr/1.11/en/zend.cache.backends.html)

i assume that return Zend_Cache::factory('Core', 'Memcached', $frontendOpts, $backendOpts); turns into return Zend_Cache::factory('Core', 'Libmemcached', $frontendOpts, $backendOpts);

john
  • 21
  • 1
2

I solve this issue is quite simple. This issue happen because you didn't install php memcached extension. Let 's install it by this command in Ubuntu

sudo apt-get install php-memcached

Or in other OS you can figure out yourself

Phuc
  • 143
  • 1
  • 7
2

needs extension called php-memcached possible solutions: (notice that extension is different from library, there are libraries called memcache & memchached, and extension called php-memcached. in my case the last one was needed) (on linux)

  • to install extension:
    sudo apt-get install php-memcached
    this will help you.

follow below if library is also needed.

  • to install library itself: sudo apt-get install memcached
  • there is also a library containing memcached: sudo apt-get install libmemcached-tools

to read more and configure it you may want to check here and here

ToTo
  • 89
  • 6