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

How to implement memcache for handling session storage?

I need to implement Memcache session storage so that i could authorize websockets (ratchet). I'm a beginner in web dev, so i have some hard time understanding how libraries, classes, etc are being implemented into the project.

My project is based on Vokuro. What i want to achieve is, session storage in memcache rather than native phalcon adapter. I've seen this memcache adapter, but i don't understand how to implement it in my project.

Also, finally when i get adapter memcache adapter functioning, will i have to modify Vokuro's authentication classes or will it still work as it was with native adapter.

Thanks in advance!



16.0k
Accepted
answer

Register the incubator namespace in bootstrap

$loader = new Phalcon\Loader();

$loader->registerNamespaces(array(
    'Phalcon' => '/path/to/incubator/Library/Phalcon/'
));

$loader->register();

Set the session details in bootstrap and phalcon will do the rest.

$di->set('session', function(){

    $memcache = new Phalcon\Session\Adapter\Memcache(array(
        'host'          => '127.0.0.1',     // mandatory
        'port'          => 11211,           // optional (standard: 11211)
        'lifetime'      => 8600,            // optional (standard: 8600)
        'prefix'        => 'my-app'         // optional (standard: [empty_string]), means memcache key is my-app_31231jkfsdfdsfds3
        'persistent'    => false            // optional (standard: false)
    ));
    $memcache->start();
    return $memcache;
});

https://github.com/phalcon/incubator

https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Session/Adapter