Created
February 3, 2014 09:05
-
-
Save lorenzo/8780782 to your computer and use it in GitHub Desktop.
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
<?php | |
/** | |
* Custom redis client class to be able to set a custom prefix for every key | |
* | |
* @see https://github.com/nicolasff/phpredis | |
**/ | |
class RedisClient extends Redis { | |
/** | |
* Default prefix | |
* | |
* @var string | |
**/ | |
protected $_prefix = null; | |
/** | |
* Constructor. | |
* | |
* @param array $config | |
* - prefix: string to be prefixed to all keys | |
* - port | |
* - hostname | |
* | |
* @return void | |
**/ | |
public function __construct($config = array()) { | |
parent::__construct(); | |
if (isset($config['prefix'])) { | |
$this->_prefix = $config['prefix']; | |
} | |
$port = isset($config['port']) ? $config['port'] : null; | |
$this->pconnect($config['host'], $port); | |
$this->setOption(Redis::OPT_PREFIX, $this->_prefix); | |
} | |
/** | |
* Clears all keys under configured prefix | |
* | |
* @return void | |
**/ | |
public function clear() { | |
$this->setOption(Redis::OPT_PREFIX, null); | |
$this->delete($this->keys(sprintf('%s*', $this->_prefix))); | |
$this->setOption(Redis::OPT_PREFIX, $this->_prefix); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment