|
|
|
@ -7,7 +7,9 @@
|
|
|
|
|
|
|
|
|
|
namespace yii\caching; |
|
|
|
|
|
|
|
|
|
use Yii; |
|
|
|
|
use yii\base\Component; |
|
|
|
|
use yii\base\InvalidConfigException; |
|
|
|
|
use yii\helpers\StringHelper; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -52,10 +54,12 @@ use yii\helpers\StringHelper;
|
|
|
|
|
abstract class Cache extends Component implements \ArrayAccess |
|
|
|
|
{ |
|
|
|
|
/** |
|
|
|
|
* @var string a string prefixed to every cache key so that it is unique. Defaults to null, meaning using |
|
|
|
|
* the value of [[Application::id]] as the key prefix. You may set this property to be an empty string |
|
|
|
|
* @var string a string prefixed to every cache key so that it is unique. If not set, |
|
|
|
|
* it will use a prefix generated from [[Application::id]]. You may set this property to be an empty string |
|
|
|
|
* if you don't want to use key prefix. It is recommended that you explicitly set this property to some |
|
|
|
|
* static value if the cached data needs to be shared among multiple applications. |
|
|
|
|
* |
|
|
|
|
* To ensure interoperability, only use alphanumeric characters should be used. |
|
|
|
|
*/ |
|
|
|
|
public $keyPrefix; |
|
|
|
|
/** |
|
|
|
@ -78,18 +82,18 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
{ |
|
|
|
|
parent::init(); |
|
|
|
|
if ($this->keyPrefix === null) { |
|
|
|
|
$this->keyPrefix = \Yii::$app->id; |
|
|
|
|
$this->keyPrefix = substr(md5(Yii::$app->id), 0, 5); |
|
|
|
|
} elseif (!ctype_alnum($this->keyPrefix)) { |
|
|
|
|
throw new InvalidConfigException(get_class($this) . '::keyPrefix should only contain alphanumeric characters.'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Builds a normalized cache key from a given key. |
|
|
|
|
* |
|
|
|
|
* The generated key contains letters and digits only, and its length is no more than 32. |
|
|
|
|
* |
|
|
|
|
* If the given key is a string containing alphanumeric characters only and no more than 32 characters, |
|
|
|
|
* then the key will be returned back without change. Otherwise, a normalized key |
|
|
|
|
* is generated by serializing the given key and applying MD5 hashing. |
|
|
|
|
* then the key will be returned back prefixed with [[keyPrefix]]. Otherwise, a normalized key |
|
|
|
|
* is generated by serializing the given key, applying MD5 hashing, and prefixing with [[keyPrefix]]. |
|
|
|
|
* |
|
|
|
|
* The following example builds a cache key using three parameters: |
|
|
|
|
* |
|
|
|
@ -97,8 +101,8 @@ abstract class Cache extends Component implements \ArrayAccess
|
|
|
|
|
* $key = $cache->buildKey(array($className, $method, $id)); |
|
|
|
|
* ~~~ |
|
|
|
|
* |
|
|
|
|
* @param array|string $key the key to be normalized |
|
|
|
|
* @return string the generated cache key include $this->keyPrefix |
|
|
|
|
* @param mixed $key the key to be normalized |
|
|
|
|
* @return string the generated cache key |
|
|
|
|
*/ |
|
|
|
|
public function buildKey($key) |
|
|
|
|
{ |
|
|
|
|