def _delete_session(self, sess_id, redis_client): session_key = self.session_key_prefix + sess_id #Delete the session if not redis_client.delete(session_key): raise SessionErrors.SessionNotFoundError("Session not found: " + sess_id)
def modify_session(self, sess_id, sess_obj): if self._serialize: sess_obj = self._serializer.serialize(sess_obj) lock, sessions = self._get_lock_and_sessions(sess_id) with lock: if not sess_id in sessions: raise SessionErrors.SessionNotFoundError( "Session not found: " + sess_id) sessions[sess_id] = SessionObj(sess_obj)
def get_session_obj(self, session_id): lock, sessions = self._get_lock_and_sessions(session_id) with lock: if not session_id in sessions: raise SessionErrors.SessionNotFoundError( "Session not found: " + session_id) session = sessions[session_id] return session
def delete_session(self, session_id): lock, sessions = self._get_lock_and_sessions(session_id) session_locks = self._get_session_lock(session_id) with lock: if session_id in sessions: sessions.pop(session_id) session_locks.pop(session_id) else: raise SessionErrors.SessionNotFoundError( "Session not found: " + session_id)
def get_session_locking(self, session_id): client = self._client_creator() self._lock(client, session_id) try: session = self._get_session(session_id, client) except SessionErrors.SessionNotFoundError: #Unlock session client.hdel(self.session_lock_key, session_id) raise SessionErrors.SessionNotFoundError("Session not found: " + session_id) return session
def _modify_session(self, sess_id, sess_obj, redis_client): session_key = self.session_key_prefix + sess_id #insert session again and restart the expiration pickled_session = self._serializer.serialize(sess_obj) if not redis_client.exists(session_key): raise SessionErrors.SessionNotFoundError("Session not found: " + sess_id) #delete session redis_client.set(session_key, pickled_session) redis_client.expire(session_key, self.timeout)
def _get_session(self, session_id, redis_client): session_key = self.session_key_prefix + session_id #get the session pickled_session = redis_client.get(session_key) if pickled_session is None: raise SessionErrors.SessionNotFoundError("Session not found: " + session_id) #reset the expiration redis_client.expire(session_key, self.timeout) #Unpickle and return the session return self._serializer.deserialize(pickled_session)
def get_session(self, session_id): session = self._session_maker() try: result = session.query( DbData.Session).filter_by(session_pool_id=self.session_pool_id, sess_id=session_id).first() if result is None: raise SessionErrors.SessionNotFoundError( "Session not found: " + session_id) session_object = result.session_obj result.latest_access = datetime.datetime.now() session.commit() finally: session.close() pickled_sess_obj = str(session_object) return self._serializer.deserialize(pickled_sess_obj)
def delete_session(self, sess_id): session = self._session_maker() try: result = session.query(DbData.Session).filter_by( session_pool_id=self.session_pool_id, sess_id=sess_id).first() if result is None: raise SessionErrors.SessionNotFoundError( "Session not found: %s" % sess_id) session.delete(result) session.commit() except SessionErrors.SessionNotFoundError: raise except Exception as e: raise SessionErrors.SessionDatabaseExecutionError( "Database exception retrieving session: %s" % e, e)
def modify_session(self, sess_id, sess_obj): serialized_sess_obj = self._serializer.serialize(sess_obj) session = self._session_maker() try: result = session.query(DbData.Session).filter_by( session_pool_id=self.session_pool_id, sess_id=sess_id).first() if result is None: raise SessionErrors.SessionNotFoundError( "Session not found: %s" % sess_id) result.session_obj = serialized_sess_obj result.latest_access = datetime.datetime.now() result.latest_change = result.latest_access session.commit() finally: session.close()
def _get_lock_and_sessions(self, session_id): if len(session_id) == 0: raise SessionErrors.SessionNotFoundError("Empty session ('')") return self._sessions[session_id[:1]]