소스 검색

implement session updateTimestamp and refresh session expire time api

Signed-off-by: lvshuang <lvshuang1201@gmail.com>
lvshuang 4 년 전
부모
커밋
1c3df6da1e

+ 12 - 1
Protocols/Http/Session.php

@@ -13,6 +13,7 @@
  */
 namespace Workerman\Protocols\Http;
 
+use Workerman\Protocols\Http\Session\SessionHandlerInterface;
 
 /**
  * Class Session
@@ -58,7 +59,7 @@ class Session
     /**
      * Session handler instance.
      *
-     * @var \SessionHandlerInterface
+     * @var SessionHandlerInterface
      */
     protected static $_handler = null;
 
@@ -258,6 +259,16 @@ class Session
     }
 
     /**
+     * Refresh session expire time.
+     * 
+     * @return bool
+     */
+    public function refresh()
+    {
+        static::$_handler->updateTimestamp($this->getId());
+    }
+
+    /**
      * Init.
      *
      * @return void

+ 25 - 1
Protocols/Http/Session/FileSessionHandler.php

@@ -17,7 +17,7 @@ namespace Workerman\Protocols\Http\Session;
  * Class FileSessionHandler
  * @package Workerman\Protocols\Http\Session
  */
-class FileSessionHandler implements \SessionHandlerInterface
+class FileSessionHandler implements SessionHandlerInterface
 {
     /**
      * Session save path.
@@ -89,6 +89,30 @@ class FileSessionHandler implements \SessionHandlerInterface
     }
 
     /**
+     * Update sesstion modify time.
+     * 
+     * @see https://www.php.net/manual/en/class.sessionupdatetimestamphandlerinterface.php
+     * @see https://www.php.net/manual/zh/function.touch.php
+     * 
+     * @param string $id Session id.
+     * @param string $data Session Data.
+     * 
+     * @return bool
+     */
+    public function updateTimestamp($id, $data = "")
+    {
+        $session_file = static::sessionFile($id);
+        if (!file_exists($session_file)) {
+            return false;
+        }
+        // set file modify time to current time
+        $set_modify_time = \touch($session_file);
+        // clear file stat cache
+        \clearstatcache();
+        return $set_modify_time;
+    }
+
+    /**
      * {@inheritdoc}
      */
     public function close()

+ 9 - 1
Protocols/Http/Session/RedisSessionHandler.php

@@ -17,7 +17,7 @@ namespace Workerman\Protocols\Http\Session;
  * Class RedisSessionHandler
  * @package Workerman\Protocols\Http\Session
  */
-class RedisSessionHandler extends \SessionHandler
+class RedisSessionHandler extends \SessionHandler implements SessionHandlerInterface
 {
 
     /**
@@ -95,6 +95,14 @@ class RedisSessionHandler extends \SessionHandler
     /**
      * {@inheritdoc}
      */
+    public function updateTimestamp($id, $data = "")
+    {
+        return true === $this->_redis->expire($id, $this->_maxLifeTime);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
     public function destroy($session_id)
     {
         $this->_redis->del($session_id);

+ 33 - 0
Protocols/Http/Session/SessionHandlerInterface.php

@@ -0,0 +1,33 @@
+<?php
+/**
+ * This file is part of workerman.
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the MIT-LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @author    walkor<walkor@workerman.net>
+ * @copyright walkor<walkor@workerman.net>
+ * @link      http://www.workerman.net/
+ * @license   http://www.opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Workerman\Protocols\Http\Session;
+
+use \SessionHandlerInterface as InternalSessionHandlerInterface;
+
+interface SessionHandlerInterface extends InternalSessionHandlerInterface
+{
+
+    /**
+     * Update sesstion modify time.
+     * 
+     * @see https://www.php.net/manual/en/class.sessionupdatetimestamphandlerinterface.php
+     * 
+     * @param string $id Session id.
+     * @param string $data Session Data.
+     * 
+     * @return bool
+     */
+    public function updateTimestamp($id, $data = "");
+
+}