ShelveSessionStore.py

#

Store sessions in a ‘shelve’ database.

import shelve
from session3.store.SessionStore import SessionStore
#

Open a ‘shelve’ dictionary with the given filename, and store sessions in it.

Shelve is not thread safe or multiprocess safe. See the “Restrictions” section for the shelve module in the Python Library Reference for information about file locking.

class ShelveSessionStore(SessionStore):
#
    is_multiprocess_safe = False  # DBM isn't process safe.
    is_thread_safe = False        # Don't know about this...
#

init takes the filename to use as the shelve store.

    def __init__(self, filename):
#
        self.filename = filename
#

Open the shelve store file.

    def open(self):
#
        return shelve.open(self.filename, 'c')
#

Load the session from the shelf.

    def load_session(self, id, default=None):
#
        
        db = self.open()
        return db.get(id, default)
#

Delete the given session from the shelf.

    def delete_session(self, session):
#
        db = self.open()
        del db[session.id]
#

Save the session to the shelf.

    def save_session(self, session):
#
        db = self.open()
        db[session.id] = session