1
我为会话管理数据库使用SqlAlchemy,并根据文档创建了自定义SessionInterface
。当会话数据库关闭时,退回Flask的默认会话管理器?
我的开发人员数据库今天出现故障,现在我无法访问我的网站,正如预期的那样。有没有办法让我在这个事件中回到Flask的默认会话管理器?
这是我目前执行的SessionInterface
class SqlAlchemySessionInterface(SessionInterface):
#...
def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
if sid:
# error is raised here when database is down
stored_session = DBSession.query.filter_by(sid=sid).first()
# ...
我有一个天真的解决崩溃数据库的问题,即在记忆字典利用了作为备份:
# A backup memory storage for sessions
backup = {}
class SqlAlchemySessionInterface(SessionInterface):
#...
def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
if sid:
try:
stored_session = DBSession.query.filter_by(sid=sid).first()
except DatabaseError:
stored_session = backup.get('sid')
# ...