|
| 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