-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpFoundation] MongoDbSessionHandler supports auto expiry via configurable expiry_field #11510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bb336a5
796b18a
2d8b2bd
4d42f32
cb5e562
d748743
2f8742e
fc962db
b55c63b
88b256c
9744f61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,9 +62,10 @@ public function __construct($mongo, array $options) | |
$this->mongo = $mongo; | ||
|
||
$this->options = array_merge(array( | ||
'id_field' => '_id', | ||
'data_field' => 'data', | ||
'time_field' => 'time', | ||
'id_field' => '_id', | ||
'data_field' => 'data', | ||
'time_field' => 'time', | ||
'expiry_field' => false, | ||
), $options); | ||
} | ||
|
||
|
@@ -109,6 +110,9 @@ public function gc($maxlifetime) | |
* | ||
* See: http://docs.mongodb.org/manual/tutorial/expire-data/ | ||
*/ | ||
if (false !== $this->options['expiry_field']) { | ||
return true; | ||
} | ||
$time = new \MongoDate(time() - $maxlifetime); | ||
|
||
$this->getCollection()->remove(array( | ||
|
@@ -123,12 +127,27 @@ public function gc($maxlifetime) | |
*/ | ||
public function write($sessionId, $data) | ||
{ | ||
$fields = array( | ||
$this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY), | ||
$this->options['time_field'] => new \MongoDate(), | ||
); | ||
|
||
/* Note: As discussed in the gc method of this class. You can utilise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment should be in the phpdoc where it is actually visible to users There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably best to submit a new PR with that chance, since this has already been merged and closed. |
||
* TTL collections in MongoDB 2.2+ | ||
* We are setting the "expiry_field" as part of the write operation here | ||
* You will need to create the index on your collection that expires documents | ||
* at that time | ||
* e.g. | ||
* db.MySessionCollection.ensureIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } ) | ||
*/ | ||
if (false !== $this->options['expiry_field']) { | ||
$expiry = new \MongoDate(time() + (int) ini_get('session.gc_maxlifetime')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Tobion: In #11508 (comment), you mentioned that the session lifetime option in Symfony sets the One concern I have is that we're storing the option's value from a particular point in time in this calculated value. If the PHP configuration changes because session lifetimes need to be extended site-wide, this storage layer is still going to delete those records based on the original time. I would assume other storage layers would simply delay their Just for context, ZF's MongoDB session handler stores the lifetime (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But only for records that have not had activity, as they will have the new expireAt set based on whatever the maxlifetime may have been changed to. Not sure how much of an issue this actually is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Good point! I missed that in my initial comment. No objection then, provided Symfony is OK with reading the |
||
$fields[$this->options['expiry_field']] = $expiry; | ||
} | ||
|
||
$this->getCollection()->update( | ||
array($this->options['id_field'] => $sessionId), | ||
array('$set' => array( | ||
$this->options['data_field'] => new \MongoBinData($data, \MongoBinData::BYTE_ARRAY), | ||
$this->options['time_field'] => new \MongoDate(), | ||
)), | ||
array('$set' => $fields), | ||
array('upsert' => true, 'multiple' => false) | ||
); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than most of the existing method body in an
if
statement, it would be preferable to simply do the following at the top of the function:That keeps the diff smaller, which benefits anyone working on the file in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍