Symfony2,在数据库中配置pdo会话存储 [英] Symfony2, Configure pdo session storage in database
问题描述
对于我的Symfony2项目,我正在使用数据库中的会话存储.
For my Symfony2 project, i'm using the session storage in a database.
所以,我像这样配置config.yml:
So, i configure my config.yml like that :
framework:
session:
handler_id: session.handler.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
services:
pdo:
class: PDO
arguments:
- "pgsql:host=%database_host%;dbname=%database_name%"
- "%database_user%"
- "%database_password%"
calls:
- [setAttribute, [3, 2]]
session.handler.pdo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
arguments: ["@pdo", "%pdo.db_options%"]
没关系,当我登录时,将在数据库中创建一个条目,并且会话运行正常.
All it's ok, when i login, an entry is created in my database and the sessions works fine.
但是,如何在数据库中定义会话的生存期?如果我的应用程序中只有4个成员,为什么我的会话表中有很多行(+50)?
But, how can i define the lifetime of my session when its in the database ? Why do I have a lot of lines (+50) in my session table, if I only have 4 members on my application ?
我该如何配置?会员登录后,我只需要一个会话.
How can i configure that ? I just need one session when a member login.
推荐答案
您可以通过更频繁地运行会话垃圾收集器来减少会话表中的行数.
You can reduce the number of rows in your session table by running the session garbage collector more frequently.
http://www.php. net/manual/en/session.configuration.php#ini.session.gc-probability
在您的php.ini文件中
In your php.ini file
session.gc_probability = 1
session.gc_probability=1
session.gc_divisor = 1
session.gc_divisor=1
session.gc_maxlifetime = 36000
session.gc_maxlifetime=36000
这些设置将以100%的概率运行垃圾收集器,对于生产来说不建议使用 ,但是您应该能够验证其是否有效并根据需要进行调整.
These settings will run the garbage collector with 100% probability, which is not recommended for production but you should be able to verify it works and tweak the settings as needed.
这篇关于Symfony2,在数据库中配置pdo会话存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!