Skip to content

Instantly share code, notes, and snippets.

@oinume
Created March 27, 2014 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oinume/9811874 to your computer and use it in GitHub Desktop.
Save oinume/9811874 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# $ pip install beaker flask redis git+git://github.com/bbangert/beaker_extensions.git
from flask import Flask, session
from flask.sessions import SessionInterface
from beaker.middleware import SessionMiddleware
session_opts = {
'session.type': 'redis',
'session.url': '127.0.0.1:6379',
}
class BeakerSessionInterface(SessionInterface):
def open_session(self, app, request):
return request.environ['beaker.session']
def save_session(self, app, session, response):
session.save()
app = Flask(__name__)
@app.route('/')
def index():
if not session.has_key('value'):
session['value'] = 'Save in session'
return "Session value set."
else:
return session['value']
if __name__ == '__main__':
app.wsgi_app = SessionMiddleware(app.wsgi_app, session_opts)
app.session_interface = BeakerSessionInterface()
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment