-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from Kdyby/fixed-sessions
Improve session handling
- Loading branch information
Showing
10 changed files
with
523 additions
and
72 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 |
---|---|---|
|
@@ -6,7 +6,6 @@ env: | |
- NETTE=nette-2.2 | ||
|
||
php: | ||
- 5.3.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
|
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
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
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
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 |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
* | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class RedisSessionHandler extends Nette\Object implements Nette\Http\ISessionStorage | ||
class RedisSessionHandler extends Nette\Object implements \SessionHandlerInterface | ||
{ | ||
|
||
/** @internal cache structure */ | ||
|
@@ -41,6 +41,11 @@ class RedisSessionHandler extends Nette\Object implements Nette\Http\ISessionSto | |
*/ | ||
private $client; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
private $ttl; | ||
|
||
|
||
|
||
/** | ||
|
@@ -49,61 +54,62 @@ class RedisSessionHandler extends Nette\Object implements Nette\Http\ISessionSto | |
public function __construct(RedisClient $redisClient) | ||
{ | ||
$this->client = $redisClient; | ||
$this->ttl = ini_get("session.gc_maxlifetime"); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param $savePath | ||
* @param $sessionName | ||
* | ||
* @param int $ttl | ||
*/ | ||
public function setTtl($ttl) | ||
{ | ||
$this->ttl = max($ttl, 0); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $savePath | ||
* @param string $sessionName | ||
* @return bool | ||
*/ | ||
public function open($savePath, $sessionName) | ||
{ | ||
return TRUE; | ||
$id = &$_COOKIE[$sessionName]; // prevent notice | ||
if (!is_string($id) || !preg_match('#^[0-9a-zA-Z,-]{22,128}\z#i', $id)) { | ||
return FALSE; // Wtf? | ||
} | ||
|
||
return (bool) $this->lock($id); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* | ||
* @throws SessionHandlerException | ||
* @return string | ||
*/ | ||
public function read($id) | ||
{ | ||
try { | ||
$key = $this->formatKey($id); | ||
$this->ssIds[$key] = $this->client->lock($key); | ||
return (string) $this->client->get($key); | ||
|
||
} catch (Exception $e) { | ||
Debugger::log($e, 'redis-session'); | ||
return FALSE; | ||
} | ||
return (string) $this->client->get($this->lock($id)); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* @param string $data | ||
* | ||
* @return bool | ||
*/ | ||
public function write($id, $data) | ||
{ | ||
try { | ||
$key = $this->formatKey($id); | ||
$this->ssIds[$key] = $this->client->lock($key); | ||
$this->client->setex($key, ini_get("session.gc_maxlifetime"), $data); | ||
return TRUE; | ||
|
||
} catch (Exception $e) { | ||
Debugger::log($e, 'redis-session'); | ||
if (!isset($this->ssIds[$id])) { | ||
return FALSE; | ||
} | ||
|
||
return $this->client->setex($this->formatKey($id), $this->ttl, $data); | ||
} | ||
|
||
|
||
|
@@ -113,33 +119,19 @@ public function write($id, $data) | |
* | ||
* @return bool | ||
*/ | ||
public function remove($id) | ||
public function destroy($id) | ||
{ | ||
try { | ||
$key = $this->formatKey($id); | ||
$this->client->multi(function (RedisClient $client) use ($key) { | ||
$client->del($key); | ||
$client->unlock($key); | ||
}); | ||
unset($this->ssIds[$key]); | ||
return TRUE; | ||
|
||
} catch (Exception $e) { | ||
Debugger::log($e, 'redis-session'); | ||
if (!isset($this->ssIds[$id])) { | ||
return FALSE; | ||
} | ||
} | ||
|
||
$key = $this->formatKey($id); | ||
$this->client->multi(function (RedisClient $client) use ($key) { | ||
$client->del($key); | ||
$client->unlock($key); | ||
}); | ||
|
||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return string | ||
*/ | ||
private function formatKey($id) | ||
{ | ||
return self::NS_NETTE . $id; | ||
return TRUE; | ||
} | ||
|
||
|
||
|
@@ -149,7 +141,7 @@ private function formatKey($id) | |
*/ | ||
public function close() | ||
{ | ||
foreach ($this->ssIds as $key => $yes) { | ||
foreach ($this->ssIds as $id => $key) { | ||
$this->client->unlock($key); | ||
} | ||
$this->ssIds = array(); | ||
|
@@ -164,13 +156,45 @@ public function close() | |
* | ||
* @return bool | ||
*/ | ||
public function clean($maxLifeTime) | ||
public function gc($maxLifeTime) | ||
{ | ||
return TRUE; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* @return string | ||
*/ | ||
protected function lock($id) | ||
{ | ||
try { | ||
$key = $this->formatKey($id); | ||
$this->client->lock($key); | ||
$this->ssIds[$id] = $key; | ||
|
||
return $key; | ||
|
||
} catch (LockException $e) { | ||
throw new SessionHandlerException(sprintf('Cannot work with non-locked session id %s: %s', $id, $e->getMessage()), 0, $e); | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return string | ||
*/ | ||
private function formatKey($id) | ||
{ | ||
return self::NS_NETTE . $id; | ||
} | ||
|
||
|
||
|
||
public function __destruct() | ||
{ | ||
$this->close(); | ||
|
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 |
---|---|---|
|
@@ -52,6 +52,16 @@ class ConnectionException extends \RuntimeException implements Exception | |
|
||
|
||
|
||
/** | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class SessionHandlerException extends \RuntimeException implements Exception | ||
{ | ||
|
||
} | ||
|
||
|
||
|
||
/** | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
|
Oops, something went wrong.