Browse Source

use redis for sessions

Andrew Dolgov 11 months ago
parent
commit
7fed864647
1 changed files with 7 additions and 43 deletions
  1. 7 43
      include/sessions.php

+ 7 - 43
include/sessions.php

@@ -48,53 +48,21 @@
 	}
 
 	function ttirc_read ($id) {
-		global $session_expire;
-
-		$dbh = DB::get();
-
-		$sth = $dbh->prepare("SELECT data FROM ttirc_sessions WHERE id = ?");
-		$sth->execute([$id]);
-
-		if ($row = $sth->fetch()) {
+		global $redis;
 
-			return base64_decode($row["data"]);
-
-		} else {
-			$expire = time() + $session_expire;
-
-			$sth = $dbh->prepare("INSERT INTO ttirc_sessions (id, data, expire)
-					VALUES (?, '', ?)");
-			$sth->execute([$id, $expire]);
-
-			return "";
-		}
+		return base64_decode($redis->get("ttirc_sessions,$id"));
 	}
 
 	function ttirc_write ($id, $data) {
-
 		global $session_expire;
+		global $redis;
 
 		if (!$data) {
 			return false;
 		}
 
-		$expire = time() + $session_expire;
-		$data = base64_encode($data);
-
-		$dbh = DB::get();
-
-		$sth = $dbh->prepare("SELECT data FROM ttirc_sessions WHERE id = ?");
-		$sth->execute([$id]);
-
-		if ($row = $sth->fetch()) {
-			$sth = $dbh->prepare("UPDATE ttirc_sessions SET data = ?, expire = ?
-				WHERE id = ?");
-			$sth->execute([$data, $expire, $id]);
-		} else {
-			$sth = $dbh->prepare("INSERT INTO ttirc_sessions (id, data, expire)
-					VALUES (?, ?, ?)");
-			$sth->execute([$id, $data, $expire]);
-		}
+		$redis->set("ttirc_sessions,$id", base64_encode($data));
+		$redis->expire("ttirc_sessions,$id", $session_expire);
 
 		return true;
 	}
@@ -104,18 +72,14 @@
 	}
 
 	function ttirc_destroy ($id) {
+		global $redis;
 
-		$dbh = DB::get();
-		$sth = $dbh->prepare("DELETE FROM ttirc_sessions WHERE id = ?");
-		$sth->execute([$id]);
+		$redis->del("ttirc_sessions,$id");
 
 		return true;
 	}
 
 	function ttirc_gc ($expire) {
-		$dbh = DB::get();
-		$dbh->query("DELETE FROM ttirc_sessions WHERE expire < " . time());
-
 		return true;
 	}