90 lines
2.2 KiB
PHP
90 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace phpcommon;
|
|
|
|
class Redis
|
|
{
|
|
private $_host = '';
|
|
private $_port = 0;
|
|
private $_passwd = '';
|
|
|
|
private $_conn = null;
|
|
private $_result = null;
|
|
|
|
public function __construct($configs){
|
|
$this->_host = array_key_exists('host', $configs) ? $configs['host'] : $this->_host;
|
|
$this->_port = array_key_exists('port', $configs) ? $configs['port'] : $this->_port;
|
|
$this->_passwd = array_key_exists('passwd', $configs) ? $configs['passwd'] : $this->_passwd;
|
|
$this->_redis = new \Redis();
|
|
$this->_redis->connect($this->_host,
|
|
$this->_port);
|
|
if (!empty($this->_passwd)) {
|
|
$this->_redis->auth($this->_passwd);
|
|
}
|
|
}
|
|
|
|
public function __destruct(){
|
|
$this->_redis = null;
|
|
}
|
|
|
|
private function rawcommand() {
|
|
$args = func_get_args();
|
|
return call_user_func_array(array($this->_redis, 'rawCommand'), $args);
|
|
}
|
|
|
|
public function hget($key, $hash_key){
|
|
return $this->_redis->hGet($key, $hash_key);
|
|
}
|
|
|
|
public function hlen($key){
|
|
return $this->_redis->hLen($key);
|
|
}
|
|
|
|
public function hexists($key, $hash_key){
|
|
return $this->_redis->hExists($key, $hash_key);
|
|
}
|
|
|
|
public function hdel($key, $hash_key){
|
|
return $this->_redis->hDel($key, $hash_key);
|
|
}
|
|
|
|
public function hset($key, $hash_key, $value){
|
|
return $this->_redis->hSet($key, $hash_key, $value);
|
|
}
|
|
|
|
public function get($key){
|
|
return $this->_redis->get($key);
|
|
}
|
|
|
|
public function exists($key){
|
|
return $this->_redis->exists($key);
|
|
}
|
|
|
|
public function del($key){
|
|
return $this->_redis->del($key);
|
|
}
|
|
|
|
public function set($key, $value){
|
|
return $this->_redis->set($key, $value);
|
|
}
|
|
|
|
public function pexpire($key, $mill_sec){
|
|
return $this->_redis->pexpire($key, $mill_sec);
|
|
}
|
|
|
|
public function incrBy($key, $num){
|
|
return $this->_redis->incrby($key, $num);
|
|
}
|
|
|
|
public function setNxPx($key, $val, $mill_sec) {
|
|
return $this->rawcommand('SET', $key, $val, 'NX', 'PX', $mill_sec);
|
|
}
|
|
|
|
public function execScript($script, $args, $num_keys) {
|
|
return $this->_redis->eval($script, $args, $num_keys);
|
|
}
|
|
|
|
}
|
|
|
|
?>
|