Redis bugfix

master
parent 437ee0efa6
commit 6daf823c5b
  1. 35
      Components/Redis.php

@ -6,28 +6,40 @@ class Harmonious_Components_Redis implements IHarmonious_Component{
// инициализация // инициализация
public function __construct (Harmonious $app) { public function __construct (Harmonious $app) {
$this->app = $app; $this->app = $app;
$this->redis = new Redis();
$this->doConnect();
}
public function __destruct () {
$this->redis->close();
}
protected function doConnect() {
$redis_server_ip = $this->app->config('redis_server_ip'); $redis_server_ip = $this->app->config('redis_server_ip');
$redis_server_port = $this->app->config('redis_server_port'); $redis_server_port = $this->app->config('redis_server_port');
$redis_server_timeout = ($this->app->config('redis_server_timeout')!==null ? $this->app->config('redis_server_timeout') : 1); $redis_server_timeout = ($this->app->config('redis_server_timeout')!==null ? $this->app->config('redis_server_timeout') : 1);
$redis_server_password = $this->app->config('redis_server_password'); $redis_server_password = $this->app->config('redis_server_password');
$this->redis = new Redis(); $ret = $this->redis->connect($redis_server_ip, $redis_server_port, $redis_server_timeout);
$this->redis->connect($redis_server_ip, $redis_server_port, $redis_server_timeout); if (!$ret) return false;
if (isset($redis_server_password)) $this->redis->auth($redis_server_password); if (isset($redis_server_password)) $ret = $this->redis->auth($redis_server_password);
} if (!$ret) return false;
return true;
public function __destruct () {
$this->redis->close();
} }
function set($key, $value, $expiration = 3600/*1 час*/) { function set($key, $value, $expiration = 3600/*1 час*/) {
if (!$this->redis->isConnected()) {
if (!$this->doConnect()) return array ('result'=>false);
}
$ret = $this->redis->setEx($key, $expiration, $value); $ret = $this->redis->setEx($key, $expiration, $value);
if (!$ret) return array ('result'=>false); if (!$ret) return array ('result'=>false);
else return array('result'=>true ); else return array('result'=>true );
} }
function get($key) { function get($key) {
if (!$this->redis->isConnected()) {
if (!$this->doConnect()) return array ('result'=>false);
}
$ret = $this->redis->get($key); $ret = $this->redis->get($key);
if ($ret === false) return array ('result'=>false); if ($ret === false) return array ('result'=>false);
else $ret = array ('result'=>true, 'value'=>$ret); else $ret = array ('result'=>true, 'value'=>$ret);
@ -35,18 +47,27 @@ class Harmonious_Components_Redis implements IHarmonious_Component{
} }
function delete($key) { function delete($key) {
if (!$this->redis->isConnected()) {
if (!$this->doConnect()) return array ('result'=>false);
}
$ret = $this->redis->delete($key); $ret = $this->redis->delete($key);
if ($ret === false) return array ('result'=>false); if ($ret === false) return array ('result'=>false);
else return array ('result'=>true); else return array ('result'=>true);
} }
function append($key, $value) { function append($key, $value) {
if (!$this->redis->isConnected()) {
if (!$this->doConnect()) return array ('result'=>false);
}
$ret = $this->redis->append($key, $value); $ret = $this->redis->append($key, $value);
if ($ret == 0) return array ('result'=>false); if ($ret == 0) return array ('result'=>false);
else return array('result'=> true); else return array('result'=> true);
} }
function exists($key) { function exists($key) {
if (!$this->redis->isConnected()) {
if (!$this->doConnect()) return array ('result'=>false);
}
$ret = $this->redis->exists($key); $ret = $this->redis->exists($key);
if ($ret == 0) return array ('result'=>false); if ($ret == 0) return array ('result'=>false);
else return array('result'=> true); else return array('result'=> true);

Loading…
Cancel
Save