Skip to content

Instantly share code, notes, and snippets.

@lorenzo
Created February 3, 2014 09:05
Show Gist options
  • Save lorenzo/8780782 to your computer and use it in GitHub Desktop.
Save lorenzo/8780782 to your computer and use it in GitHub Desktop.
<?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