diff options
Diffstat (limited to 'src/jwebmail/read_mails.py')
-rw-r--r-- | src/jwebmail/read_mails.py | 51 |
1 files changed, 18 insertions, 33 deletions
diff --git a/src/jwebmail/read_mails.py b/src/jwebmail/read_mails.py index cae04b1..324b4d0 100644 --- a/src/jwebmail/read_mails.py +++ b/src/jwebmail/read_mails.py @@ -1,5 +1,4 @@ from contextlib import closing -from datetime import datetime, timedelta from flask import current_app, g from flask_login import UserMixin, current_user @@ -53,31 +52,25 @@ class MysqlTimeoutSession: ) def set(self, key, value): - timeout = datetime.now() + timedelta(seconds=self.timeout) - with closing(self.conn.cursor()) as cur: - cur.execute("DELETE FROM session WHERE user = %s", [key]) cur.execute( - "INSERT INTO session VALUES (%s, %s, %s)", [key, value, timeout] + "REPLACE INTO session VALUES (%s, %s, now() + INTERVAL %s SECOND)", + [key, value, self.timeout], ) self.conn.commit() def get(self, key): with closing(self.conn.cursor()) as cur: - cur.execute("DELETE FROM session WHERE timeout < NOW()") + cur.execute("DELETE FROM session WHERE timeout < now()") + cur.execute( + "UPDATE session SET timeout = now() + INTERVAL %s SECOND WHERE user = %s", + [self.timeout, key], + ) cur.execute("SELECT password FROM session WHERE user = %s", [key]) row = cur.fetchone() + self.conn.commit() - if row is None: - self.conn.commit() - return None - else: - timeout = datetime.now() + timedelta(seconds=self.timeout) - cur.execute( - "UPDATE session SET timeout = %s WHERE user = %s", [timeout, key] - ) - self.conn.commit() - return row[0] + return None if row is None else row[0] def close(self): self.conn.close() @@ -89,7 +82,7 @@ class SqliteTimeoutSession: self.timeout = timeout - self.conn = sqlite3.connect(database) + self.conn = sqlite3.connect(database, isolation_level="IMMEDIATE") cur = self.conn.cursor() cur.execute( "CREATE TABLE IF NOT EXISTS session (user text PRIMARY KEY, password text, timeout integer NOT NULL) STRICT" @@ -97,32 +90,24 @@ class SqliteTimeoutSession: cur.execute("CREATE INDEX IF NOT EXISTS timeout_idx ON session (timeout)") def set(self, key, value): - timeout = datetime.now() + timedelta(seconds=self.timeout) - with closing(self.conn.cursor()) as cur: cur.execute( - "REPLACE INTO session VALUES (?, ?, unixepoch(?))", - [key, value, timeout], + "REPLACE INTO session VALUES (?, ?, unixepoch('now', format('%d seconds', ?)))", + [key, value, self.timeout], ) self.conn.commit() def get(self, key): with closing(self.conn.cursor()) as cur: cur.execute("DELETE FROM session WHERE timeout < unixepoch()") - cur.execute("SELECT password FROM session WHERE user = ?", [key]) + cur.execute( + "UPDATE session SET timeout = unixepoch('now', format('%d seconds', ?)) WHERE user = ? RETURNING password", + [self.timeout, key], + ) row = cur.fetchone() + self.conn.commit() - if row is None: - self.conn.commit() - return None - else: - timeout = datetime.now() + timedelta(seconds=self.timeout) - cur.execute( - "UPDATE session SET timeout = unixepoch(?) WHERE user = ?", - [timeout, key], - ) - self.conn.commit() - return row[0] + return None if row is None else row[0] def close(self): self.conn.close() |