Package aquarium :: Package session :: Module SessionContainer :: Class SessionContainer
[show private | hide private]
[frames | no frames]

Class SessionContainer

Known Subclasses:
DatabaseSessionContainer

This is a container for sessions.

A container class is needed because:

On the other hand, no instance state is kept, so one instance of this class is just as good as another. All state is kept in the class. I'd use a singleton, but the version of Python I'm stuck with doesn't have class methods.

Concerning locking: in general, a global lock (of some sort) should be used so that creating, deleting, reading, and writing sessions is serialized. However, it is not necessary to have a lock for each session. If a user wishes to use two browser windows at the same time, the last writer wins.

Concerning aquarium.util.AquariumClass: this class does not subclass aquarium.util.AquariumClass and does not require a ctx parameter in its constructor. Subclasses may choose to mixin aquarium.util.AquariumClass as necessary, but they will then require a ctx in their constructors. aquarium.util.Aquarium is aware of this.

This base class is useful for mutlithreaded environments. For other environments, such as CGI, it simply provides an API.

The following class level constants are defined:

SID_LENGTH
This is the length of standard sid's.

These are needed since not all "mutexes" provide the same API, unfortunately:

_acquire
Acquire the global lock.
_release
Release the global lock.

The following protected class variables are used:

_lock
This is the global lock. It is initialized statically. (This base class uses threading.Lock. Subclasses can set this to something different if appropriate.)
_sessions
This is the global dictionary of sessions.

Method Summary
  adjustTime(self, deltaSeconds)
Adjust all of the lastModified keys.
  cleanup(self)
Delete all of the expired sessions.
  open(self, sid)
Create or open a session.

Class Variable Summary
int SID_LENGTH = 20                                                                    

Method Details

adjustTime(self, deltaSeconds)

Adjust all of the lastModified keys.

If you have to change the time, do this:

sessionContainer._acquire()
try:
    changeTime()
    sessionContainer.adjustTime(deltaSeconds)
finally:
    sessionContainer._release()

You have to manage the lock yourself, otherwise changing the time might cause some sessions to get deleted before we can update them.

cleanup(self)

Delete all of the expired sessions.

It's the application's responsibility to occasionally call this.

open(self, sid=None)

Create or open a session.

sid
If this is None, the default, or if no such session exists, a new sid will be generated (even if you provided an sid) and a new session will be created with that sid.

Return the session.


Class Variable Details

SID_LENGTH

Type:
int
Value:
20                                                                    

Generated by Epydoc 2.1 on Mon Jan 1 16:34:19 2007 http://epydoc.sf.net