如何在 Rails 中为基于 cookie 的会话动态设置到期时间 [英] How to I dynamically set the expiry time for a cookie-based session in Rails
问题描述
我目前正在为我的 Rails 应用程序使用基于 ActiveRecord 的会话存储,我有一个后台进程每 30 分钟清除一次非活动会话.
I'm currently using the ActiveRecord-based session store for my Rails app and I have a background process which clears out inactive sessions every 30 minutes.
我想切换到 Rails 新的基于 cookie 的会话存储,但如何将会话的到期时间设置为 30 分钟,而不是默认的会话结束时"值?
I'd like to switch to Rails' new cookie-based session store but how do I set the expiry time of the session to 30 minutes, as opposed to the default 'at end of session' value?
推荐答案
我在办公室交谈后偶然发现了这个问题.只是为了完整起见,我发现有可能在一段时间不活动后使会话过期,并且它内置于 Rails 中.在 config/environment.rb 中,执行以下操作:
I stumbled across this question after a conversation in the office. Just for the sake of completeness, I've discovered that it is possible to expire sessions after a period of inactivity and it's built into Rails. In config/environment.rb, do something along the lines of:
config.action_controller.session = {
:key => 'whatever',
:secret => 'nottellingyou',
:expire_after => 30.minutes
}
查看lib/action_controller/session/cookie_store.rb#114 用于(显然未记录的)正在运行的选项.看起来它自 2008 年 12 月转向 Rack 会话以来就已经存在了.
Check out lib/action_controller/session/cookie_store.rb#114 for the (apparently undocumented) option in action. Looks like it's been around since the move to Rack sessions back in December 2008.
这篇关于如何在 Rails 中为基于 cookie 的会话动态设置到期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!