類別 yii\caching\TagDependency
繼承關係 | yii\caching\TagDependency » yii\caching\Dependency » yii\base\BaseObject |
---|---|
實作介面 | yii\base\Configurable |
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/caching/TagDependency.php |
TagDependency 將快取資料項目與一個或多個 $tags 關聯。
透過呼叫 invalidate(),您可以使所有與指定標籤名稱相關聯的快取資料項目失效。
// setting multiple cache keys to store data forever and tagging them with "user-123"
Yii::$app->cache->set('user_42_profile', '', 0, new TagDependency(['tags' => 'user-123']));
Yii::$app->cache->set('user_42_stats', '', 0, new TagDependency(['tags' => 'user-123']));
// invalidating all keys tagged with "user-123"
TagDependency::invalidate(Yii::$app->cache, 'user-123');
關於快取的更多詳細資訊和使用方式,請參閱快取概觀的指南文章。
公共屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$data | mixed | 相依性資料,儲存在快取中,稍後會與最新的相依性資料進行比較。 | yii\caching\Dependency |
$reusable | boolean | 此相依性是否可重複使用。 | yii\caching\Dependency |
$tags | string|array | 此相依性的標籤名稱列表。 | yii\caching\TagDependency |
公共方法
方法 | 描述 | 定義於 |
---|---|---|
__call() | 呼叫未定義為類別方法的具名方法。 | yii\base\BaseObject |
__construct() | 建構子。 | yii\base\BaseObject |
__get() | 傳回物件屬性的值。 | yii\base\BaseObject |
__isset() | 檢查屬性是否已設定,即已定義且非 null。 | yii\base\BaseObject |
__set() | 設定物件屬性的值。 | yii\base\BaseObject |
__unset() | 將物件屬性設定為 null。 | yii\base\BaseObject |
canGetProperty() | 傳回一個值,指示屬性是否可讀取。 | yii\base\BaseObject |
canSetProperty() | 傳回一個值,指示屬性是否可設定。 | yii\base\BaseObject |
className() | 傳回此類別的完整限定名稱。 | yii\base\BaseObject |
evaluateDependency() | 透過產生和儲存與相依性相關的資料來評估相依性。 | yii\caching\Dependency |
getHasChanged() | 傳回一個值,指示相依性是否已變更。 | yii\caching\Dependency |
hasMethod() | 傳回一個值,指示是否已定義方法。 | yii\base\BaseObject |
hasProperty() | 傳回一個值,指示是否已定義屬性。 | yii\base\BaseObject |
init() | 初始化物件。 | yii\base\BaseObject |
invalidate() | 使所有與任何指定的 $tags 相關聯的快取資料項目失效。 | yii\caching\TagDependency |
isChanged() | 檢查相依性是否已變更。 | yii\caching\TagDependency |
resetReusableData() | 重置可重複使用相依性的所有快取資料。 | yii\caching\Dependency |
保護方法
方法 | 描述 | 定義於 |
---|---|---|
generateDependencyData() | 產生判斷相依性是否已變更所需的資料。 | yii\caching\TagDependency |
generateReusableHash() | 產生可用於檢索可重複使用相依性資料的唯一雜湊值。 | yii\caching\Dependency |
getTimestamps() | 傳回指定標籤的時間戳記。 | yii\caching\TagDependency |
touchKeys() | 為指定的快取鍵產生時間戳記。 | yii\caching\TagDependency |
屬性詳細資訊
方法詳細資訊
public mixed __call ( $name, $params ) | ||
$name | string |
方法名稱 |
$params | array |
方法參數 |
return | mixed |
方法傳回值 |
---|---|---|
throws | yii\base\UnknownMethodException |
當呼叫未知方法時 |
public function __call($name, $params)
{
throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}
定義於: yii\base\BaseObject::__construct()
建構子。
預設實作會做兩件事
- 使用給定的組態
$config
初始化物件。 - 呼叫 init()。
如果子類別覆寫了此方法,建議
- 建構子的最後一個參數是一個組態陣列,就像這裡的
$config
一樣。 - 在建構子的結尾呼叫父類別的實作。
public void __construct ( $config = [] ) | ||
$config | array |
將用於初始化物件屬性的名稱-值配對 |
public function __construct($config = [])
{
if (!empty($config)) {
Yii::configure($this, $config);
}
$this->init();
}
定義於: yii\base\BaseObject::__get()
傳回物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP magic method,當執行 $value = $object->property;
時會被隱式呼叫。
另請參閱 __set()。
public mixed __get ( $name ) | ||
$name | string |
屬性名稱 |
return | mixed |
屬性值 |
---|---|---|
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
throws | yii\base\InvalidCallException |
如果屬性為唯寫 |
public function __get($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter();
} elseif (method_exists($this, 'set' . $name)) {
throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
}
throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}
定義於: yii\base\BaseObject::__isset()
檢查屬性是否已設定,即已定義且非 null。
請勿直接呼叫此方法,因為它是一個 PHP magic method,當執行 isset($object->property)
時會被隱式呼叫。
請注意,如果屬性未定義,將會回傳 false。
public boolean __isset ( $name ) | ||
$name | string |
屬性名稱或事件名稱 |
return | boolean |
指定的屬性是否已設定 (非 null)。 |
---|
public function __isset($name)
{
$getter = 'get' . $name;
if (method_exists($this, $getter)) {
return $this->$getter() !== null;
}
return false;
}
定義於: yii\base\BaseObject::__set()
設定物件屬性的值。
請勿直接呼叫此方法,因為它是一個 PHP magic method,當執行 $object->property = $value;
時會被隱式呼叫。
另請參閱 __get()。
public void __set ( $name, $value ) | ||
$name | string |
屬性名稱或事件名稱 |
$value | mixed |
屬性值 |
throws | yii\base\UnknownPropertyException |
如果屬性未定義 |
---|---|---|
throws | yii\base\InvalidCallException |
如果屬性為唯讀 |
public function __set($name, $value)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter($value);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
} else {
throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}
}
定義於: yii\base\BaseObject::__unset()
將物件屬性設定為 null。
請勿直接呼叫此方法,因為它是一個 PHP magic method,當執行 unset($object->property)
時會被隱式呼叫。
請注意,如果屬性未定義,此方法將不會執行任何操作。如果屬性為唯讀,則會拋出例外。
public void __unset ( $name ) | ||
$name | string |
屬性名稱 |
throws | yii\base\InvalidCallException |
如果屬性為唯讀。 |
---|
public function __unset($name)
{
$setter = 'set' . $name;
if (method_exists($this, $setter)) {
$this->$setter(null);
} elseif (method_exists($this, 'get' . $name)) {
throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
}
}
定義於: yii\base\BaseObject::canGetProperty()
傳回一個值,指示屬性是否可讀取。
如果符合以下條件,屬性即可讀取:
- 類別具有與指定名稱相關聯的 getter 方法 (在此情況下,屬性名稱不區分大小寫);
- 類別具有與指定名稱相同的成員變數 (當
$checkVars
為 true 時);
另請參閱 canSetProperty()。
public boolean canGetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可讀取 |
---|
public function canGetProperty($name, $checkVars = true)
{
return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}
定義於: yii\base\BaseObject::canSetProperty()
傳回一個值,指示屬性是否可設定。
如果符合以下條件,屬性即可寫入:
- 類別具有與指定名稱相關聯的 setter 方法 (在此情況下,屬性名稱不區分大小寫);
- 類別具有與指定名稱相同的成員變數 (當
$checkVars
為 true 時);
另請參閱 canGetProperty()。
public boolean canSetProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否可寫入 |
---|
public function canSetProperty($name, $checkVars = true)
{
return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}
::class
。
定義於: yii\base\BaseObject::className()
傳回此類別的完整限定名稱。
public static string className ( ) | ||
return | string |
此類別的完整限定名稱。 |
---|
public static function className()
{
return get_called_class();
}
public void evaluateDependency ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
public function evaluateDependency($cache)
{
if ($this->reusable) {
$hash = $this->generateReusableHash();
if (!array_key_exists($hash, self::$_reusableData)) {
self::$_reusableData[$hash] = $this->generateDependencyData($cache);
}
$this->data = self::$_reusableData[$hash];
} else {
$this->data = $this->generateDependencyData($cache);
}
}
產生判斷相依性是否已變更所需的資料。
此方法在此類別中不執行任何操作。
protected mixed generateDependencyData ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
return | mixed |
判斷相依性是否已變更所需的資料。 |
---|
protected function generateDependencyData($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
$newKeys = [];
foreach ($timestamps as $key => $timestamp) {
if ($timestamp === false) {
$newKeys[] = $key;
}
}
if (!empty($newKeys)) {
$timestamps = array_merge($timestamps, static::touchKeys($cache, $newKeys));
}
return $timestamps;
}
protected string generateReusableHash ( ) | ||
return | string |
此快取相依性的唯一雜湊值。 |
---|
protected function generateReusableHash()
{
$clone = clone $this;
$clone->data = null; // https://github.com/yiisoft/yii2/issues/3052
try {
$serialized = serialize($clone);
} catch (\Exception $e) {
// unserializable properties are nulled
foreach ($clone as $name => $value) {
if (is_object($value) && $value instanceof \Closure) {
$clone->{$name} = null;
}
}
$serialized = serialize($clone);
}
return sha1($serialized);
}
定義於: yii\caching\Dependency::getHasChanged()
傳回一個值,指示相依性是否已變更。
public boolean getHasChanged ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
return | boolean |
相依性是否已變更。 |
---|
public function getHasChanged($cache)
{
return $this->isChanged($cache);
}
傳回指定標籤的時間戳記。
protected array getTimestamps ( $cache, $tags ) | ||
$cache | yii\caching\CacheInterface | |
$tags | string[] | |
return | array |
以指定標籤索引的時間戳記。 |
---|
protected function getTimestamps($cache, $tags)
{
if (empty($tags)) {
return [];
}
$keys = [];
foreach ($tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
return $cache->multiGet($keys);
}
定義於: yii\base\BaseObject::hasMethod()
傳回一個值,指示是否已定義方法。
預設實作是呼叫 php 函數 method_exists()
。當您實作了 php magic method __call()
時,您可以覆寫此方法。
public boolean hasMethod ( $name ) | ||
$name | string |
方法名稱 |
return | boolean |
方法是否已定義 |
---|
public function hasMethod($name)
{
return method_exists($this, $name);
}
定義於: yii\base\BaseObject::hasProperty()
傳回一個值,指示是否已定義屬性。
如果符合以下條件,屬性即已定義:
- 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在此情況下,屬性名稱不區分大小寫);
- 類別具有與指定名稱相同的成員變數 (當
$checkVars
為 true 時);
另請參閱
public boolean hasProperty ( $name, $checkVars = true ) | ||
$name | string |
屬性名稱 |
$checkVars | boolean |
是否將成員變數視為屬性 |
return | boolean |
屬性是否已定義 |
---|
public function hasProperty($name, $checkVars = true)
{
return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}
public void init ( ) |
public function init()
{
}
使所有與任何指定的 $tags 相關聯的快取資料項目失效。
public static void invalidate ( $cache, $tags ) | ||
$cache | yii\caching\CacheInterface |
快取資料項目的快取元件 |
$tags | string|array |
public static function invalidate($cache, $tags)
{
$keys = [];
foreach ((array) $tags as $tag) {
$keys[] = $cache->buildKey([__CLASS__, $tag]);
}
static::touchKeys($cache, $keys);
}
檢查相依性是否已變更。
public boolean isChanged ( $cache ) | ||
$cache | yii\caching\CacheInterface |
目前正在評估此相依性的快取元件 |
return | boolean |
相依性是否已變更。 |
---|
public function isChanged($cache)
{
$timestamps = $this->getTimestamps($cache, (array) $this->tags);
return $timestamps !== $this->data;
}
定義於: yii\caching\Dependency::resetReusableData()
重置可重複使用相依性的所有快取資料。
public static void resetReusableData ( ) |
public static function resetReusableData()
{
self::$_reusableData = [];
}
為指定的快取鍵產生時間戳記。
protected static array touchKeys ( $cache, $keys ) | ||
$cache | yii\caching\CacheInterface | |
$keys | string[] | |
return | array |
以快取鍵索引的時間戳記 |
---|
protected static function touchKeys($cache, $keys)
{
$items = [];
$time = microtime();
foreach ($keys as $key) {
$items[$key] = $time;
}
$cache->multiSet($items);
return $items;
}
註冊 或 登入 以發表評論。