0 追蹤者

類別 yii\caching\DbCache

繼承關係yii\caching\DbCache » yii\caching\Cache » yii\base\Component » yii\base\BaseObject
實作介面yii\base\Configurable, yii\caching\CacheInterface
自版本起可用2.0
原始碼 https://github.com/yiisoft/yii2/blob/master/framework/caching/DbCache.php

DbCache 實作了一個快取應用程式元件,透過將快取資料儲存在資料庫中。

預設情況下,DbCache 將 session 資料儲存在名為 'cache' 的資料庫表格中。此表格必須預先建立。表格名稱可以透過設定 $cacheTable 來變更。

請參考 yii\caching\Cache 以了解 DbCache 支援的常見快取操作。

以下範例展示如何設定應用程式以使用 DbCache

'cache' => [
    'class' => 'yii\caching\DbCache',
    // 'db' => 'mydb',
    // 'cacheTable' => 'my_cache',
]

有關快取的更多詳細資訊和使用方法,請參閱 快取概觀指南文章

公開屬性

隱藏繼承的屬性

屬性 類型 描述 定義於
$behaviors yii\base\Behavior[] 附加到此元件的行為列表。 yii\base\Component
$cacheTable 字串 用於儲存快取內容的資料庫表格名稱。 yii\caching\DbCache
$db yii\db\Connection|陣列|字串 資料庫連線物件或資料庫連線的應用程式元件 ID。 yii\caching\DbCache
$defaultDuration 整數 快取項目過期前的預設持續時間(秒)。 yii\caching\Cache
$gcProbability 整數 當在快取中儲存一塊資料時,應執行垃圾回收 (GC) 的機率(百萬分率)。 yii\caching\DbCache
$isVarbinaryDataField yii\caching\DbCache
$keyPrefix 字串 前綴於每個快取鍵的字串,使其在整個快取儲存空間中成為全域唯一。 yii\caching\Cache
$serializer array|null|false 用於序列化和反序列化快取資料的函式。 yii\caching\Cache

受保護的屬性

隱藏繼承的屬性

屬性 類型 描述 定義於

公開方法

隱藏繼承的方法

方法 描述 定義於
__call() 呼叫指定的名稱方法,該方法不是類別方法。 yii\base\Component
__clone() 在通過複製現有物件建立新物件後,將會呼叫此方法。 yii\base\Component
__construct() 建構子。 yii\base\BaseObject
__get() 傳回組件屬性的值。 yii\base\Component
__isset() 檢查屬性是否已設定,即已定義且非 null。 yii\base\Component
__set() 設定組件屬性的值。 yii\base\Component
__unset() 將組件屬性設定為 null。 yii\base\Component
add() 如果快取中不包含此鍵,則將由鍵識別的值儲存到快取中。 yii\caching\Cache
attachBehavior() 將行為附加到此組件。 yii\base\Component
attachBehaviors() 將行為列表附加到組件。 yii\base\Component
behaviors() 傳回此組件應表現為的行為列表。 yii\base\Component
buildKey() 從給定的鍵建立標準化的快取鍵。 yii\caching\Cache
canGetProperty() 傳回一個值,指示屬性是否可以被讀取。 yii\base\Component
canSetProperty() 傳回一個值,指示屬性是否可以被設定。 yii\base\Component
className() 傳回此類別的完整限定名稱。 yii\base\BaseObject
delete() 從快取中刪除具有指定鍵的值。 yii\caching\Cache
detachBehavior() 從組件中分離行為。 yii\base\Component
detachBehaviors() 從組件中分離所有行為。 yii\base\Component
ensureBehaviors() 確保在 behaviors() 中宣告的行為已附加到此組件。 yii\base\Component
exists() 檢查快取中是否存在指定的鍵。 yii\caching\DbCache
flush() 從快取中刪除所有值。 yii\caching\Cache
gc() 移除過期的資料值。 yii\caching\DbCache
get() 使用指定的鍵從快取中檢索值。 yii\caching\Cache
getBehavior() 傳回指定的行為物件。 yii\base\Component
getBehaviors() 傳回附加到此組件的所有行為。 yii\base\Component
getOrSet() 此方法結合了 set()get() 方法,以檢索由 $key 識別的值,或者在 $key 沒有可用的快取時,儲存 $callable 執行的結果。 yii\caching\Cache
hasEventHandlers() 傳回一個值,指示是否有名稱事件附加任何處理常式。 yii\base\Component
hasMethod() 傳回一個值,指示是否已定義方法。 yii\base\Component
hasProperty() 傳回一個值,指示是否為此組件定義了屬性。 yii\base\Component
init() 初始化 DbCache 組件。 yii\caching\DbCache
madd() 在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。 yii\caching\Cache
mget() 使用指定的鍵從快取中檢索多個值。 yii\caching\Cache
mset() 在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。 yii\caching\Cache
multiAdd() 在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。 yii\caching\Cache
multiGet() 使用指定的鍵從快取中檢索多個值。 yii\caching\Cache
multiSet() 在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。 yii\caching\Cache
off() 從此組件分離現有的事件處理常式。 yii\base\Component
offsetExists() 傳回是否具有指定鍵的快取條目。 yii\caching\Cache
offsetGet() 使用指定的鍵從快取中檢索值。 yii\caching\Cache
offsetSet() 將由鍵識別的值儲存到快取中。 yii\caching\Cache
offsetUnset() 從快取中刪除具有指定鍵的值。此方法是 ArrayAccess 介面所要求的。 yii\caching\Cache
on() 將事件處理常式附加到事件。 yii\base\Component
set() 將由鍵識別的值儲存到快取中。 yii\caching\Cache
trigger() 觸發事件。 yii\base\Component

受保護的方法

隱藏繼承的方法

方法 描述 定義於
addValue() 如果快取中不包含此鍵,則將由鍵識別的值儲存到快取中。 yii\caching\DbCache
addValues() 將多個鍵值對新增到快取中。 yii\caching\Cache
deleteValue() 從快取中刪除具有指定鍵的值。這是父類別中宣告的方法的實作。 yii\caching\DbCache
flushValues() 從快取中刪除所有值。 yii\caching\DbCache
getDataFieldName() yii\caching\DbCache
getDataFieldValue() yii\caching\DbCache
getValue() 使用指定的鍵從快取中檢索值。 yii\caching\DbCache
getValues() 使用指定的鍵從快取中檢索多個值。 yii\caching\DbCache
isVarbinaryDataField() yii\caching\DbCache
setValue() 將由鍵識別的值儲存在快取中。 yii\caching\DbCache
setValues() 在快取中儲存多個鍵值對。 yii\caching\Cache

屬性詳細資訊

隱藏繼承的屬性

$cacheTable 公開屬性

用於儲存快取內容的 DB 表格名稱。表格應預先建立如下

CREATE TABLE cache (
    id char(128) NOT NULL PRIMARY KEY,
    expire int(11),
    data BLOB
);

對於 MSSQL: `php CREATE TABLE cache (

id VARCHAR(128) NOT NULL PRIMARY KEY,
expire INT(11),
data VARBINARY(MAX)

); `

其中 'BLOB' 指的是您偏好的 DBMS 的 BLOB 類型。以下是一些常用 DBMS 可以使用的 BLOB 類型

  • MySQL: LONGBLOB
  • PostgreSQL: BYTEA

在生產伺服器中使用 DbCache 時,我們建議您為快取表格中的 'expire' 欄位建立 DB 索引,以提高效能。

public string $cacheTable '{{%cache}}'
$db 公開屬性

DB 連線物件或 DB 連線的應用程式組件 ID。建立 DbCache 物件後,如果您想要變更此屬性,則應僅使用 DB 連線物件指派它。從 2.0.2 版本開始,這也可以是用於建立物件的組態陣列。

$gcProbability 公開屬性

在快取中儲存資料時,應該執行垃圾收集 (GC) 的機率(百萬分率)。預設值為 100,意思是 0.01% 的機率。此數字應該介於 0 到 1000000 之間。值 0 意思是完全不執行 GC。

public integer $gcProbability 100
$isVarbinaryDataField 受保護的屬性
protected $isVarbinaryDataField null

方法詳細資訊

隱藏繼承的方法

__call() 公開方法

定義於: yii\base\Component::__call()

呼叫指定的名稱方法,該方法不是類別方法。

這個方法會檢查是否有任何附加的行為具有指定的名稱方法,如果有的話,則會執行它。

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當調用未知方法時,將會隱式呼叫它。

public mixed __call ( $name, $params )
$name 字串

方法名稱

$params array

方法參數

return mixed

方法的傳回值

throws yii\base\UnknownMethodException

當呼叫未知方法時

                public function __call($name, $params)
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $object) {
        if ($object->hasMethod($name)) {
            return call_user_func_array([$object, $name], $params);
        }
    }
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__clone() 公開方法

定義於: yii\base\Component::__clone()

在通過複製現有物件建立新物件後,將會呼叫此方法。

它會移除所有行為,因為它們被附加到舊的物件。

public void __clone ( )

                public function __clone()
{
    $this->_events = [];
    $this->_eventWildcards = [];
    $this->_behaviors = null;
}

            
__construct() 公開方法

定義於: 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();
}

            
__get() 公開方法

定義於: yii\base\Component::__get()

傳回組件屬性的值。

這個方法會依照以下順序檢查並相應地執行操作

  • 由 getter 定義的屬性:傳回 getter 結果
  • 行為的屬性:傳回行為屬性值

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $value = $component->property; 時,將會隱式呼叫它。

另請參閱 __set()

public mixed __get ( $name )
$name 字串

屬性名稱

return mixed

屬性值或行為屬性的值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性為唯寫。

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        // read property, e.g. getName()
        return $this->$getter();
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name;
        }
    }
    if (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);
}

            
__isset() 公開方法

定義於: yii\base\Component::__isset()

檢查屬性是否已設定,即已定義且非 null。

這個方法會依照以下順序檢查並相應地執行操作

  • 由 setter 定義的屬性:傳回屬性是否已設定
  • 行為的屬性:傳回屬性是否已設定
  • 對於不存在的屬性,傳回 false

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 isset($component->property) 時,將會隱式呼叫它。

另請參閱 https://php.dev.org.tw/manual/en/function.isset.php

public boolean __isset ( $name )
$name 字串

屬性名稱或事件名稱

return boolean

指定的屬性是否已設定

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canGetProperty($name)) {
            return $behavior->$name !== null;
        }
    }
    return false;
}

            
__set() 公開方法

定義於: yii\base\Component::__set()

設定組件屬性的值。

這個方法會依照以下順序檢查並相應地執行操作

  • 由 setter 定義的屬性:設定屬性值
  • 格式為 "on xyz" 的事件:將處理常式附加到事件 "xyz"
  • 格式為 "as xyz" 的行為:附加名為 "xyz" 的行為
  • 行為的屬性:設定行為屬性值

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 $component->property = $value; 時,將會隱式呼叫它。

另請參閱 __get()

public void __set ( $name, $value )
$name 字串

屬性名稱或事件名稱

$value mixed

屬性值

throws yii\base\UnknownPropertyException

如果未定義屬性

throws yii\base\InvalidCallException

如果屬性為唯讀。

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // set property
        $this->$setter($value);
        return;
    } elseif (strncmp($name, 'on ', 3) === 0) {
        // on event: attach event handler
        $this->on(trim(substr($name, 3)), $value);
        return;
    } elseif (strncmp($name, 'as ', 3) === 0) {
        // as behavior: attach behavior
        $name = trim(substr($name, 3));
        $this->attachBehavior($name, $value instanceof Behavior ? $value : Yii::createObject($value));
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = $value;
            return;
        }
    }
    if (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
}

            
__unset() 公開方法

定義於: yii\base\Component::__unset()

將組件屬性設定為 null。

這個方法會依照以下順序檢查並相應地執行操作

  • 由 setter 定義的屬性:將屬性值設定為 null
  • 行為的屬性:將屬性值設定為 null

請勿直接呼叫此方法,因為它是一個 PHP 魔術方法,當執行 unset($component->property) 時,將會隱式呼叫它。

另請參閱 https://php.dev.org.tw/manual/en/function.unset.php

public void __unset ( $name )
$name 字串

屬性名稱

throws yii\base\InvalidCallException

如果屬性為唯讀。

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
        return;
    }
    // behavior property
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $behavior) {
        if ($behavior->canSetProperty($name)) {
            $behavior->$name = null;
            return;
        }
    }
    throw new InvalidCallException('Unsetting an unknown or read-only property: ' . get_class($this) . '::' . $name);
}

            
add() 公開方法

定義於: yii\caching\Cache::add()

如果快取中不包含此鍵,則將由鍵識別的值儲存到快取中。

如果快取已經包含該鍵,則不會執行任何動作。

public boolean add ( $key, $value, $duration 0, $dependency null )
$key mixed

識別要快取的值的鍵。這可以是簡單的字串,也可以是由表示鍵的因素組成的複雜資料結構。

$value mixed

要快取的值

$duration 整數

快取值將過期的秒數。0 表示永不過期。

$dependency yii\caching\Dependency|null

快取項目的依賴性。如果依賴性變更,則在透過 get() 擷取時,快取中的相應值將失效。如果 $serializer 為 false,則會忽略此參數。

return boolean

值是否成功儲存到快取中

                public function add($key, $value, $duration = 0, $dependency = null)
{
    if ($dependency !== null && $this->serializer !== false) {
        $dependency->evaluateDependency($this);
    }
    if ($this->serializer === null) {
        $value = serialize([$value, $dependency]);
    } elseif ($this->serializer !== false) {
        $value = call_user_func($this->serializer[0], [$value, $dependency]);
    }
    $key = $this->buildKey($key);
    return $this->addValue($key, $value, $duration);
}

            
addValue() 受保護的方法

如果快取中不包含此鍵,則將由鍵識別的值儲存到快取中。

這是父類別中宣告的方法的實作。

protected boolean addValue ( $key, $value, $duration )
$key 字串

識別要快取的值的鍵

$value 字串

要快取的值。其他類型(如果您已經停用 $serializer)無法儲存。

$duration 整數

快取值將過期的秒數。0 表示永不過期。

return boolean

如果值成功儲存到快取中,則為 True,否則為 false

                protected function addValue($key, $value, $duration)
{
    $this->gc();
    try {
        $this->db->noCache(function (Connection $db) use ($key, $value, $duration) {
            $db->createCommand()
                ->insert($this->cacheTable, [
                    'id' => $key,
                    'expire' => $duration > 0 ? $duration + time() : 0,
                    'data' => $this->getDataFieldValue($value),
                ])->execute();
        });
        return true;
    } catch (\Exception $e) {
        Yii::warning("Unable to insert cache data: {$e->getMessage()}", __METHOD__);
        return false;
    }
}

            
addValues() 受保護的方法

定義於: yii\caching\Cache::addValues()

將多個鍵值對新增到快取中。

預設的實作會多次呼叫 addValue(),以逐一新增數值。如果底層快取儲存空間支援多重新增,則應該覆寫此方法以利用該功能。

protected array addValues ( $data, $duration )
$data array

陣列,其中鍵對應到快取鍵,而值是要儲存的數值。

$duration 整數

快取值將過期的秒數。0 表示永不過期。

return array

失敗的鍵陣列

                protected function addValues($data, $duration)
{
    $failedKeys = [];
    foreach ($data as $key => $value) {
        if ($this->addValue($key, $value, $duration) === false) {
            $failedKeys[] = $key;
        }
    }
    return $failedKeys;
}

            
attachBehavior() 公開方法

定義於: yii\base\Component::attachBehavior()

將行為附加到此組件。

這個方法會根據給定的組態建立行為物件。之後,將透過呼叫 yii\base\Behavior::attach() 方法將行為物件附加到此組件。

另請參閱 detachBehavior()

public yii\base\Behavior attachBehavior ( $name, $behavior )
$name 字串

行為的名稱。

$behavior string|array|yii\base\Behavior

行為組態。這可以是下列其中之一

return yii\base\Behavior

行為物件

                public function attachBehavior($name, $behavior)
{
    $this->ensureBehaviors();
    return $this->attachBehaviorInternal($name, $behavior);
}

            
attachBehaviors() 公開方法

定義於: yii\base\Component::attachBehaviors()

將行為列表附加到組件。

每個行為都由其名稱索引,並且應該是 yii\base\Behavior 物件、指定行為類別的字串,或是用於建立行為的組態陣列。

另請參閱 attachBehavior()

public void attachBehaviors ( $behaviors )
$behaviors array

要附加到組件的行為列表

                public function attachBehaviors($behaviors)
{
    $this->ensureBehaviors();
    foreach ($behaviors as $name => $behavior) {
        $this->attachBehaviorInternal($name, $behavior);
    }
}

            
behaviors() 公開方法

定義於: yii\base\Component::behaviors()

傳回此組件應表現為的行為列表。

子類別可以覆寫這個方法,以指定它們想要表現為的行為。

此方法的傳回值應該是由行為名稱索引的行為物件或組態陣列。行為組態可以是指定行為類別的字串,或是下列結構的陣列

'behaviorName' => [
    'class' => 'BehaviorClass',
    'property1' => 'value1',
    'property2' => 'value2',
]

請注意,行為類別必須從 yii\base\Behavior 擴充。行為可以使用名稱或匿名方式附加。當名稱用作陣列鍵時,使用此名稱,稍後可以使用 getBehavior() 檢索行為,或使用 detachBehavior() 分離行為。匿名行為無法檢索或分離。

在這個方法中宣告的行為將自動(依需求)附加到組件。

public array behaviors ( )
return array

行為組態。

                public function behaviors()
{
    return [];
}

            
buildKey() 公開方法

定義於: yii\caching\Cache::buildKey()

從給定的鍵建立標準化的快取鍵。

如果給定的鍵是一個僅包含字母數字字元且不超過 32 個字元的字串,則將傳回該鍵,並以前綴 $keyPrefix 作為前綴。否則,將通過序列化給定的鍵、應用 MD5 雜湊並以前綴 $keyPrefix 作為前綴來產生標準化的鍵。

public string buildKey ( $key )
$key mixed

要標準化的鍵

return 字串

產生的快取鍵

                public function buildKey($key)
{
    if (is_string($key)) {
        $key = ctype_alnum($key) && StringHelper::byteLength($key) <= 32 ? $key : md5($key);
    } else {
        if ($this->_igbinaryAvailable) {
            $serializedKey = igbinary_serialize($key);
        } else {
            $serializedKey = serialize($key);
        }
        $key = md5($serializedKey);
    }
    return $this->keyPrefix . $key;
}

            
canGetProperty() 公開方法

定義於: yii\base\Component::canGetProperty()

傳回一個值,指示屬性是否可以被讀取。

在以下情況下,屬性可以被讀取

  • 類別具有與指定名稱關聯的 getter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有指定名稱的可讀屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canSetProperty()

public boolean canGetProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此組件的屬性

return boolean

屬性是否可以被讀取

                public function canGetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canGetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
canSetProperty() 公開方法

定義於: yii\base\Component::canSetProperty()

傳回一個值,指示屬性是否可以被設定。

在以下情況下,屬性可以被寫入

  • 類別具有與指定名稱關聯的 setter 方法(在這種情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的行為具有指定名稱的可寫屬性(當 $checkBehaviors 為 true 時)。

另請參閱 canGetProperty()

public boolean canSetProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此組件的屬性

return boolean

屬性是否可以被寫入

                public function canSetProperty($name, $checkVars = true, $checkBehaviors = true)
{
    if (method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->canSetProperty($name, $checkVars)) {
                return true;
            }
        }
    }
    return false;
}

            
className() 公開靜態方法
自 2.0.14 版本起已棄用。在 PHP >=5.5 上,請改用 ::class

定義於: yii\base\BaseObject::className()

傳回此類別的完整限定名稱。

public static string className ( )
return 字串

此類別的完整限定名稱。

                public static function className()
{
    return get_called_class();
}

            
delete() 公開方法

定義於: yii\caching\Cache::delete()

從快取中刪除具有指定鍵的值。

public boolean delete ( $key )
$key mixed

識別要從快取中刪除的值的鍵。這可以是簡單的字串,也可以是由表示鍵的因素組成的複雜資料結構。

return boolean

如果在刪除過程中沒有發生錯誤

                public function delete($key)
{
    $key = $this->buildKey($key);
    return $this->deleteValue($key);
}

            
deleteValue() protected method

從快取中刪除具有指定鍵的值。這是父類別中宣告的方法的實作。

protected boolean deleteValue ( $key )
$key 字串

要刪除的值的鍵

return boolean

如果在刪除過程中沒有發生錯誤

                protected function deleteValue($key)
{
    $this->db->noCache(function (Connection $db) use ($key) {
        $db->createCommand()
            ->delete($this->cacheTable, ['id' => $key])
            ->execute();
    });
    return true;
}

            
detachBehavior() public method

定義於: yii\base\Component::detachBehavior()

從組件中分離行為。

將會調用 behavior 的 yii\base\Behavior::detach() 方法。

public yii\base\Behavior|null detachBehavior ( $name )
$name 字串

Behavior 的名稱。

return yii\base\Behavior|null

已分離的 behavior。如果 behavior 不存在則為 Null。

                public function detachBehavior($name)
{
    $this->ensureBehaviors();
    if (isset($this->_behaviors[$name])) {
        $behavior = $this->_behaviors[$name];
        unset($this->_behaviors[$name]);
        $behavior->detach();
        return $behavior;
    }
    return null;
}

            
detachBehaviors() public method

定義於: yii\base\Component::detachBehaviors()

從組件中分離所有行為。

public void detachBehaviors ( )

                public function detachBehaviors()
{
    $this->ensureBehaviors();
    foreach ($this->_behaviors as $name => $behavior) {
        $this->detachBehavior($name);
    }
}

            
ensureBehaviors() public method

定義於: yii\base\Component::ensureBehaviors()

確保在 behaviors() 中宣告的行為已附加到此組件。

public void ensureBehaviors ( )

                public function ensureBehaviors()
{
    if ($this->_behaviors === null) {
        $this->_behaviors = [];
        foreach ($this->behaviors() as $name => $behavior) {
            $this->attachBehaviorInternal($name, $behavior);
        }
    }
}

            
exists() public method

檢查快取中是否存在指定的鍵。

如果資料量很大,這會比從快取中取得值還快。請注意,此方法不會檢查與快取資料相關聯的依賴項(如果有的話)是否已變更。因此,呼叫 get() 可能會傳回 false,而 exists 則傳回 true。

public boolean exists ( $key )
$key mixed

識別快取值的鍵。這可以是簡單的字串,也可以是由代表鍵的因素組成的複雜資料結構。

return boolean

如果快取中存在值則為 True,如果快取中沒有值或已過期則為 false。

                public function exists($key)
{
    $key = $this->buildKey($key);
    $query = new Query();
    $query->select(['COUNT(*)'])
        ->from($this->cacheTable)
        ->where('[[id]] = :id AND ([[expire]] = 0 OR [[expire]] >' . time() . ')', [':id' => $key]);
    if ($this->db->enableQueryCache) {
        // temporarily disable and re-enable query caching
        $this->db->enableQueryCache = false;
        $result = $query->createCommand($this->db)->queryScalar();
        $this->db->enableQueryCache = true;
    } else {
        $result = $query->createCommand($this->db)->queryScalar();
    }
    return $result > 0;
}

            
flush() public method

定義於: yii\caching\Cache::flush()

從快取中刪除所有值。

如果快取在多個應用程式之間共用,請小心執行此操作。

public boolean flush ( )
return boolean

清除操作是否成功。

                public function flush()
{
    return $this->flushValues();
}

            
flushValues() protected method

從快取中刪除所有值。

這是父類別中宣告的方法的實作。

protected boolean flushValues ( )
return boolean

清除操作是否成功。

                protected function flushValues()
{
    $this->db->createCommand()
        ->delete($this->cacheTable)
        ->execute();
    return true;
}

            
gc() public method

移除過期的資料值。

public void gc ( $force false )
$force boolean

是否強制執行垃圾回收,無論 $gcProbability 為何。預設為 false,表示實際刪除會根據 $gcProbability 指定的機率發生。

                public function gc($force = false)
{
    if ($force || random_int(0, 1000000) < $this->gcProbability) {
        $this->db->createCommand()
            ->delete($this->cacheTable, '[[expire]] > 0 AND [[expire]] < ' . time())
            ->execute();
    }
}

            
get() public method

定義於: yii\caching\Cache::get()

使用指定的鍵從快取中檢索值。

public mixed get ( $key )
$key mixed

識別快取值的鍵。這可以是簡單的字串,也可以是由代表鍵的因素組成的複雜資料結構。

return mixed

快取中儲存的值;如果快取中沒有值、已過期,或與快取資料相關聯的依賴項已變更,則為 false。

                public function get($key)
{
    $key = $this->buildKey($key);
    $value = $this->getValue($key);
    if ($value === false || $this->serializer === false) {
        return $value;
    } elseif ($this->serializer === null) {
        $value = unserialize((string)$value);
    } else {
        $value = call_user_func($this->serializer[1], $value);
    }
    if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->isChanged($this))) {
        return $value[0];
    }
    return false;
}

            
getBehavior() public method

定義於: yii\base\Component::getBehavior()

傳回指定的行為物件。

public yii\base\Behavior|null getBehavior ( $name )
$name 字串

Behavior 名稱

return yii\base\Behavior|null

Behavior 物件;如果 behavior 不存在則為 null

                public function getBehavior($name)
{
    $this->ensureBehaviors();
    return isset($this->_behaviors[$name]) ? $this->_behaviors[$name] : null;
}

            
getBehaviors() public method

定義於: yii\base\Component::getBehaviors()

傳回附加到此組件的所有行為。

public yii\base\Behavior[] getBehaviors ( )
return yii\base\Behavior[]

附加到此組件的 behavior 列表

                public function getBehaviors()
{
    $this->ensureBehaviors();
    return $this->_behaviors;
}

            
getDataFieldName() protected method (available since version 2.0.42)

protected string getDataFieldName ( )
return 字串

轉換後的 data 欄位名稱,用於 MSSQL (如果需要)

                protected function getDataFieldName()
{
    return $this->isVarbinaryDataField() ? 'CONVERT(VARCHAR(MAX), [[data]]) data' : 'data';
}

            
getDataFieldValue() protected method (available since version 2.0.42)

protected yii\db\PdoValue getDataFieldValue ( $value )
$value
return yii\db\PdoValue

用於 MSSQL 的 PdoValue 或直接 $value

                protected function getDataFieldValue($value)
{
    return $this->isVarbinaryDataField() ? $value : new PdoValue($value, \PDO::PARAM_LOB);
}

            
getOrSet() public method (available since version 2.0.11)

定義於: yii\caching\Cache::getOrSet()

此方法結合了 set()get() 方法,以檢索由 $key 識別的值,或者在 $key 沒有可用的快取時,儲存 $callable 執行的結果。

使用範例

public function getTopProducts($count = 10) {
    $cache = $this->cache; // Could be Yii::$app->cache
    return $cache->getOrSet(['top-n-products', 'n' => $count], function () use ($count) {
        return Products::find()->mostPopular()->limit($count)->all();
    }, 1000);
}
public mixed getOrSet ( $key, $callable, $duration null, $dependency null )
$key mixed

識別要快取的值的鍵。這可以是簡單的字串,也可以是由表示鍵的因素組成的複雜資料結構。

$callable callable|Closure

將用於產生要快取的值的可呼叫或匿名函式。如果您使用的 $callable 可能會傳回 false,請記住 getOrSet() 可能會運作效率不彰,因為 yii\caching\Cache::get() 方法使用 false 傳回值來表示在快取中找不到資料項目。因此,快取 false 值會導致不必要的內部呼叫。

$duration integer|null

快取到期前的預設持續時間(秒)。如果未設定,將會使用 $defaultDuration 值。

$dependency yii\caching\Dependency|null

快取項目的依賴項。如果依賴項變更,則在透過 get() 擷取時,快取中的對應值將會失效。如果 $serializerfalse,則會忽略此參數。

return mixed

$callable 執行的結果

                public function getOrSet($key, $callable, $duration = null, $dependency = null)
{
    if (($value = $this->get($key)) !== false) {
        return $value;
    }
    $value = call_user_func($callable, $this);
    if (!$this->set($key, $value, $duration, $dependency)) {
        Yii::warning('Failed to set cache value for key ' . json_encode($key), __METHOD__);
    }
    return $value;
}

            
getValue() protected method

使用指定的鍵從快取中檢索值。

這是父類別中宣告的方法的實作。

protected string|false getValue ( $key )
$key 字串

識別快取值的唯一鍵

return string|false

快取中儲存的值;如果快取中沒有值或已過期,則為 false。

                protected function getValue($key)
{
    $query = new Query();
    $query->select([$this->getDataFieldName()])
        ->from($this->cacheTable)
        ->where('[[id]] = :id AND ([[expire]] = 0 OR [[expire]] >' . time() . ')', [':id' => $key]);
    if ($this->db->enableQueryCache) {
        // temporarily disable and re-enable query caching
        $this->db->enableQueryCache = false;
        $result = $query->createCommand($this->db)->queryScalar();
        $this->db->enableQueryCache = true;
        return $result;
    }
    return $query->createCommand($this->db)->queryScalar();
}

            
getValues() protected method

使用指定的鍵從快取中檢索多個值。

protected array getValues ( $keys )
$keys array

識別快取值的鍵列表

return array

由鍵索引的快取值列表

                protected function getValues($keys)
{
    if (empty($keys)) {
        return [];
    }
    $query = new Query();
    $query->select(['id', $this->getDataFieldName()])
        ->from($this->cacheTable)
        ->where(['id' => $keys])
        ->andWhere('([[expire]] = 0 OR [[expire]] > ' . time() . ')');
    if ($this->db->enableQueryCache) {
        $this->db->enableQueryCache = false;
        $rows = $query->createCommand($this->db)->queryAll();
        $this->db->enableQueryCache = true;
    } else {
        $rows = $query->createCommand($this->db)->queryAll();
    }
    $results = [];
    foreach ($keys as $key) {
        $results[$key] = false;
    }
    foreach ($rows as $row) {
        if (is_resource($row['data']) && get_resource_type($row['data']) === 'stream') {
            $results[$row['id']] = stream_get_contents($row['data']);
        } else {
            $results[$row['id']] = $row['data'];
        }
    }
    return $results;
}

            
hasEventHandlers() public method

定義於: yii\base\Component::hasEventHandlers()

傳回一個值,指示是否有名稱事件附加任何處理常式。

public boolean hasEventHandlers ( $name )
$name 字串

事件名稱

return boolean

是否有任何處理常式附加到事件。

                public function hasEventHandlers($name)
{
    $this->ensureBehaviors();
    if (!empty($this->_events[$name])) {
        return true;
    }
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
            return true;
        }
    }
    return Event::hasHandlers($this, $name);
}

            
hasMethod() public method

定義於: yii\base\Component::hasMethod()

傳回一個值,指示是否已定義方法。

如果符合以下條件,則定義方法

  • 類別具有指定名稱的方法
  • 附加的 behavior 具有給定名稱的方法 (當 $checkBehaviors 為 true 時)。
public boolean hasMethod ( $name, $checkBehaviors true )
$name 字串

屬性名稱

$checkBehaviors boolean

是否將 behavior 的方法視為此組件的方法

return boolean

方法是否已定義

                public function hasMethod($name, $checkBehaviors = true)
{
    if (method_exists($this, $name)) {
        return true;
    } elseif ($checkBehaviors) {
        $this->ensureBehaviors();
        foreach ($this->_behaviors as $behavior) {
            if ($behavior->hasMethod($name)) {
                return true;
            }
        }
    }
    return false;
}

            
hasProperty() public method

定義於: yii\base\Component::hasProperty()

傳回一個值,指示是否為此組件定義了屬性。

如果符合以下條件,則定義屬性

  • 類別具有與指定名稱相關聯的 getter 或 setter 方法 (在此情況下,屬性名稱不區分大小寫);
  • 類別具有具有指定名稱的成員變數(當 $checkVars 為 true 時);
  • 附加的 behavior 具有給定名稱的屬性 (當 $checkBehaviors 為 true 時)。

另請參閱

public boolean hasProperty ( $name, $checkVars true, $checkBehaviors true )
$name 字串

屬性名稱

$checkVars boolean

是否將成員變數視為屬性

$checkBehaviors boolean

是否將行為的屬性視為此組件的屬性

return boolean

屬性是否已定義

                public function hasProperty($name, $checkVars = true, $checkBehaviors = true)
{
    return $this->canGetProperty($name, $checkVars, $checkBehaviors) || $this->canSetProperty($name, false, $checkBehaviors);
}

            
init() public method

初始化 DbCache 組件。

此方法將初始化 $db 屬性,以確保它參考有效的資料庫連線。

public void init ( )
throws yii\base\InvalidConfigException

如果 $db 無效。

                public function init()
{
    parent::init();
    $this->db = Instance::ensure($this->db, Connection::className());
}

            
isVarbinaryDataField() protected method (available since version 2.0.42)

protected boolean isVarbinaryDataField ( )
return boolean

欄位是否為 MSSQL varbinary

                protected function isVarbinaryDataField()
{
    if ($this->isVarbinaryDataField === null) {
        $this->isVarbinaryDataField = in_array($this->db->getDriverName(), ['sqlsrv', 'dblib']) &&
            $this->db->getTableSchema($this->cacheTable)->columns['data']->dbType === 'varbinary';
    }
    return $this->isVarbinaryDataField;
}

            
madd() public method
已過時。此方法是 multiAdd() 的別名,將在 2.1.0 版中移除。

定義於: yii\caching\Cache::madd()

在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。

如果快取已包含此鍵,則現有的值和到期時間將會保留。

public array madd ( $items, $duration 0, $dependency null )
$items array

要快取的項目,以鍵值對的形式。

$duration 整數

快取值到期的預設秒數。0 表示永不過期。

$dependency yii\caching\Dependency|null

快取項目的依賴項。如果依賴項變更,則在透過 get() 擷取時,快取中的對應值將會失效。如果 $serializer 為 false,則會忽略此參數。

return array

失敗的鍵陣列

                public function madd($items, $duration = 0, $dependency = null)
{
    return $this->multiAdd($items, $duration, $dependency);
}

            
mget() public method
已過時。此方法是 multiGet() 的別名,將在 2.1.0 版中移除。

定義於: yii\caching\Cache::mget()

使用指定的鍵從快取中檢索多個值。

某些快取 (例如 memcache、apc) 允許同時擷取多個快取值,這可能會提高效能。如果快取本身不支援此功能,則此方法將嘗試模擬它。

public array mget ( $keys )
$keys 字串[]

識別快取值的字串鍵列表

return array

對應於指定鍵的快取值列表。陣列以 (鍵,值) 對的形式傳回。如果值未快取或已過期,則對應的陣列值將為 false。

                public function mget($keys)
{
    return $this->multiGet($keys);
}

            
mset() public method
已過時。此方法是 multiSet() 的別名,將在 2.1.0 版中移除。

定義於: yii\caching\Cache::mset()

在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。

如果快取已包含此鍵,則現有的值和到期時間將分別替換為新的值和到期時間。

public array mset ( $items, $duration null, $dependency null )
$items array

要快取的項目,以鍵值對的形式。

$duration integer|null

快取到期前的預設持續時間(秒)。如果未設定,則使用預設的 $defaultDuration 值。

$dependency yii\caching\Dependency|null

快取項目的依賴項。如果依賴項變更,則在透過 get() 擷取時,快取中的對應值將會失效。如果 $serializer 為 false,則會忽略此參數。

return array

失敗的鍵陣列

                public function mset($items, $duration = null, $dependency = null)
{
    return $this->multiSet($items, $duration, $dependency);
}

            
multiAdd() public method (available since version 2.0.7)

定義於: yii\caching\Cache::multiAdd()

在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。

如果快取已包含此鍵,則現有的值和到期時間將會保留。

public array multiAdd ( $items, $duration 0, $dependency null )
$items array

要快取的項目,以鍵值對的形式。

$duration 整數

快取值到期的預設秒數。0 表示永不過期。

$dependency yii\caching\Dependency|null

快取項目的依賴項。如果依賴項變更,則在透過 get() 擷取時,快取中的對應值將會失效。如果 $serializer 為 false,則會忽略此參數。

return array

失敗的鍵陣列

                public function multiAdd($items, $duration = 0, $dependency = null)
{
    $data = $this->prepareCacheData($items, $dependency);
    return $this->addValues($data, $duration);
}

            
multiGet() public method (available since version 2.0.7)

定義於: yii\caching\Cache::multiGet()

使用指定的鍵從快取中檢索多個值。

某些快取 (例如 memcache、apc) 允許同時擷取多個快取值,這可能會提高效能。如果快取本身不支援此功能,則此方法將嘗試模擬它。

public array multiGet ( $keys )
$keys 字串[]

識別快取值的字串鍵列表

return array

對應於指定鍵的快取值列表。陣列以 (鍵,值) 對的形式傳回。如果值未快取或已過期,則對應的陣列值將為 false。

                public function multiGet($keys)
{
    $keyMap = [];
    foreach ($keys as $key) {
        $keyMap[$key] = $this->buildKey($key);
    }
    $values = $this->getValues(array_values($keyMap));
    $results = [];
    foreach ($keyMap as $key => $newKey) {
        $results[$key] = false;
        if (isset($values[$newKey])) {
            if ($this->serializer === false) {
                $results[$key] = $values[$newKey];
            } else {
                $value = $this->serializer === null ? unserialize($values[$newKey])
                    : call_user_func($this->serializer[1], $values[$newKey]);
                if (is_array($value) && !($value[1] instanceof Dependency && $value[1]->isChanged($this))) {
                    $results[$key] = $value[0];
                }
            }
        }
    }
    return $results;
}

            
multiSet() public method (available since version 2.0.7)

定義於: yii\caching\Cache::multiSet()

在快取中儲存多個項目。每個項目都包含一個由鍵識別的值。

如果快取已包含此鍵,則現有的值和到期時間將分別替換為新的值和到期時間。

public array multiSet ( $items, $duration null, $dependency null )
$items array

要快取的項目,以鍵值對的形式。

$duration integer|null

快取到期前的預設持續時間(秒)。如果未設定,則使用預設的 $defaultDuration 值。

$dependency yii\caching\Dependency|null

快取項目的依賴項。如果依賴項變更,則在透過 get() 擷取時,快取中的對應值將會失效。如果 $serializer 為 false,則會忽略此參數。

return array

失敗的鍵陣列

                public function multiSet($items, $duration = null, $dependency = null)
{
    if ($duration === null) {
        $duration = $this->defaultDuration;
    }
    $data = $this->prepareCacheData($items, $dependency);
    return $this->setValues($data, $duration);
}

            
off() public method

定義於: yii\base\Component::off()

從此組件分離現有的事件處理常式。

此方法與 on() 相反。

注意:如果為事件名稱傳遞了萬用字元模式,則只會移除使用此萬用字元註冊的處理常式,而使用符合此萬用字元的純名稱註冊的處理常式將會保留。

另請參閱 on()

public boolean off ( $name, $handler null )
$name 字串

事件名稱

$handler callable|null

要移除的事件處理常式。如果為 null,則會移除附加到具名事件的所有處理常式。

return boolean

如果找到並分離處理常式

                public function off($name, $handler = null)
{
    $this->ensureBehaviors();
    if (empty($this->_events[$name]) && empty($this->_eventWildcards[$name])) {
        return false;
    }
    if ($handler === null) {
        unset($this->_events[$name], $this->_eventWildcards[$name]);
        return true;
    }
    $removed = false;
    // plain event names
    if (isset($this->_events[$name])) {
        foreach ($this->_events[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_events[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_events[$name] = array_values($this->_events[$name]);
            return true;
        }
    }
    // wildcard event names
    if (isset($this->_eventWildcards[$name])) {
        foreach ($this->_eventWildcards[$name] as $i => $event) {
            if ($event[0] === $handler) {
                unset($this->_eventWildcards[$name][$i]);
                $removed = true;
            }
        }
        if ($removed) {
            $this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
            // remove empty wildcards to save future redundant regex checks:
            if (empty($this->_eventWildcards[$name])) {
                unset($this->_eventWildcards[$name]);
            }
        }
    }
    return $removed;
}

            
offsetExists() public method

定義於: yii\caching\Cache::offsetExists()

傳回是否具有指定鍵的快取條目。

此方法為介面 ArrayAccess 所需。

public boolean offsetExists ( $key )
$key 字串

識別快取值的鍵

                #[\ReturnTypeWillChange]
public function offsetExists($key)
{
    return $this->get($key) !== false;
}

            
offsetGet() public method

定義於: yii\caching\Cache::offsetGet()

使用指定的鍵從快取中檢索值。

此方法為介面 ArrayAccess 所需。

public mixed offsetGet ( $key )
$key 字串

識別快取值的鍵

return mixed

快取中儲存的值;如果快取中沒有值或已過期,則為 false。

                #[\ReturnTypeWillChange]
public function offsetGet($key)
{
    return $this->get($key);
}

            
offsetSet() public method

定義於: yii\caching\Cache::offsetSet()

將由鍵識別的值儲存到快取中。

如果快取已包含此鍵,則現有的值將替換為新的值。若要新增到期時間和依賴項,請使用 set() 方法。此方法為介面 ArrayAccess 所需。

public void offsetSet ( $key, $value )
$key 字串

識別要快取的值的鍵

$value mixed

要快取的值

                #[\ReturnTypeWillChange]
public function offsetSet($key, $value)
{
    $this->set($key, $value);
}

            
offsetUnset() public method

定義於: yii\caching\Cache::offsetUnset()

從快取中刪除具有指定鍵的值。此方法是 ArrayAccess 介面所要求的。

public void offsetUnset ( $key )
$key 字串

要刪除的值的鍵

                #[\ReturnTypeWillChange]
public function offsetUnset($key)
{
    $this->delete($key);
}

            
on() public method

定義於: yii\base\Component::on()

將事件處理常式附加到事件。

事件處理常式必須是有效的 PHP 回呼。以下是一些範例

function ($event) { ... }         // anonymous function
[$object, 'handleClick']          // $object->handleClick()
['Page', 'handleClick']           // Page::handleClick()
'handleClick'                     // global function handleClick()

事件處理常式必須使用以下簽章定義:

function ($event)

其中 $event 是一個 yii\base\Event 物件,其中包含與事件相關聯的參數。

從 2.0.14 版本開始,您可以將事件名稱指定為萬用字元模式

$component->on('event.group.*', function ($event) {
    Yii::trace($event->name . ' is triggered.');
});

另請參閱 off()

public void on ( $name, $handler, $data null, $append true )
$name 字串

事件名稱

$handler 可呼叫

事件處理器

$data mixed

當事件觸發時,要傳遞給事件處理器的資料。當事件處理器被調用時,可以透過 yii\base\Event::$data 存取此資料。

$append boolean

是否將新的事件處理器附加到現有處理器列表的末尾。如果為 false,新的處理器將會被插入到現有處理器列表的開頭。

                public function on($name, $handler, $data = null, $append = true)
{
    $this->ensureBehaviors();
    if (strpos($name, '*') !== false) {
        if ($append || empty($this->_eventWildcards[$name])) {
            $this->_eventWildcards[$name][] = [$handler, $data];
        } else {
            array_unshift($this->_eventWildcards[$name], [$handler, $data]);
        }
        return;
    }
    if ($append || empty($this->_events[$name])) {
        $this->_events[$name][] = [$handler, $data];
    } else {
        array_unshift($this->_events[$name], [$handler, $data]);
    }
}

            
set() 公開方法

定義於: yii\caching\Cache::set()

將由鍵識別的值儲存到快取中。

如果快取已包含此鍵,則現有的值和到期時間將分別替換為新的值和到期時間。

public boolean set ( $key, $value, $duration null, $dependency null )
$key mixed

識別要快取的值的鍵。這可以是簡單的字串,也可以是由表示鍵的因素組成的複雜資料結構。

$value mixed

要快取的值

$duration integer|null

快取到期前的預設持續時間(秒)。如果未設定,則使用預設的 $defaultDuration 值。

$dependency yii\caching\Dependency|null

快取項目的依賴性。如果依賴性變更,則在透過 get() 擷取時,快取中的相應值將失效。如果 $serializer 為 false,則會忽略此參數。

return boolean

值是否成功儲存到快取中

                public function set($key, $value, $duration = null, $dependency = null)
{
    if ($duration === null) {
        $duration = $this->defaultDuration;
    }
    if ($dependency !== null && $this->serializer !== false) {
        $dependency->evaluateDependency($this);
    }
    if ($this->serializer === null) {
        $value = serialize([$value, $dependency]);
    } elseif ($this->serializer !== false) {
        $value = call_user_func($this->serializer[0], [$value, $dependency]);
    }
    $key = $this->buildKey($key);
    return $this->setValue($key, $value, $duration);
}

            
setValue() 受保護方法

將由鍵識別的值儲存在快取中。

這是父類別中宣告的方法的實作。

protected boolean setValue ( $key, $value, $duration )
$key 字串

識別要快取的值的鍵

$value 字串

要快取的值。其他類型(如果您已經停用 $serializer)無法儲存。

$duration 整數

快取值將過期的秒數。0 表示永不過期。

return boolean

如果值成功儲存到快取中,則為 True,否則為 false

                protected function setValue($key, $value, $duration)
{
    try {
        $this->db->noCache(function (Connection $db) use ($key, $value, $duration) {
            $db->createCommand()->upsert($this->cacheTable, [
                'id' => $key,
                'expire' => $duration > 0 ? $duration + time() : 0,
                'data' => $this->getDataFieldValue($value),
            ])->execute();
        });
        $this->gc();
        return true;
    } catch (\Exception $e) {
        Yii::warning("Unable to update or insert cache data: {$e->getMessage()}", __METHOD__);
        return false;
    }
}

            
setValues() 受保護方法

定義於: yii\caching\Cache::setValues()

在快取中儲存多個鍵值對。

預設的實作會多次呼叫 setValue() 以逐個儲存值。如果底層快取儲存支援多重設定,則應該覆寫此方法以利用該功能。

protected array setValues ( $data, $duration )
$data array

陣列,其中鍵對應到快取鍵,而值是要儲存的值

$duration 整數

快取值將過期的秒數。0 表示永不過期。

return array

失敗的鍵陣列

                protected function setValues($data, $duration)
{
    $failedKeys = [];
    foreach ($data as $key => $value) {
        if ($this->setValue($key, $value, $duration) === false) {
            $failedKeys[] = $key;
        }
    }
    return $failedKeys;
}

            
trigger() 公開方法

定義於: yii\base\Component::trigger()

觸發事件。

此方法表示事件的發生。它會調用所有附加到事件的處理器,包括類別層級的處理器。

public void trigger ( $name, yii\base\Event $event null )
$name 字串

事件名稱

$event yii\base\Event|null

事件實例。如果未設定,將會建立預設的 yii\base\Event 物件。

                public function trigger($name, Event $event = null)
{
    $this->ensureBehaviors();
    $eventHandlers = [];
    foreach ($this->_eventWildcards as $wildcard => $handlers) {
        if (StringHelper::matchWildcard($wildcard, $name)) {
            $eventHandlers[] = $handlers;
        }
    }
    if (!empty($this->_events[$name])) {
        $eventHandlers[] = $this->_events[$name];
    }
    if (!empty($eventHandlers)) {
        $eventHandlers = call_user_func_array('array_merge', $eventHandlers);
        if ($event === null) {
            $event = new Event();
        }
        if ($event->sender === null) {
            $event->sender = $this;
        }
        $event->handled = false;
        $event->name = $name;
        foreach ($eventHandlers as $handler) {
            $event->data = $handler[1];
            call_user_func($handler[0], $event);
            // stop further handling if the event is handled
            if ($event->handled) {
                return;
            }
        }
    }
    // invoke class-level attached handlers
    Event::trigger($this, $name, $event);
}