Mercurial > libervia-backend
comparison src/memory/memory.py @ 1215:d9c399ec5dd9
memory: session timeout won't be reset on each access if the Sessions() is called with resettable_timeout=False
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 22 Sep 2014 19:24:07 +0200 |
parents | ed3b01ed70d7 |
children | 5e5661ab5c81 |
comparison
equal
deleted
inserted
replaced
1214:ed3b01ed70d7 | 1215:d9c399ec5dd9 |
---|---|
38 | 38 |
39 class Sessions(object): | 39 class Sessions(object): |
40 """Sessions are data associated to key used for a temporary moment, with optional profile checking.""" | 40 """Sessions are data associated to key used for a temporary moment, with optional profile checking.""" |
41 DEFAULT_TIMEOUT = 600 | 41 DEFAULT_TIMEOUT = 600 |
42 | 42 |
43 def __init__(self, timeout=None): | 43 def __init__(self, timeout=None, resettable_timeout=True): |
44 """ | 44 """ |
45 @param timeout: nb of seconds before session destruction | 45 @param timeout (int): nb of seconds before session destruction |
46 @param resettable_timeout (bool): if True, the timeout is reset on each access | |
46 """ | 47 """ |
47 self._sessions = dict() | 48 self._sessions = dict() |
48 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT | 49 self.timeout = timeout or Sessions.DEFAULT_TIMEOUT |
50 self.resettable_timeout = resettable_timeout | |
49 | 51 |
50 def newSession(self, session_data=None, session_id=None, profile=None): | 52 def newSession(self, session_data=None, session_id=None, profile=None): |
51 """ Create a new session | 53 """ Create a new session |
52 @param session_data: mutable data to use, default to a dict | 54 @param session_data: mutable data to use, default to a dict |
53 @param session_id (str): force the session_id to the given string | 55 @param session_id (str): force the session_id to the given string |
81 timer, session_data, profile_set = self._sessions[session_id] | 83 timer, session_data, profile_set = self._sessions[session_id] |
82 except ValueError: | 84 except ValueError: |
83 raise exceptions.InternalError("You need to use __getitem__ when profile is not set") | 85 raise exceptions.InternalError("You need to use __getitem__ when profile is not set") |
84 if profile_set != profile: | 86 if profile_set != profile: |
85 raise exceptions.InternalError("current profile differ from set profile !") | 87 raise exceptions.InternalError("current profile differ from set profile !") |
86 timer.reset(self.timeout) | 88 if self.resettable_timeout: |
89 timer.reset(self.timeout) | |
87 return session_data | 90 return session_data |
88 | 91 |
89 def __getitem__(self, session_id): | 92 def __getitem__(self, session_id): |
90 try: | 93 try: |
91 timer, session_data = self._sessions[session_id] | 94 timer, session_data = self._sessions[session_id] |
92 except ValueError: | 95 except ValueError: |
93 raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set") | 96 raise exceptions.InternalError("You need to use profileGet instead of __getitem__ when profile is set") |
94 timer.reset(self.timeout) | 97 if self.resettable_timeout: |
98 timer.reset(self.timeout) | |
95 return session_data | 99 return session_data |
96 | 100 |
97 def __setitem__(self, key, value): | 101 def __setitem__(self, key, value): |
98 raise NotImplementedError("You need do use newSession to create a session") | 102 raise NotImplementedError("You need do use newSession to create a session") |
99 | 103 |
144 - raise an error if more than one session are found | 148 - raise an error if more than one session are found |
145 """ | 149 """ |
146 ids = self._profileGetAllIds(profile) | 150 ids = self._profileGetAllIds(profile) |
147 if len(ids) > 1: | 151 if len(ids) > 1: |
148 raise exceptions.InternalError('profileGetUnique has been used but more than one session has been found!') | 152 raise exceptions.InternalError('profileGetUnique has been used but more than one session has been found!') |
149 return self.profileGet(ids[0], profile) if len(ids) == 1 else None | 153 return self.profileGet(ids[0], profile) if len(ids) == 1 else None # XXX: timeout might be reset |
150 | 154 |
151 | 155 |
152 def profileDelUnique(self, profile): | 156 def profileDelUnique(self, profile): |
153 """Delete the unique session that is associated to the given profile. | 157 """Delete the unique session that is associated to the given profile. |
154 | 158 |