From c5c43c85d3c8ca8cf158f20a2433984872e54ce4 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 5 Aug 2021 14:48:23 +0200 Subject: Use sqlite in WAL mode. --- connect-better-sqlite3.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'connect-better-sqlite3.js') diff --git a/connect-better-sqlite3.js b/connect-better-sqlite3.js index 2bfdb12..de858ed 100644 --- a/connect-better-sqlite3.js +++ b/connect-better-sqlite3.js @@ -29,14 +29,17 @@ module.exports = function (session) { db_path = (options.dir || '.') + '/' + db_path; let db = new SQLite(db_path, options.mode); + db.pragma("journal_mode = WAL"); + db.pragma("synchronous = NORMAL"); db.exec("CREATE TABLE IF NOT EXISTS "+table+" (sid PRIMARY KEY, expires INTEGER, sess TEXT)"); db.exec("DELETE FROM "+table+" WHERE "+now()+" > expires"); db.exec("VACUUM"); + db.exec("PRAGMA wal_checkpoint(TRUNCATE)"); this.sql_destroy = db.prepare("DELETE FROM "+table+" WHERE sid = ?"); this.sql_get = db.prepare("SELECT sess FROM "+table+" WHERE sid = ? AND ? <= expires"); this.sql_set = db.prepare("INSERT OR REPLACE INTO "+table+" VALUES (?,?,?)"); - this.sql_touch = db.prepare("UPDATE "+table+" SET expires = ? WHERE sid = ? AND ? <= expires"); + this.sql_touch = db.prepare("UPDATE "+table+" SET expires = ? WHERE sid = ? AND expires < ?"); } destroy(sid, cb = noop) { @@ -77,7 +80,8 @@ module.exports = function (session) { try { if (sess && sess.cookie && sess.cookie.expires) { let expires = seconds(sess.cookie.expires); - this.sql_touch.run(expires, sid, now()); + let limit = expires - 3600; + this.sql_touch.run(expires, sid, limit); cb(null); } else { cb(null); -- cgit v1.2.3