...
|
...
|
@@ -12,8 +12,6 @@ class Parser{ |
|
|
|
|
|
//路径
|
|
|
private $path;
|
|
|
//待替换参数
|
|
|
private $params;
|
|
|
|
|
|
/**
|
|
|
* 缓存句柄
|
...
|
...
|
@@ -37,30 +35,34 @@ class Parser{ |
|
|
static $self;
|
|
|
if (empty($self)){
|
|
|
$self = new self;
|
|
|
|
|
|
//初始化缓存句柄
|
|
|
if (function_exists('apcu_store')){
|
|
|
$self->cache = Yii::createObject([
|
|
|
'class'=>'yii\caching\ApcCache',
|
|
|
'useApcu'=>true
|
|
|
]);
|
|
|
}elseif (function_exists('apc_store')){
|
|
|
$self->cache = Yii::createObject('yii\caching\ApcCache');
|
|
|
}elseif (function_exists('shm_attach')){
|
|
|
$self->cache = Yii::createObject('common\components\caching\ShmCache');
|
|
|
}else{
|
|
|
$self->cache = Yii::createObject('yii\caching\ArrayCache');
|
|
|
}
|
|
|
}
|
|
|
return $self;
|
|
|
}
|
|
|
|
|
|
public function __construct() {
|
|
|
$this->cache = Yii::createObject('yii\caching\ArrayCache');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 载入路径
|
|
|
* @param type $path
|
|
|
* @param type $params
|
|
|
* @return type
|
|
|
*/
|
|
|
static function load($path,$params = []){
|
|
|
static function load($path){
|
|
|
$self = self::self();
|
|
|
$self->path = $path;
|
|
|
|
|
|
if (func_num_args() > 2){
|
|
|
$params = func_get_args();
|
|
|
array_shift($params);
|
|
|
}
|
|
|
$self->params = $params;
|
|
|
|
|
|
return $self;
|
|
|
}
|
|
|
|
...
|
...
|
@@ -68,9 +70,15 @@ class Parser{ |
|
|
* 获取最终键名
|
|
|
* @return type
|
|
|
*/
|
|
|
public function key(){
|
|
|
public function key($params = []){
|
|
|
|
|
|
if (func_num_args() > 1){
|
|
|
$params = func_get_args();
|
|
|
}
|
|
|
$path = $this->path;
|
|
|
$params = $this->params;
|
|
|
if (is_array($params)){
|
|
|
ksort($params);
|
|
|
}
|
|
|
|
|
|
$main_key = 'get'.$path.serialize($params);
|
|
|
if ($realkey = $this->cache->get($main_key)){
|
...
|
...
|
@@ -93,6 +101,10 @@ class Parser{ |
|
|
$this->cache->set($key,$cachekey,$this->expire);
|
|
|
}
|
|
|
|
|
|
if (count((array)$params) != preg_match_all('/{[^}]+}/',$cachekey)){
|
|
|
throw new \Exception('Number of arguments not match');
|
|
|
}
|
|
|
|
|
|
if (is_array($params)){
|
|
|
if (!is_list($params)){
|
|
|
$dict = $list = [];
|
...
|
...
|
@@ -125,7 +137,7 @@ class Parser{ |
|
|
* @return type
|
|
|
*/
|
|
|
private function addPrefix($key){
|
|
|
return strtr(Yii::$app->params['cache_prefix'].':'.$key,array('::'=>':'));
|
|
|
return rtrim(Yii::$app->params['cache_prefix'],':').':'.ltrim($key,':');
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
|