We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Fatal Error: class 'memcache' not found

Hi all,

When trying to use the Memcache feature, I systematically get the error mentioned in the title.

I'm using PHP5-FPM with php 5.4.9 on a Ubuntu 13.04. The memcached server, libmemcached10 and the PHP memcached extension are all installed and functional:


dpkg -l memcached*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                       Version            Architecture       Description
+++-==========================-==================-==================-===============================================
ii  memcached                  1.4.14-0ubuntu1    amd64              A high-performance memory object caching system```

```bash
dpkg -l libmemcached*
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                       Version            Architecture       Description
+++-==========================-==================-==================-================================================
un  libmemcached               <none>                                (no description available)
ii  libmemcached10:amd64       1.0.8-1            amd64              C and C++ client library to the memcached server
un  libmemcached2              <none>                                (no description available)
un  libmemcached3              <none>                                (no description available)
un  libmemcached6              <none>                                (no description available)
un  libmemcached9              <none>                                (no description available)```

```bash
php5-fpm -m|grep memcache
memcached```

```bash
php5-fpm -i|grep memcache
/etc/php5/fpm/conf.d/20-memcached.ini,
memcached
memcached support => enabled
libmemcached version => 1.0.8
memcached.compression_factor => 1.3 => 1.3
memcached.compression_threshold => 2000 => 2000
memcached.compression_type => fastlz => fastlz
memcached.serializer => php => php
memcached.sess_binary => 0 => 0
memcached.sess_lock_wait => 150000 => 150000
memcached.sess_locking => 1 => 1
memcached.sess_prefix => memc.sess.key. => memc.sess.key.
Registered save handlers => files user memcached```

Now, the error message I get seems to indicate that Phalcon actually uses memcache and not memcache*d*.

If that's the case, must I implement my own backend memcached then?

Thanks,
Steven


14.7k

Ok I just uninstalled memcached and installed memcache, but I get the same error message... Here's where it goes wrong:


$frontCache = new \Phalcon\Cache\Frontend\Data(array(
    'lifetime' => 86400
));
$mcache = new \Phalcon\Cache\Backend\Memcache($frontCache, array(
    'host' => 'localhost',
    'port' => 11211
));
$mcache->get('some_id'); // I get the error message on this call```

So what am I doing wrong?

Thanks,
Steven


14.7k

This works, by the way (after uninstalling memcache and reinstalling memcached): ```php<?php $m = new Memcached(); $m->addServer('localhost', 11211);

$m->set('foo', 100); var_dump($m->get('foo')); // int 100```



14.7k

Ok I figured it out. Phalcon assumes the use of the (older) php5-memcache client instead of the (better) php5-memcached client. I can keep the memcached server, I just need to replace the memcached client by a memcache client.

I guess I'll need to write up a new backend, because there is none in the incubator either: https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Cache/Backend :)

If any moderator stops by, this question can be marked "closed".

Thanks, Steven



98.9k

I didn't know there was a better memcache extension, nice to know that :)



13.4k

i too have the same problem,

phalcon has no support for "memcached" but only "memcache", however its indsutry common knowlege and trend is to use the "memcached" as its more reliable than memcache extenstion it self. Plus php7 dont have support for the "memcache" only memcached is supported.

should write a new backend for memcached i guess soon.

i have it error to. it problem not resolved yet? php-7.1 via fpm. memcached installed. error on model cache set: Fatal error: Class 'memcache' not found in ... how fix it if i have memcached? @phalcon

i have it error to. it problem not resolved yet? php-7.1 via fpm. memcached installed. error on model cache set: Fatal error: Class 'memcache' not found in ... how fix it if i have memcached?

Hi, I've got the same PROBLEM, Has anybody resolved it yet ?

Background: memcached & memcache install on the Docker container but the application is complaining about

<br />
<b>Fatal error</b>:  Class 'memcache' not found in
<b>/home/user/myapp/public/index.php</b> on line
<b>23</b>
<br />

Try it (real code from my project, for use memcached to caching DB-data.)

<?php

// ...

use Phalcon\Cache\Frontend\Data as FrontendData;
use Phalcon\Cache\Backend\Libmemcached;

// ...

$di->set('modelsCache', function () {
    // Cache data for one day (default setting)
    $frontCache = new FrontendData([
        'lifetime' => 86400,
    ]);

    // Memcached connection settings
    $cache = new Libmemcached(
        $frontCache,
        [
            'servers' => [
                [
                    'host' => 'localhost',
                    'port' => '11211',
                    'weight' => 1,
                ]
            ],
            'client' => [
                \Memcached::OPT_HASH       => \Memcached::HASH_MD5,
                \Memcached::OPT_PREFIX_KEY => "mysite.",
            ],

        ]
    );

    return $cache;

});