ZSession Help

ZSession is a server-side application and session object manager that can store Session and Application state in RAM.

Install:

Add to the Zope Products Directory and restart Zope. i.e. unzip into the <Zope Install Dir>/lib/python/Products directory or where you install Zope Add-In Products.

Usage:

On starting Zope a ZSession object will be created in the root Zope Folder and called ZSession. In all methods that require Session or Application information put at the top of the method

<dtml-call ZSession>
for example in the standard_html_header. Or call ZSessionName = self.ZSession(). It only needs to be called once on the page.

This does a few things: It looks for a session cookie (by default called _ZSession, but that's something you can set). If it finds one, it checks the Session in memory. If it doesn't find a cookie, or it finds that the Session is corrupt, it will create a new Session, and then send back a cookie with the session name in it.

Next, it creates a ZSessionObj called SESSION, and puts it in the REQUEST object, that is REQUEST['SESSION'] which is just like REQUEST['RESPONSE'].

Note, you can change the name of the ZSessionObj which by default is 'SESSION' to any unique name of your choosing if the name 'SESSION' conflicts with other Session objects in Zope. Use the properties tab in the ZSession manage screens.

If applicationName is used as a parameter when calling ZSession. For example:
<dtml-var "ZSession(applicationName='MyWeb')">, it will also creates a ZApplicationObj called APPLICATION, and put it in the REQUEST object, that is REQUEST['APPLCIATION'] which is just like REQUEST['RESPONSE'].

<dtml-var "REQUEST['SESSION']">

<dtml-var "REQUEST['APPLICATION']">

This looks and acts like a dictionary, only it pulls its data out of RAM, and sets data in RAM.

Both objects support the following methods:

The Application Object has two extra methods:

The method copy()' - Is not implemented in either.

From here on inside the document, you can do things like:

  Sessions - DTML Methods:
  <dtml-var ZSession> (using dtml-call will not print the Session Name)
  <dtml-call "SESSION.set('someitem', 'somevalue')">
  <dtml-var "SESSION['someitem']">
  
  Sessions - Python:
  #Need to call ZSession to create the Session Object
  sessionName = self.ZSession()
  self.REQUEST.SESSION.set('someitem', 'somevalue')
  someitem = self.REQUEST.SESSION['someitem']
  
OR:
  Applications - DTML Methods:
  <dtml-var "ZSession(applicationName='MyWeb')"> (using dtml-call will not print the Session Name)
  <dtml-call "APPLICATION.lock()">
  <dtml-call "APPLICATION.set('someitem', 'somevalue')">
  <dtml-call "APPLICATION.unlock()">
  <dtml-var "APPLICATION['someitem']">
  
  Applications Python:
  #Need to call ZSession to create the Application Object
  sessionName = self.ZSession(applicationName='MyWeb')
  self.REQUEST.APPLICATION.lock()
  self.REQUEST.APPLICATION.set('someitem', 'somevalue')
  self.REQUEST.APPLICATION.unlock()
  someitem = self.REQUEST.APPLICATION.['someitem']
  

The manage interface allows ZSession objects to be viewed, deleted and also delete individual keys from each ZSession SESSION or APPLICATION objects.

Storing objects:

The SESSION and APPLICATION Objects can store any type of variable or object. You can store objects such as a Database Connections, Result Set objects or any value you wish. You can then reference these again on future requested pages.

Note: While you can store objects such as a Database Connections, Result Set objects or any value you wish these may be quite large and take up substantial memory (or memory loss? - this is being investigated).

A ZSessionObj is created for each request, and a ZApplicationObj is created for each request if applicationName is used as a parameter when calling ZSession. For example: <dtml-var "ZSession(applicationName='MyWeb')">

The APPLICATION must be locked to be able to add, delete, and change values. This is done by calling APPLICATION.lock() and APPLICATION.unlock().

A ZSession.log is also created in the var directory to alert of any problems. If the process which clears ZSessions is restarted it will be logged here.

Credits:

Anthony Baxter for writing SQLSession which much of ZSession is based on.

Gerard Hale.