Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fast, lightweight Sessions middleware for Google App Engine (secure cookies, memcache, or datastore)
Python

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
demo-with-google-logins
demo
gaesessions
.gitignore
README.markdown

README.markdown

GAE Sessions

NOTE: This is beta software. If you discover a problem, please report it on the gae-sessions issues page.

Advantages:

  • Lightweight: One short file and references to a handful of standard libs.
  • High Availability is ensured by persisting all changes to the datastore.
    • If you don't need this, you can use set_quick() and pop_quick() and data will only be changed in memcache.
  • Fast and Efficient
    • Uses memcache to minimize read times.
    • Minimizes gets() and puts() by compactly storing all values in one field.
    • Automatically converts db.Model instances to protobufs for more efficient storage and CPU usage.
    • Frequency of writes is minimized by only writing if there is a change, and only once per request (when the response is being sent).
  • Simple to Use
    • Easily installed as WSGI Middleware.
    • Session values are accessed via a dictionary interface.
    • The session automatically initializes when you first assign a value. Until then, no cookies are set and no writes are done.
    • Sessions expire automatically (based on a lifetime you can specify).

Limitations:

  • Limited to 1MB of data in a session. (to fit in a single memcache entry)
  • No checks for User-Agent or IP consistency (yet).
  • I'm sure you'll have lots to add to this list :).

Installation

After downloading and unpacking gae-sessions, copy the 'gaesessions' folder into your app's root directory.

gae-sessions includes WSGI middleware to make it easy to integrate into your app

  • you just need to add in the middleware. If you're using App Engine's built-in webapp framework, or any other framework that calls the run_wsgi_app function, you can use App Engine's configuration framework to install gae-sessions. Create a file called "appengine_config.py" in your app's root directory, and put the following in it:

    from gaesessions import SessionMiddleware def webapp_add_wsgi_middleware(app): app = SessionMiddleware(app) return app

Example Usage

There is a complete demo application in the 'demo' folder - just launch it with the development server (or upload it to GAE) and check it out. This demo uses OpenID via RPX for user authentication. There's another demo in the 'demo-with-google-logins' folder which uses Google Accounts for authentication Here's a few lines of example code too:

from gaesessions import get_current_session
session = get_current_session()
if session.is_active():
    c = session.get('counter', 0)
    session['counter'] = c + 1
    session['blah'] = 325
    del session.blah  # remove 'blah' from the session
    # model instances and other complex objects can be stored too

    # If you don't care if a particular change to the session is persisted
    # to the datastore, then you can use the "quick" methods.  They will
    # only cause the session to be stored to memcache.  Of course if you mix
    # regular and quick methods, then everything will be persisted to the
    # datastore (and memcache) at the end of the request like usual.
    session.set_quick('x', 9)
    x = session.get('x')
    x = session.pop_quick('x')

Author: David Underhill
Updated: 2010-May-06 (v0.04-beta)
License: Apache License Version 2.0

For more information, please visit the gae-sessions webpage.

Something went wrong with that request. Please try again.