|
|
Documentation - Modules
3.8.6. Session
Sessions allow information to be efficiently passed from one user request to
the next via some browser mechanism: get, post or cookie. Potentially large or
sensitive information is stored at the server, and only a short identifier is
sent to the client to be returned on callback. Sessions are often used to
create sequences of stateful pages that represent an application or work-flow.
This module automates sessioning for a Spyce web site. It emulates
a dictionary specific to each user (really each browser) accessing
your web site. You simply use session as if it were a dictionary
variable, and its contents automatically change depending upon the
user calling the page. For example:
examples/session2.spy
|
[[.import name=session ]]
[[\
session['visited'] = session.get('visited', 0) + 1
]]
<spy:parent title="Session example" />
You visited us [[= session['visited'] ]] times.
|
Run this code
|
In the example above, the 'visited' key would now be valid for
all pages on your site, until the session expires.
Global session options
These options are configured only in the Spyce config file:
- session_store: declares the backing storage for session
information. It should be either session.DbmStore(path) or session.MemoryStore(). In-memory sessions storage is
faster, but volatile, and does not work in multi-process server
configurations. Advanced users are welcome to create their own storage
manager, by subclassing session.SessionStore.
Advanced users can create their own storage manager by subclassing
session.SessionStore.
Per-session options
These options are set in the Spyce config file, but may be overridden
at module-import time on a per-page basis:
- session_path: the default path to attach the session to.
This refers to
cookie semantics. For example, if the path is /myapp, the session
will only be valid under /myapp pages (and below). The default is
'/', which means the session is valid site-wide.
- session_expire: the number of seconds the session is good for.
The default is one day.
You should clean up expired session state periodically. The easiest way
is to schedule session.clean_store every day or so in your config file:
import session, scheduler
scheduler.schedule_daily(0, 0, session.clean_store)
(Note: for backwards compatibility, there is also a module called "session1."
New code should simply use the module described here.)
|