<?php
/**
* * * * * * * *<4297088@qq.com>
* */
namespace cn\gz53\framework\sdk\redis;
use cn\gz53\framework\sdk\Sdk;
use Exception;
use Redis;
class RedisPoolSdk extends Sdk
{
private $_pool = [];
private $configList;
public function setConfig(RedisPoolConfigSetSdkI $po){
$this->configList = $po->getConfig();
return true;
}
public function init(RedisPoolInitSdkI $po){
$list = $this->configList;
if(!is_array($list)){
$this->sysLog(1, '', null);
return false;
}
foreach ($list as $key => $val){
try {
$redis = new Redis();
$redis->pconnect($val['host'], $val['port']);
if(!empty($val['password'])){
$redis->auth($val['password']);
}
$redis->select($val['db']);
$this->_pool[$key] = $redis;
} catch (Exception $e) {
printf("\n[%s]Redis init, key:%s,code:%d, msg:%s...",
date("Y-m-d H:i:s"), $key, $e->getCode(), $e->getMessage());
print_r($e->getTraceAsString());
$this->sysLog($e->getCode(), $e->getMessage(), $e->getTraceAsString());
return false;
}
}
return true;
}
public function getRedis(RedisPoolRedisGetSdkI $po){
$key = $po->getRedisKey();
if(!isset($this->_pool[$key])){
$this->sysLog(1, 'redis not exist', [$key]);
return false;
}
$po->setRedis($this->_pool[$key]);
return true;
}
}