Authored by wuxiao

缓存键生成组件

@@ -3,9 +3,6 @@ @@ -3,9 +3,6 @@
3 namespace common\components\cachekey; 3 namespace common\components\cachekey;
4 4
5 use Yii; 5 use Yii;
6 -use yii\di\Instance;  
7 -use yii\di\Container;  
8 -use yii\caching\ArrayCache;  
9 6
10 /** 7 /**
11 * @author wuxiao 8 * @author wuxiao
@@ -13,12 +10,24 @@ use yii\caching\ArrayCache; @@ -13,12 +10,24 @@ use yii\caching\ArrayCache;
13 */ 10 */
14 class Parser{ 11 class Parser{
15 12
16 - static $error;  
17 - 13 + /**
  14 + * 缓存句柄
  15 + * @var type
  16 + */
18 private $cache; 17 private $cache;
19 18
  19 + /**
  20 + * 缓存过期时间,秒
  21 + * @var type
  22 + */
20 private $expire = 0; 23 private $expire = 0;
21 24
  25 + /**
  26 + * 错误信息
  27 + * @var type
  28 + */
  29 + static $error;
  30 +
22 static function self(){ 31 static function self(){
23 static $self; 32 static $self;
24 if (empty($self)){ 33 if (empty($self)){
@@ -35,9 +44,12 @@ class Parser{ @@ -35,9 +44,12 @@ class Parser{
35 $self = self::self(); 44 $self = self::self();
36 45
37 $main_key = 'get'.$path.serialize($params); 46 $main_key = 'get'.$path.serialize($params);
  47 + if ($realkey = $self->cache->get($main_key)){
  48 + return $realkey;
  49 + }
38 50
39 $key = 'cacheKey'.$path; 51 $key = 'cacheKey'.$path;
40 - if (!$cacheKey = $self->cache->get($key)){ 52 + if (!$cachekey = $self->cache->get($key)){
41 $cache = basename($path); 53 $cache = basename($path);
42 $dir = dirname($path); 54 $dir = dirname($path);
43 if (!$cacheMap = $self->read($dir)){ 55 if (!$cacheMap = $self->read($dir)){
@@ -48,8 +60,8 @@ class Parser{ @@ -48,8 +60,8 @@ class Parser{
48 self::$error = 'cannot find map key '.$cache; 60 self::$error = 'cannot find map key '.$cache;
49 return; 61 return;
50 } 62 }
51 - $cacheKey = $cacheMap[$cache];  
52 - $self->cache->set($key,$cacheKey,$self->expire); 63 + $cachekey = $cacheMap[$cache];
  64 + $self->cache->set($key,$cachekey,$self->expire);
53 } 65 }
54 66
55 if (func_num_args() > 2){ 67 if (func_num_args() > 2){
@@ -66,27 +78,29 @@ class Parser{ @@ -66,27 +78,29 @@ class Parser{
66 $list[] = $value; 78 $list[] = $value;
67 } 79 }
68 } 80 }
69 - $cacheKey = strtr($cacheKey,$dict); 81 + $cachekey = strtr($cachekey,$dict);
70 $params = $list; 82 $params = $list;
71 } 83 }
72 84
73 - $cacheKey = preg_replace('/{[^}]+}/', '%s', $cacheKey);  
74 - $cacheKey = call_user_func_array('sprintf',array($cacheKey)+$params); 85 + $realkey = preg_replace('/{[^}]+}/', '%s', $cachekey);
  86 + $realkey = call_user_func_array('sprintf',array($realkey)+$params);
75 }else{ 87 }else{
76 - $cacheKey = preg_replace('/{[^}]+}/', '%s', $cacheKey);  
77 - $cacheKey = sprintf($cacheKey,$params); 88 + $realkey = preg_replace('/{[^}]+}/', '%s', $cachekey);
  89 + $realkey = sprintf($realkey,$params);
78 } 90 }
79 - return $cacheKey; 91 +
  92 + $self->cache->set($main_key,$realkey,$self->expire);
  93 + return $realkey;
80 } 94 }
81 95
82 private function read($path){ 96 private function read($path){
83 $key = __FUNCTION__.$path; 97 $key = __FUNCTION__.$path;
84 - if (!$content = $self->cache->get($key)){ 98 + if (!$content = $this->cache->get($key)){
85 if (!is_readable($file = __DIR__.'/keys/'.$path.'.php')){ 99 if (!is_readable($file = __DIR__.'/keys/'.$path.'.php')){
86 return false; 100 return false;
87 } 101 }
88 $content = include($file); 102 $content = include($file);
89 - $self->cache->set($key,$content,$self->expire); 103 + $this->cache->set($key,$content,$this->expire);
90 } 104 }
91 return $content; 105 return $content;
92 } 106 }
1 <?php 1 <?php
2 -var_dump('list.php!'); 2 +
3 return [ 3 return [
4 'best'=>'best{app}', 4 'best'=>'best{app}',
5 'listing'=>'listing{app}', 5 'listing'=>'listing{app}',