开发者

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 开发者_StackOverflow中文版libraries/extensions? As far as performance improvement what have you seen? Any caveats?


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.


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.


It's not practical to use Memcached for storage of relational data (like MySQL); it would be inefficient to request each item from Memcached and then test to see if it matches a query. There are better solutions to a problem like that (consider Memory tables in MySQL, for instance).

On the other hand, if you're looking for simple key/value storage, that's certainly a practical application for Memcached. What I'd be a little wary of, though, is writing a CodeIgniter driver for it. The interface for Memcached in PHP is already dead simple:

$memcached->get('my key');
$memcached->set('my key', 'my value');

I would suggest simply using the Memcached classes directly. Adding all the extra overhead to CI just seems dirty and unnecessary to me.

On the flip-side of that, I've seen implementations of Memcached used for CodeIgniter's session engine. That's certainly a very valid reason to write a driver, and I would highly encourage it (sessions are a pain in the neck to scale).

Good luck

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜