-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSessionMemoryStore.py
More file actions
107 lines (82 loc) · 3.3 KB
/
Copy pathSessionMemoryStore.py
File metadata and controls
107 lines (82 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""Session store in memory."""
from MiscUtils import NoDefault
from SessionStore import SessionStore
from SessionFileStore import SessionFileStore
class SessionMemoryStore(SessionStore):
"""Stores the session in memory as a dictionary.
This is fast and secure when you have one, persistent application instance.
"""
# region Init
def __init__(self, app, restoreFiles=None):
"""Initialize the session memory store.
If restoreFiles is true, and sessions have been saved to file,
the store will be initialized from these files.
"""
SessionStore.__init__(self, app)
self._store = {}
if restoreFiles is None:
restoreFiles = self._retain
if restoreFiles:
fileStore = SessionFileStore(app)
for key in fileStore:
try:
self[key] = fileStore[key]
except Exception:
app.handleException()
fileStore.clear()
self._restoreFiles = restoreFiles
# endregion Init
# region Access
def __len__(self):
"""Return the number of sessions in the store."""
return len(self._store)
def __getitem__(self, key):
"""Get a session item from the store."""
return self._store[key]
def __setitem__(self, key, value):
"""Set a session item, saving it to the store."""
value.setDirty(False)
self._store[key] = value
def __delitem__(self, key):
"""Delete a session item from the store."""
session = self[key]
if not session.isExpired():
session.expiring()
del self._store[key]
def __contains__(self, key):
"""Check whether the session store has a given key."""
return key in self._store
def __iter__(self):
"""Return an iterator over the stored session keys."""
return iter(self._store)
def keys(self):
"""Return a list with the keys of all the stored sessions."""
return list(self._store)
def clear(self):
"""Clear the session store, removing all of its items."""
self._store.clear()
def setdefault(self, key, default=None):
"""Return value if key available, else default (also setting it)."""
# note that setdefault() is atomic, so no locking is needed
return self._store.setdefault(key, default)
def pop(self, key, default=NoDefault):
"""Return value if key available, else default (also remove key)."""
# note that pop() is atomic, so no locking is needed
if default is NoDefault:
return self._store.pop(key)
return self._store.pop(key, default)
# endregion Access
# region Application support
def storeSession(self, session):
"""Save already potentially changed session in the store."""
if self._alwaysSave or session.isDirty():
key = session.identifier()
if key not in self or self[key] is not session:
self[key] = session
def storeAllSessions(self):
"""Permanently save all sessions in the store."""
if self._restoreFiles:
fileStore = SessionFileStore(self._app)
for key, session in list(self.items()):
fileStore[key] = session
# endregion Application support