Browse Source

Modify URL Rules caching #13425

tags/2.0.14
Yuriy Stovbur 8 years ago
parent
commit
46e7faddd0
  1. 41
      framework/web/UrlManager.php

41
framework/web/UrlManager.php

@ -172,24 +172,16 @@ class UrlManager extends Component
} }
} }
if (!$this->enablePrettyUrl || empty($this->rules)) { if (!$this->enablePrettyUrl) {
return; return;
} }
if (is_string($this->cache)) { if (is_string($this->cache)) {
$this->cache = Yii::$app->get($this->cache, false); $this->cache = Yii::$app->get($this->cache, false);
} }
if ($this->cache instanceof Cache) { if (empty($this->rules)) {
$cacheKey = $this->cacheKey; return;
$hash = md5(json_encode($this->rules));
if (($data = $this->cache->get($cacheKey)) !== false && isset($data[1]) && $data[1] === $hash) {
$this->rules = $data[0];
} else {
$this->rules = $this->buildRules($this->rules);
$this->cache->set($cacheKey, [$this->rules, $hash]);
}
} else {
$this->rules = $this->buildRules($this->rules);
} }
$this->rules = $this->buildRules($this->rules);
} }
/** /**
@ -209,19 +201,7 @@ class UrlManager extends Component
if (!$this->enablePrettyUrl) { if (!$this->enablePrettyUrl) {
return; return;
} }
$rules = $this->buildRules($rules);
if ($this->cache instanceof Cache) {
$cacheKey = $this->cacheKey . ':' . md5(json_encode($rules));
if (($data = $this->cache->get($cacheKey)) !== false) {
$rules = $data;
} else {
$rules = $this->buildRules($rules);
$this->cache->set($cacheKey, $rules);
}
} else {
$rules = $this->buildRules($rules);
}
if ($append) { if ($append) {
$this->rules = array_merge($this->rules, $rules); $this->rules = array_merge($this->rules, $rules);
} else { } else {
@ -238,6 +218,13 @@ class UrlManager extends Component
*/ */
protected function buildRules($rules) protected function buildRules($rules)
{ {
if ($this->cache instanceof Cache) {
$cacheKey = $this->cacheKey . '#' . md5(json_encode($rules));
if (($compiledRules = $this->cache->get($cacheKey)) !== false) {
return $compiledRules;
}
}
$compiledRules = []; $compiledRules = [];
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS'; $verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS';
foreach ($rules as $key => $rule) { foreach ($rules as $key => $rule) {
@ -261,6 +248,10 @@ class UrlManager extends Component
} }
$compiledRules[] = $rule; $compiledRules[] = $rule;
} }
if ($this->cache instanceof Cache) {
$this->cache->set($cacheKey, $compiledRules);
}
return $compiledRules; return $compiledRules;
} }

Loading…
Cancel
Save