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.
  _acquire(self)
Acquire the global lock.
  _createSession(self, sid)
This is a session factory method.
  _createSid(self)
Generate a new sid.
  _exists(self, sid)
Does a session with the given sid exist?
  _isExpired(self, sid)
Is the session with the given sid expired?
  _release(self)
Release the global lock.
  _unlink(self, sid)
Delete a session permanently.

Class Variable Summary
int SID_LENGTH = 20                                                                    
lock _lock = <thread.lock object at 0xb7df80a0>
dict _sessions = {}

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.

_acquire(self)

Acquire the global lock.

_createSession(self, sid)

This is a session factory method.

It's here so that you can override what type of session gets created without too much trouble. You must acquire the lock before calling this.

_createSid(self)

Generate a new sid.

It is not guaranteed to be unique.

_exists(self, sid)

Does a session with the given sid exist?

This implies that it is not expired. You must acquire the lock before calling this.

_isExpired(self, sid)

Is the session with the given sid expired?

If the session doesn't exist, this will raise a KeyError. You must acquire the lock before calling this.

_release(self)

Release the global lock.

_unlink(self, sid)

Delete a session permanently.

You must acquire the lock before calling this.


Class Variable Details

SID_LENGTH

Type:
int
Value:
20                                                                    

_lock

Type:
lock
Value:
<thread.lock object at 0xb7df80a0>                                     

_sessions

Type:
dict
Value:
{}                                                                     

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