清理服务器端会话文件-使用文件系统的Flask-Session [英] Clean server-side session files - Flask-Session using filesystem
问题描述
我选择通过Flask-Session在Flask中使用服务器端会话管理.
I chose to use a server-side session management with Flask using Flask-Session.
我使用filesystem
存储数据,并且按预期方式,这些文件存储在配置目录中的/flask_session
文件夹下.
I store the data using filesystem
and as expected, these files are stored under a /flask_session
folder in my config directory.
这是我在__init__.py
# __init__.py
from flask_session import Session
[...]
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SECRET_KEY'] = config.SECRET_KEY
sess = Session()
sess.init_app(app)
按预期,会生成会话文件&存储在/flask_session
As expected, session files generated & stored under /flask_session
▾ flask_session/
1695e5cbf9b4edbbbb82a8ef1fad89ae
192761f7ce8e3cbf3ca11665133b7794
2029240f6d1128be89ddc32729463129
...
问题是:这些文件是否在特定时间后(例如,作为会话存储在客户端)由flask_session
自动删除?如果是,是否可以减少/增加此时间?
Question is: Are these files automatically removed by flask_session
after a specific amount of time (ie. as the session stored client-side)? If yes, is it possible to decrease/increase this timing?
推荐答案
正如Danila Ganchar所说,使用PERMANENT_SESSION_LIFETIME
可以控制会话的到期时间.
As Danila Ganchar commented, using PERMANENT_SESSION_LIFETIME
allows to control the session expiration time.
Flask-Session使用与Flask本身相同的内置配置(与会话相关).来自 Flask-Session 文档:
Flask-Session use the same builtin config than Flask itself (related to session). From Flask-Session doc:
以下配置值是内置配置值 与会话相关的Flask本身.他们都是 例如,Flask-Session可以理解,您应该使用 PERMANENT_SESSION_LIFETIME来控制会话的生存期.
The following configuration values are builtin configuration values within Flask itself that are related to session. They are all understood by Flask-Session, for example, you should use PERMANENT_SESSION_LIFETIME to control your session lifetime.
示例:
# __init__.py
from flask_session import Session
from datetime import timedelta
app.config['SESSION_PERMANENT'] = True
app.config['SESSION_TYPE'] = 'filesystem'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=5)
# The maximum number of items the session stores
# before it starts deleting some, default 500
app.config['SESSION_FILE_THRESHOLD'] = 100
app.config['SECRET_KEY'] = config.SECRET_KEY
sess = Session()
sess.init_app(app)
这篇关于清理服务器端会话文件-使用文件系统的Flask-Session的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!