转:MongoSession – A PHP MongoDB Session Handler
About
MongoSession is a PHP implementation of a MongoDB session wrapper. This class was built as a drop-in for easily switching to handling sessions using Mongo. It’s a great replacement to memcache(d) for VPS servers where you risk memory being reshuffled in the pool and taking performance hits.
A Word of Warning
Based on a new blog post entitled MongoDB Performance & Durability, you should be made fully aware that the current stable release of MongoDB, 1.4.4, risks corrupting or losing session data. There are no guarantees of 100% reliability. If you are familiar with this topic, please chime in in the comments section for clarification. For the rest of you, please consider reading the linked article. Certain preventative measures are available to reduce this risk. Possibilities include setting up a replica pairs (replica sets will be available in 1.6). Alot of the concerns are minor when we’re talking about simple user login validation. You might be risking a maximum of losing a minute’s worth of data in the worst case and forcing a re-login.
I have recently added two boolean constants to the library which allow you to specify whether you’d like to increase the apparent consistency of the session handler. You may toggle both FSYNC and SAFE to ensure session data is written to disk before returning.
In the comments section, PHPGangsta has also reported that the default session handler uses file locking to avoid race conditions. He links to a great article, Race Conditions with AJAX and PHP Sessions, which spells out the issue and the possible negative outcomes of not implementing locking.
I have recently updated the library to support atomic operations on both session writes and garbage collection to help prevent these race conditions.