[Zope-Coders] Persistent Sessions question
Evan Simpson
evan@4-am.com
Mon, 14 Oct 2002 17:16:58 -0500
This is primarily a question for Chris, but please chime in if you're
familiar with <ominous echo>Mounting Databases</ominous echo>.
I'm setting up an application where Sessions will save me a lot of work,
but I want to ensure that a user's session data doesn't just disappear
in the middle of their, well, session. In particular, data kept in
temp_folder will go *poof* when I restart the server.
I don't want to keep sessions in the main ZODB, either, since they can
change frequently and there will be a lot of them. Also, I'm using ZEO.
Fun enough yet? ;-)
My solution seems to work, but I'm very nervous about it and want
reassurance. I set up ZEO to serve a 'Sessions.fs' FileStorage in
addition to the normal 'Data.fs'.
I copied & hacked TemporaryFolder into a Product called MountedFolder.
Here are the important guts from the MountedFolderMount class:
def __init__(self, id, label):
self.id = str(id)
self.label = str(label)
MountPoint.__init__(self, path='/',
params='Mountable %s' % self.label)
def _createDB(self, db=None):
""" Fetch the storage to mount from
the Mountables dictionary in custom_zodb. """
# We defer actually creating the Storage until it is needed.
klass, args, kwargs =
sys.modules['Zope.custom_zodb'].Mountables[self.label]
db = DB(klass(*args, **kwargs))
return db
def _getMountRoot(self, root):
sdc = root.get('folder', None)
if sdc is None:
sdc = MountedFolder()
sdc.id = self.id
sdc.storage_label_ = self.label
root['folder'] = sdc
return sdc
Now, I created a MountedFolderMount with label 'Session'. In my
'custom_zodb.py', I created a dictionary called 'Mountables' with a
single entry: the key is 'Session' and the value is a tuple consisting
of ZEO.ClientStorage.ClientStorage, a tuple, and a dictionary containing
the necessary arguments. So, when the MountedFolderMount's _createDB is
called, it instantiates a new ClientStorage and wraps it in a DB.
Having done all this, I created a Transient Object Container in the
MountedFolder and set expiration to about a week. I pointed my Session
Data Manager at the new container, and everything *seems* to work as
planned.
Thanks,
Evan