Authored by wuxiao

缓存键生成组件

... ... @@ -3,9 +3,6 @@
namespace common\components\cachekey;
use Yii;
use yii\di\Instance;
use yii\di\Container;
use yii\caching\ArrayCache;
/**
* @author wuxiao
... ... @@ -13,12 +10,24 @@ use yii\caching\ArrayCache;
*/
class Parser{
static $error;
/**
* 缓存句柄
* @var type
*/
private $cache;
/**
* 缓存过期时间,秒
* @var type
*/
private $expire = 0;
/**
* 错误信息
* @var type
*/
static $error;
static function self(){
static $self;
if (empty($self)){
... ... @@ -35,9 +44,12 @@ class Parser{
$self = self::self();
$main_key = 'get'.$path.serialize($params);
if ($realkey = $self->cache->get($main_key)){
return $realkey;
}
$key = 'cacheKey'.$path;
if (!$cacheKey = $self->cache->get($key)){
if (!$cachekey = $self->cache->get($key)){
$cache = basename($path);
$dir = dirname($path);
if (!$cacheMap = $self->read($dir)){
... ... @@ -48,8 +60,8 @@ class Parser{
self::$error = 'cannot find map key '.$cache;
return;
}
$cacheKey = $cacheMap[$cache];
$self->cache->set($key,$cacheKey,$self->expire);
$cachekey = $cacheMap[$cache];
$self->cache->set($key,$cachekey,$self->expire);
}
if (func_num_args() > 2){
... ... @@ -66,27 +78,29 @@ class Parser{
$list[] = $value;
}
}
$cacheKey = strtr($cacheKey,$dict);
$cachekey = strtr($cachekey,$dict);
$params = $list;
}
$cacheKey = preg_replace('/{[^}]+}/', '%s', $cacheKey);
$cacheKey = call_user_func_array('sprintf',array($cacheKey)+$params);
$realkey = preg_replace('/{[^}]+}/', '%s', $cachekey);
$realkey = call_user_func_array('sprintf',array($realkey)+$params);
}else{
$cacheKey = preg_replace('/{[^}]+}/', '%s', $cacheKey);
$cacheKey = sprintf($cacheKey,$params);
$realkey = preg_replace('/{[^}]+}/', '%s', $cachekey);
$realkey = sprintf($realkey,$params);
}
return $cacheKey;
$self->cache->set($main_key,$realkey,$self->expire);
return $realkey;
}
private function read($path){
$key = __FUNCTION__.$path;
if (!$content = $self->cache->get($key)){
if (!$content = $this->cache->get($key)){
if (!is_readable($file = __DIR__.'/keys/'.$path.'.php')){
return false;
}
$content = include($file);
$self->cache->set($key,$content,$self->expire);
$this->cache->set($key,$content,$this->expire);
}
return $content;
}
... ...
<?php
var_dump('list.php!');
return [
'best'=>'best{app}',
'listing'=>'listing{app}',
... ...