Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using memcached as a session storage with CodeIgniter

I am researching possibilities of using memcached as a session storage for a system built on CodeIgniter. Has anybody done this before(that's probably a stupid question :) and if so what's your experience folks? Have you used any existing libraries/extensions? As far as performance improvement what have you seen? Any caveats?

like image 646
Alex N. Avatar asked Apr 11 '10 16:04

Alex N.


2 Answers

Having PHP put the sessions into Memcache directly, rather than through framework code is easy - it's just changing two lines in the PHP.ini:

# see http://php.net/manual/en/memcache.ini.php
session.save_handler = memcache
session.save_path="tcp://127.0.0.1:11211?persistent=1&weight=1&timeout=1&retry_interval=15"

This uses the slightly older (but still entirely supported) 'memcache' extension from PECL.

like image 71
Alister Bulman Avatar answered Nov 15 '22 22:11

Alister Bulman


You can choose the CodeIgniter Multicache Library, which can be found here: http://www.haughin.com/code/multicache/

In the code you can simple use like this:

$this->load->library('cache');
//To use memcache
$this->cache->useMemcache($iptomemcache, $port); /*if you want, you can check to see if the connection even worked, as this will return false if the connection failed.*/
$this->cache->save('testkey', 'testdata', NULL, 3600); /*caches the testdata string for 1 hour. */
echo $this->cache->get('testkey');
//To switch back to file based caching
$this->cache->useFile();
//etc.
like image 34
Sergey Kuznetsov Avatar answered Nov 15 '22 22:11

Sergey Kuznetsov