-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:swoft-cloud/swoft-component
- Loading branch information
Showing
6 changed files
with
80 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,40 +9,35 @@ | |
|
||
|
||
/** | ||
* Class FileSessionHandler | ||
* | ||
* @Bean() | ||
* @uses FileSessionHandler | ||
* @version 2017年12月05日 | ||
* @author huangzhhui <[email protected]> | ||
* @copyright Copyright 2010-2017 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
* @package Swoft\Session\Handler | ||
*/ | ||
class FileSessionHandler implements \SessionHandlerInterface | ||
class FileSessionHandler implements \SessionHandlerInterface, LifetimeInterface | ||
{ | ||
|
||
use LifetimeTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $minutes; | ||
|
||
/** | ||
* FileSessionHandler constructor. | ||
* | ||
* @param string $path | ||
* @param int $minutes | ||
* @param int $lifetime | ||
*/ | ||
public function __construct($path = null, $minutes = 15) | ||
public function __construct($path = null, $lifetime = 15 * 60) | ||
{ | ||
if (null !== $path) { | ||
$this->path = $path; | ||
} else { | ||
$this->path = App::getAlias('@runtime/sessions'); | ||
} | ||
$this->minutes = $minutes; | ||
$this->setLifetime($lifetime); | ||
} | ||
|
||
/** | ||
|
@@ -85,7 +80,7 @@ public function read($sessionId) | |
{ | ||
$path = $this->path . '/' . $sessionId; | ||
if (file_exists($path)) { | ||
if (filemtime($path) >= Carbon::now()->subMinutes($this->minutes)->getTimestamp()) { | ||
if (filemtime($path) >= Carbon::now()->subSeconds($this->getLifetime())->getTimestamp()) { | ||
return file_get_contents($path); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Swoft\Session\Handler; | ||
|
||
|
||
interface LifetimeInterface | ||
{ | ||
|
||
/** | ||
* @param int $seconds | ||
* @return $this | ||
*/ | ||
public function setLifetime(int $seconds); | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getLifetime(): int; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Swoft\Session\Handler; | ||
|
||
|
||
trait LifetimeTrait | ||
{ | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $lifetime = 0; | ||
|
||
/** | ||
* @param int $seconds | ||
* @return $this | ||
*/ | ||
public function setLifetime(int $seconds) | ||
{ | ||
$this->lifetime = $seconds; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getLifetime(): int | ||
{ | ||
return $this->lifetime; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,9 @@ | |
use Swoft\Http\Message\Server\Request; | ||
|
||
/** | ||
* @uses NeedRequestInterface | ||
* @version 2018年02月01日 | ||
* @author huangzhhui <[email protected]> | ||
* @copyright Copyright 2010-2018 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
* Interface NeedRequestInterface | ||
* | ||
* @package Swoft\Session\Handler | ||
*/ | ||
interface NeedRequestInterface | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,16 +7,16 @@ | |
|
||
|
||
/** | ||
* Class RedisSessionHandler | ||
* | ||
* @Bean() | ||
* @uses RedisSessionHandler | ||
* @version 2017年12月05日 | ||
* @author huangzhhui <[email protected]> | ||
* @copyright Copyright 2010-2017 Swoft software | ||
* @license PHP Version 7.x {@link http://www.php.net/license/3_0.txt} | ||
* @package Swoft\Session\Handler | ||
*/ | ||
class RedisSessionHandler implements \SessionHandlerInterface | ||
class RedisSessionHandler implements \SessionHandlerInterface, LifetimeInterface | ||
{ | ||
|
||
use LifetimeTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
|
@@ -27,11 +27,6 @@ class RedisSessionHandler implements \SessionHandlerInterface | |
*/ | ||
private $glue = ':'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $minutes; | ||
|
||
/** | ||
* @Inject() | ||
* @var \Swoft\Redis\Redis | ||
|
@@ -41,11 +36,11 @@ class RedisSessionHandler implements \SessionHandlerInterface | |
/** | ||
* RedisSessionHandler constructor. | ||
* | ||
* @param int $minutes | ||
* @param int $lifetime | ||
*/ | ||
public function __construct($minutes = 15) | ||
public function __construct($lifetime = 15 * 60) | ||
{ | ||
$this->minutes = $minutes; | ||
$this->setLifetime($lifetime); | ||
} | ||
|
||
/** | ||
|
@@ -94,7 +89,7 @@ public function read($sessionId) | |
*/ | ||
public function write($sessionId, $data) | ||
{ | ||
return (bool)$this->redis->set($this->key($sessionId), $this->serialize($data)); | ||
return (bool)$this->redis->set($this->key($sessionId), $this->serialize($data), $this->getLifetime()); | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters