开发者

Using cache systems for session storage?

Say I have a caching system that i can use for storing sessions:

// example i have a cache class
$memcached->add('key','value');
// then i can get the session from
$memcached->get('key');

What are the advantages and disadvantages of using caching (e.g. memcached, maybe Redis can fit in, or other thin开发者_如何学JAVAgs) for sessions rather than using PHP's built-in sessions?


I want to start by clarifying. When you're using PHP session storage, the sessions are being stored in a file (usually in /tmp). Each session becomes it's own file.

When you use memcached or redis to store sessions, nothing in your PHP code changes.

You can simply configure php to use redis or memcache to store the sessions instead (configured via session save handler in php.ini).

Pros:

  • session storage/retrieval becomes much much faster

cons:

  • you need to configure php.ini (or implement the session save handler yourself)

now, I would actually recommend using redis instead of memcache, simply because the sessions will become permanent, so if you need to reboot the server, all your users wont get logged out.

For redis: https://github.com/owlient/phpredis (see "session handler" lower on the page)


You should be able to have PHP store session data in Apache's runtime memory by setting session.save_handler to mm. However, to do this you need to compile PHP with the memory management module (--with-mm), which I don't think is available for Windows.

If you want to use memcached or some other caching mechanism, then it'd probably be best to implement user-defined storage handlers using session_set_save_handler so you don't have to rewrite your session management code.

If you do that, then I don't think there are any obvious disadvantages to storing session data in that way. The obvious advantage is speed.

Edit: I came across this page which discusses, aside from speed, the main advantages/disadvantages of using memcached for storing sessions, namely:

  • It's easy to share sessions across multiple webservers without using sticky sessions.
  • However, memcached makes no promises of keeping the data up until expiration—only that the data will not be available after expiration. So if memcached is low on ram, hasn't been used lately, or the server goes down at all, the session data will be lost.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜