php之yii快取學習筆記

2021-07-16 19:11:40 字數 3064 閱讀 2194

php之yii快取學習筆記

其中yii支援   1.資料快取

2.片段快取

3.頁面快取

4.http快取

1.資料快取:資料快取是指將一些 php 變數儲存到快取中,使用時再從快取中取回。 它也是更高階快取特性的基礎,例如查詢快取 和內容快取。

(1)讀取快取

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

(2)相同屬性重複新增時

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

(3)修改快取

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

(4)刪除快取

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

(5)設定、修改快取的有效期

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

資料快取中依賴關係詳解

(1)檔案的依賴

class hellocontroller extends controller

}

(2)表示式依賴

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}(3)db依賴 資料庫的依賴

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

2.片段快取:片段快取指的是快取頁面內容中的某個片段。例如,乙個頁面顯示了逐年銷售額的摘要**, 可以把**快取下來,以消除每次請求都要重新生成** 的耗時。片段快取是基於資料快取實現的。

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

}

<?php $this->beginblock('block1');?>

<?php $this->endblock();?>

3.頁面快取:頁面快取指的是在伺服器端快取整個頁面的內容。隨後當同乙個頁面 被請求時,內容將從快取中取出,而不是重新生成。頁面快取由 yii\filters\pagecache 類提供支援,該類是乙個 過濾器。它可以像這樣在控制器類中使用:

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

public function actionindex()

}

頁面快取和片段快取極其相似。它們都支援 duration,dependencies, variations 和 enabled 配置選項。它們的主要區別是頁面快取是由過濾器實現, 而片段快取則是乙個小部件。

4.http快取:通過配置 yii\filters\httpcache 過濾器,控制器操作渲染的內容就能 快取在客戶端。yii\filters\httpcache 過濾器僅對 get 和 head 請求生效,它能為這些請求設定三種與快取有關的 http 頭。

通過配置 yii\filters\httpcache::lastmodified 屬性向客戶端傳送 last-modified 頭。該屬性的值應該為 php callable 型別,返回的是頁 面修改時的 unix 時間戳。該 callable 的引數和返回值應該如下:

<?php

use yii\web\controller;

/** * qishuixian 2016.8.9

*/class hellocontroller extends controller

, ],

];} //http快取簡介

public function actionindex()

}

2.

YII之片段快取

yii的片段快取應用的話挺簡單的 1,在配置檔案裡開啟快取 man.php cache array class system.caching.cfilecache 或者這樣 只是不同的書寫格式而已 cache class yii caching filecache 2,接下來就是使用快取了 直接在v...

yii1 1 x學習筆記(3)快取

1 片段快取 在view中的快取html片段起始新增 begincache cachename array duration 3600,設定快取過期時間 vartbyparam array param 設定變化快取,生成不同的快取 class ststem.caaching.dependencies...

Yii學習筆記

1 執行環境檢測,yii提供了檢測工具 requirements index.php 2 使用yiic生成應用程式 下面的樹圖描述了我們這個應用的目錄結構。請檢視約定以獲取該結構的詳細解釋。testdrive index.php web 應用入口指令碼檔案 index test.php 功能測試使用...