Skip to content

Commit 22a270e

Browse files
committed
Merge pull request #3017 from adrienbrault/session-metadata
Add a cookbook entry on how to prevent unnecessary session writes
2 parents 26b2e9f + a866606 commit 22a270e

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
* :doc:`/cookbook/session/locale_sticky_session`
149149
* :doc:`/cookbook/session/sessions_directory`
150150
* :doc:`/cookbook/session/php_bridge`
151+
* :doc:`/cookbook/session/limit_metadata_writes`
151152

152153
* **symfony1**
153154

cookbook/session/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ Sessions
77
proxy_examples
88
locale_sticky_session
99
sessions_directory
10-
php_bridge
10+
php_bridge
11+
limit_metadata_writes
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
.. index::
2+
single: Limit Metadata Writes; Session
3+
4+
Limit session metadata writes
5+
=============================
6+
7+
.. versionadded:: 2.4
8+
The ability to limit session metadata writes was added in Symfony 2.4.
9+
10+
The default behaviour of PHP session is to persist the session regardless of
11+
whether the session data has changed or not. In Symfony, each time the session
12+
is accessed metadata is recorded (session created/last used) which can be used
13+
to determine session age and idle time.
14+
15+
If for performance reasons you wish to limit the frequency at which the session
16+
persists, this feature can adjust the granularity of the metadata updates and
17+
persist the session less often while still maintaining relatively accurate
18+
metadata. If other session data is changed, the session will always persist.
19+
20+
You can tell Symfony not to update the metadata "session last updated" time
21+
until a certain amount of time has passed, by setting
22+
``framework.session.metadata_update_threshold`` to a value in seconds greater
23+
than zero:
24+
25+
.. configuration-block::
26+
27+
.. code-block:: yaml
28+
29+
framework:
30+
session:
31+
metadata_update_threshold: 120
32+
33+
.. code-block:: xml
34+
35+
<?xml version="1.0" encoding="UTF-8" ?>
36+
<container xmlns="http://symfony.com/schema/dic/services"
37+
xmlns:framework="http://symfony.com/schema/dic/symfony"
38+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
39+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
40+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
41+
42+
<framework:config>
43+
<framework:session metadata-update-threshold="120" />
44+
</framework:config>
45+
46+
</container>
47+
48+
.. code-block:: php
49+
50+
$container->loadFromExtension('framework', array(
51+
'session' => array(
52+
'metadata_update_threshold' => 120,
53+
),
54+
));
55+
56+
.. info::
57+
58+
PHP default's behavior is to save the session whether it has been changed or
59+
not. When using ``framework.session.metadata_update_threshold`` Symfony
60+
will wrap the session handler (configured at
61+
``framework.session.handler_id``) into the WriteCheckSessionHandler, that
62+
will prevent any session write if the session was not modified.
63+
64+
.. caution::
65+
66+
Be aware that if the session is not written at every request, it may be
67+
garbage collected sooner than usual. This means that your users may be
68+
logged out sooner than expected.

0 commit comments

Comments
 (0)