-
Notifications
You must be signed in to change notification settings - Fork 71
Add RedisSharedClient that can share one connected driver for more clients #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of the Kdyby (http://www.kdyby.org) | ||
| * | ||
| * Copyright (c) 2008 Filip Procházka (filip@prochazka.su) | ||
| * | ||
| * For the full copyright and license information, please view the file license.txt that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Kdyby\Redis; | ||
|
|
||
| use Kdyby; | ||
| use Nette; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * @author Filip Procházka <filip@prochazka.su> | ||
| */ | ||
| class ConnectionPool extends Nette\Object | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like the name very much, because it's not accurate. I don't have an idea for a better one through...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, it is not pool and it is not for connections... What about something like DriverSharer or similar? |
||
| { | ||
|
|
||
| /** | ||
| * @var IRedisDriver[] | ||
| */ | ||
| private $connections = array(); | ||
|
|
||
|
|
||
| /** | ||
| * Add new connection to pool | ||
| * @param string $host | ||
| * @param int $port | ||
| * @param IRedisDriver $connection | ||
| * @throws ConnectionAlreadyInPoolException | ||
| */ | ||
| public function addConnection($host, $port, IRedisDriver $connection) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A lot of people also use sockets
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, that this will work also with sockets. You pass host = '/tmp/redis.sock' and port = NULL. |
||
| { | ||
| $key = $this->getKey($host, $port); | ||
|
|
||
| if (isset($this->connections[$key])) { | ||
| throw new ConnectionAlreadyInPoolException; | ||
| } | ||
|
|
||
| $this->connections[$key] = $connection; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * Get connection from pool | ||
| * @param string $host | ||
| * @param int $port | ||
| * @return Driver\PhpRedisDriver | ||
| */ | ||
| public function getConnection($host, $port) | ||
| { | ||
| $key = $this->getKey($host, $port); | ||
|
|
||
| if (!isset($this->connections[$key])) { | ||
| return NULL; | ||
| } | ||
|
|
||
| return $this->connections[$key]; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| private function getKey($host, $port) | ||
| { | ||
| return strtolower($host) . ':' . $port; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * This file is part of the Kdyby (http://www.kdyby.org) | ||
| * | ||
| * Copyright (c) 2008 Filip Procházka (filip@prochazka.su) | ||
| * | ||
| * 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; | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * @author Jakub Trmota <jakub@trmota.cz> | ||
| */ | ||
| class RedisSharedClient extends RedisClient | ||
| { | ||
|
|
||
| /** | ||
| * @var ConnectionPool | ||
| */ | ||
| private $connectionPool; | ||
|
|
||
|
|
||
| /** | ||
| * @param ConnectionPool $connectionPool | ||
| * @param string $host | ||
| * @param int $port | ||
| * @param int $database | ||
| * @param int $timeout | ||
| * @param string $auth | ||
| * @param bool $persistent | ||
| * @throws MissingExtensionException | ||
| */ | ||
| public function __construct(ConnectionPool $connectionPool, $host = '127.0.0.1', $port = NULL, $database = 0, $timeout = 10, $auth = NULL, $persistent = FALSE) | ||
| { | ||
| parent::__construct($host, $port, $database, $timeout, $auth, $persistent); | ||
| $this->connectionPool = $connectionPool; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| public function connect() | ||
| { | ||
| if (!$this->driver) { | ||
| $driver = $this->connectionPool->getConnection($this->getHost(), $this->getPort()); | ||
|
|
||
| if ($driver === NULL) { | ||
| $driver = new Driver\PhpRedisDriver(); | ||
| $this->connectionPool->addConnection($this->getHost(), $this->getPort(), $driver); | ||
| } | ||
|
|
||
| $this->driver = $driver; | ||
| } | ||
|
|
||
| parent::connect(); | ||
|
|
||
| $this->synchronizeClientDatabase(); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function send($cmd, array $args = array()) | ||
| { | ||
| if ($this->driver) { | ||
| if (!$this->driver->isConnected()) { | ||
| $this->connect(); | ||
| } | ||
| $this->synchronizeClientDatabase(); | ||
| } | ||
|
|
||
| return parent::send($cmd, $args); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| /** | ||
| * @throws RedisClientException | ||
| */ | ||
| private function synchronizeClientDatabase() | ||
| { | ||
| if ($this->driver->getDatabase() != $this->getDatabase()) { | ||
| if (call_user_func_array(array($this->driver, 'select'), [$this->getDatabase()]) === FALSE) { | ||
| throw new RedisClientException('Can\'t set client database on driver'); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here should be your name ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
He, yes, thanks :-)