root/software/session2/trunk/session2/store/MemorySessionStore.py

Revision 12, 2.2 kB (checked in by t, 12 years ago)

Moved VolatileSessionStore? over to MemorySessionStore? (MO)

Line 
1 from session2.store.SessionStore import SessionStore
2
3 class MemorySessionStore(SessionStore):
4     """
5     Non-persistent memory session storage, useful only for debugging.
6     DO NOT USE WITH MULTIPROCESS SERVERS (e.g., Quixote's SCGI server)!
7     """
8
9     is_multiprocess_safe = False
10     is_thread_safe = False
11    
12     def __init__(self):
13         self.data = {}
14    
15     #### Methods called by the SessionManager ####
16     def load_session(self, id, default=None):
17         """Return the session if it exists, else return 'default'."""
18
19         return self.data.get(id, default)
20
21     def save_session(self, session):
22         """Save the session in the store."""
23
24         self.data[session.id] = session
25
26     def delete_session(self, session):
27         """Delete the session in the store."""
28
29         try:
30             del self.data[session.id]
31         except KeyError:
32             pass
33
34     def has_session(self, id):
35         """Return true if the session exists in the store, else false."""
36
37         return id in self.data
38
39     #### Other methods ####
40     def iter_sessions(self):
41         """
42         Return an iterable of (id, session) for all sessions in the store.
43
44         This method is never called by the session manager; it's for admin
45         applications that want to browse the sessions.
46         """
47
48         return self.data.iteritems()
49    
50     def setup(self):
51         """
52         Initialize the session store; e.g., create required database tables.
53         If a previous store exists, overwrite it or raise an error.  The
54         default implmentation does nothing, meaning no setup is necessary.
55
56         This method is never called by the session manager; it's for your
57         application setup program.
58         """
59
60         pass
61
62     def delete_old_sessions(self, minutes):
63         """
64         Delete all sessions that have not been modified for N minutes.  The
65         default implementation does nothing, meaning the store cannot delete
66         old sessions.
67
68         This method is never called by the session manager.  It's for your
69         application maintenance program; e.g., a daily cron job.
70         """
71
72         cutoff = minutes * 60
73         for id, sess in self.data.iteritems():
74             if sess.get_access_age() > minutes:
75                 del self.data[id]
Note: See TracBrowser for help on using the browser.