Commit 0ab5bbaf by votintsev

Update Cache.php

Moved add prefix ($this->keyPrefix) to the key in function buildKey(). Could it be better?
parent ead91417
...@@ -98,15 +98,16 @@ abstract class Cache extends Component implements \ArrayAccess ...@@ -98,15 +98,16 @@ abstract class Cache extends Component implements \ArrayAccess
* ~~~ * ~~~
* *
* @param array|string $key the key to be normalized * @param array|string $key the key to be normalized
* @return string the generated cache key * @return string the generated cache key include $this->keyPrefix
*/ */
public function buildKey($key) public function buildKey($key)
{ {
if (is_string($key)) { if (is_string($key)) {
return ctype_alnum($key) && StringHelper::strlen($key) <= 32 ? $key : md5($key); $key = ctype_alnum($key) && StringHelper::strlen($key) <= 32 ? $key : md5($key);
} else { } else {
return md5(json_encode($key)); $key = md5(json_encode($key));
} }
return $this->keyPrefix . $key;
} }
/** /**
...@@ -117,7 +118,7 @@ abstract class Cache extends Component implements \ArrayAccess ...@@ -117,7 +118,7 @@ abstract class Cache extends Component implements \ArrayAccess
*/ */
public function get($key) public function get($key)
{ {
$key = $this->keyPrefix . $this->buildKey($key); $key = $this->buildKey($key);
$value = $this->getValue($key); $value = $this->getValue($key);
if ($value === false || $this->serializer === false) { if ($value === false || $this->serializer === false) {
return $value; return $value;
...@@ -147,7 +148,7 @@ abstract class Cache extends Component implements \ArrayAccess ...@@ -147,7 +148,7 @@ abstract class Cache extends Component implements \ArrayAccess
{ {
$keyMap = array(); $keyMap = array();
foreach ($keys as $key) { foreach ($keys as $key) {
$keyMap[$key] = $this->keyPrefix . $this->buildKey($key); $keyMap[$key] = $this->buildKey($key);
} }
$values = $this->getValues(array_values($keyMap)); $values = $this->getValues(array_values($keyMap));
$results = array(); $results = array();
...@@ -192,7 +193,7 @@ abstract class Cache extends Component implements \ArrayAccess ...@@ -192,7 +193,7 @@ abstract class Cache extends Component implements \ArrayAccess
} elseif ($this->serializer !== false) { } elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], array($value, $dependency)); $value = call_user_func($this->serializer[0], array($value, $dependency));
} }
$key = $this->keyPrefix . $this->buildKey($key); $key = $this->buildKey($key);
return $this->setValue($key, $value, $expire); return $this->setValue($key, $value, $expire);
} }
...@@ -217,7 +218,7 @@ abstract class Cache extends Component implements \ArrayAccess ...@@ -217,7 +218,7 @@ abstract class Cache extends Component implements \ArrayAccess
} elseif ($this->serializer !== false) { } elseif ($this->serializer !== false) {
$value = call_user_func($this->serializer[0], array($value, $dependency)); $value = call_user_func($this->serializer[0], array($value, $dependency));
} }
$key = $this->keyPrefix . $this->buildKey($key); $key = $this->buildKey($key);
return $this->addValue($key, $value, $expire); return $this->addValue($key, $value, $expire);
} }
...@@ -228,7 +229,7 @@ abstract class Cache extends Component implements \ArrayAccess ...@@ -228,7 +229,7 @@ abstract class Cache extends Component implements \ArrayAccess
*/ */
public function delete($key) public function delete($key)
{ {
$key = $this->keyPrefix . $this->buildKey($key); $key = $this->buildKey($key);
return $this->deleteValue($key); return $this->deleteValue($key);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment