-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Session: non-native session handler with locks is back!
I was sad the native handler doesn't care about locks, so I had to revert the old session handler written in PHP. The native is still default, but this pull adds new option to turn it back on redis: session: {native: off}
- Loading branch information
1 parent
40e2328
commit a7b53fa
Showing
4 changed files
with
740 additions
and
20 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
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,185 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Kdyby (http://www.kdyby.org) | ||
* | ||
* Copyright (c) 2008 Filip Procházka ([email protected]) | ||
* | ||
* For the full copyright and license information, please view the file license.md that was distributed with this source code. | ||
*/ | ||
|
||
namespace Kdyby\Redis; | ||
|
||
use Kdyby; | ||
use Nette\Diagnostics\Debugger; | ||
use Nette; | ||
|
||
|
||
|
||
/** | ||
* Redis session handler allows to store session in redis using Nette\Http\Session. | ||
* | ||
* <code> | ||
* $session->setStorage(new Kdyby\Redis\RedisSessionHandler($redisClient)); | ||
* </code> | ||
* | ||
* @author Filip Procházka <[email protected]> | ||
*/ | ||
class RedisSessionHandler extends Nette\Object implements Nette\Http\ISessionStorage | ||
{ | ||
|
||
/** @internal cache structure */ | ||
const NS_NETTE = 'Nette.Session:'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $savePath; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $ssIds = array(); | ||
|
||
/** | ||
* @var RedisClient | ||
*/ | ||
private $client; | ||
|
||
|
||
|
||
/** | ||
* @param RedisClient $redisClient | ||
*/ | ||
public function __construct(RedisClient $redisClient) | ||
{ | ||
$this->client = $redisClient; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param $savePath | ||
* @param $sessionName | ||
* | ||
* @return bool | ||
*/ | ||
public function open($savePath, $sessionName) | ||
{ | ||
$this->savePath = $savePath; | ||
return TRUE; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* | ||
* @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; | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* @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'); | ||
return FALSE; | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return bool | ||
*/ | ||
public function remove($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'); | ||
return FALSE; | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param string $id | ||
* | ||
* @return string | ||
*/ | ||
private function formatKey($id) | ||
{ | ||
return self::NS_NETTE . $id; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function close() | ||
{ | ||
foreach ($this->ssIds as $key => $yes) { | ||
$this->client->unlock($key); | ||
} | ||
$this->ssIds = array(); | ||
|
||
return TRUE; | ||
} | ||
|
||
|
||
|
||
/** | ||
* @param int $maxLifeTime | ||
* | ||
* @return bool | ||
*/ | ||
public function clean($maxLifeTime) | ||
{ | ||
return TRUE; | ||
} | ||
|
||
|
||
|
||
public function __destruct() | ||
{ | ||
$this->close(); | ||
} | ||
|
||
} |
Oops, something went wrong.