SessionStore

A general session store.

class SessionStore.SessionStore(app)

Bases: object

A general session store.

SessionStores are dictionary-like objects used by Application to store session state. This class is abstract and it’s up to the concrete subclass to implement several key methods that determine how sessions are stored (such as in memory, on disk or in a database). We assume that session keys are always strings.

Subclasses often encode sessions for storage somewhere. In light of that, this class also defines methods encoder(), decoder() and setEncoderDecoder(). The encoder and decoder default to the load() and dump() functions of the pickle module. However, using the setEncoderDecoder() method, you can use the functions from marshal (if appropriate) or your own encoding scheme. Subclasses should use encoder() and decoder() (and not pickle.load() and pickle.dump()).

Subclasses may rely on the attribute self._app to point to the application.

Subclasses should be named SessionFooStore since Application expects “Foo” to appear for the “SessionStore” setting and automatically prepends Session and appends Store. Currently, you will also need to add another import statement in Application.py. Search for SessionStore and you’ll find the place.

TO DO

  • Should there be a check-in/check-out strategy for sessions to prevent concurrent requests on the same session? If so, that can probably be done at this level (as opposed to pushing the burden on various subclasses).

__init__(app)

Initialize the session store.

Subclasses must invoke super.

application()

Return the application owning the session store.

cleanStaleSessions(_task=None)

Clean stale sessions.

Called by the Application to tell this store to clean out all sessions that have exceeded their lifetime.

clear()

Clear the session store, removing all of its items.

Subclasses must implement this method.

decoder()

Return the value deserializer for the store.

encoder()

Return the value serializer for the store.

get(key, default=None)

Return value if key available, else return the default.

has_key(key)

Check whether the session store has a given key.

items()

Return a list with the (key, value) pairs for all sessions.

iteritems()

Return an iterator over the (key, value) pairs for all sessions.

iterkeys()

Return an iterator over the stored session keys.

itervalues()

Return an iterator over the stored values of all sessions.

keys()

Return a list with the keys of all the stored sessions.

Subclasses must implement this method.

pop(key, default=None)

Return value if key available, else default (also remove key).

Subclasses must implement this method.

setEncoderDecoder(encoder, decoder)

Set the serializer and deserializer for the store.

setdefault(key, default=None)

Return value if key available, else default (also setting it).

Subclasses must implement this method.

storeAllSessions()

Permanently save all sessions in the store.

Used when the application server is shut down.

Subclasses must implement this method.

storeSession(session)

Save potentially changed session in the store.

Used at the end of transactions.

Subclasses must implement this method.

values()

Return a list with the values of all stored sessions.

SessionStore.dumpWithHighestProtocol(obj, f)

Same as pickle.dump, but by default with the highest protocol.