]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
a memcached_dataobject class for saving sessions
authorEvan Prodromou <evan@controlyourself.ca>
Sat, 27 Jun 2009 12:48:22 +0000 (05:48 -0700)
committerEvan Prodromou <evan@controlyourself.ca>
Sat, 27 Jun 2009 12:48:22 +0000 (05:48 -0700)
classes/Session.php
lib/util.php

index 9b48dabac7bdc28b275b79f739b708712da795e0..6f13c7d2732a1b40e092c0d88b0855664b1d6e95 100755 (executable)
@@ -39,4 +39,71 @@ class Session extends Memcached_DataObject
 
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
+
+    static function open($save_path, $session_name)
+    {
+        return true;
+    }
+
+    static function close()
+    {
+        return true;
+    }
+
+    static function read($id)
+    {
+        $session = Session::staticGet('id', $id);
+
+        if (empty($session)) {
+            return '';
+        } else {
+            return (string)$session->session_data;
+        }
+    }
+
+    static function write($id, $session_data)
+    {
+        $session = Session::staticGet('id', $id);
+
+        if (empty($session)) {
+            $session = new Session();
+
+            $session->id           = $id;
+            $session->session_data = $session_data;
+            $session->created      = common_sql_now();
+
+            return $session->insert();
+        } else {
+            $session->session_data = $session_data;
+
+            return $session->update();
+        }
+    }
+
+    static function destroy($id)
+    {
+        $session = Session::staticGet('id', $id);
+
+        if (!empty($session)) {
+            return $session->delete();
+        }
+    }
+
+    static function gc($maxlifetime)
+    {
+        $epoch = time() - $maxlifetime;
+
+        $qry = 'DELETE FROM session ' .
+          'WHERE modified < "'.$epoch.'"';
+
+        $session = new Session();
+
+        $session->query($qry);
+    }
+
+    static function setSaveHandler()
+    {
+        session_set_save_handler('Session::open', 'Session::close', 'Session::read',
+                                 'Session::write', 'Session::destroy', 'Session::gc');
+    }
 }
index e5a8eaea06250438a0299256507a77988793c860..b3496a09e81ee6e9efa805eb208e1e6add40a5cd 100644 (file)
@@ -828,7 +828,12 @@ function common_date_iso8601($dt)
 
 function common_sql_now()
 {
-    return strftime('%Y-%m-%d %H:%M:%S', time());
+    return common_sql_date(time());
+}
+
+function common_sql_date($datetime)
+{
+    return strftime('%Y-%m-%d %H:%M:%S', $datetime);
 }
 
 function common_redirect($url, $code=307)