redis = new \Redis; if (!$config) { $host = sysconf('redis_host'); if (!$host) { $config['host'] = '127.0.0.1'; $config['port'] = '6379'; //$config['password'] = 'dm_redis_123'; } else { $config['host'] = $host; $config['port'] = sysconf('redis_port'); $password = sysconf('redis_password'); if ($password) { $config['password'] = $password; } } } $this->connect($config); } public function connect($config) { if (isset($config["host"])) { $this->expire = isset($config['expire']) ? $config['expire'] : 86400*365; $this->redis->pconnect($config["host"], $config["port"]); if (isset($config['password']) && $config['password']) { $this->redis->auth($config['password']); } $this->redis->setOption(\Redis::OPT_READ_TIMEOUT, -1); } } public function getRedis() { return $this->redis; } public function push($key, $value) { $value = json_encode($value); return $this->redis->lpush($key, $value); } public function pop($key) { $data = $this->redis->brPop($key, 10); if ($data) { if (isset($data[1])) { $data = $data[1]; } $data = json_decode($data, true); return $data; } else { $pong = $this->redis->ping(); if ($pong != '+PONG') { throw new \Exception('Redis ping failure!', 500); } usleep(100000); } return false; } public function len($key) { return $this->redis->llen($key); } public function get($key) { if (!$this->redis) { return false; } $key = $this->key($key); $result = $this->redis->get($key); $result = json_decode($result, true); return $result; } public function set($key, $value, $expire = 0) { if (!$this->redis) { return false; } if (!is_string($key)) { return false; } $key = $this->key($key); $expire = $expire > 0 ? $expire : $this->expire; $value = json_encode($value); $result = $this->redis->set($key, $value, $expire); return $result; } public function incr($key, $value) { if (!$this->redis) { return false; } if (!is_string($key)) { return false; } $key = $this->key($key); $result = $this->redis->incr($key, $value); return $result; } public function hGet($key, $hkey = false) { if (!$this->redis) { return false; } $key = $this->key($key); if ($hkey) { $result = $this->redis->hGet($key, $hkey); } else { $result = $this->redis->hGetAll($key); } return $result; } public function hDel($key, $hkey) { if (!$this->redis) { return false; } $key = $this->key($key); $result = $this->redis->hDel($key, $hkey); return $result; } public function hExists($key, $hkey) { if (!$this->redis) { return false; } $key = $this->key($key); $result = $this->redis->hExists($key, $hkey); return $result; } public function hKeys($key) { if (!$this->redis) { return false; } $key = $this->key($key); $result = $this->redis->hKeys($key); return $result; } public function hSet($key, $hkey, $value, $expire = 0) { if (!$this->redis) { return false; } if (!is_string($key)) { return false; } $key = $this->key($key); $expire = $expire > 0 ? $expire : $this->expire; $result = $this->redis->hSet($key, $hkey, $value); return $result; } public function delete($key) { if (!$this->redis) { return false; } if (!is_array($key)) { $key = $this->key($key); } if ($this->redis->del($key)) { return true; } return false; } public function close() { if (!$this->redis) { return false; } if ($this->redis->close()) { return true; } return false; } private function key($key) { return 'v1_' . $key; // return DEVER_APP_NAME . '_' . $key; } }