準備資料庫
-
在終端運行 mysql -u root –p
-
登錄數據後,通過執行 CREATE DATABASE mystudy CHARACTER SET utf8 COLLATE utf8_general_ci; 創建一個新的資料庫;
<?php
return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host = localhost;dbname = mystudy',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
];
?>
第3步 - 在專案根檔夾執行:yii migrate/create test_table 。此命令將用於創建管理資料庫資料庫遷移。 migrations檔會出現在專案的根的 migrations 檔夾中。
<?php
use yii\db\Schema;
use yii\db\Migration;
class m160529_014611_test_table extends Migration {
public function up() {
$this->createTable("user", [
"id" => Schema::TYPE_PK,
"name" => Schema::TYPE_STRING,
"email" => Schema::TYPE_STRING,
]);
$this->batchInsert("user", ["name", "email"], [
["User1", "user11@gmail.com"],
["User2", "user22@gmail.com"],
["User3", "user33@gmail.com"],
["User4", "user44@gmail.com"],
["User5", "user55@gmail.com"],
["User6", "user66@gmail.com"],
["User7", "user77@gmail.com"],
["User8", "user88@gmail.com"],
["User9", "user99@gmail.com"],
["User10", "user1010@gmail.com"],
["User11", "user1111@gmail.com"],
]);
}
public function down() {
//$this->dropTable('user');
}
}
?>

第6步-現在,我們需要為user表創建模型。為了簡便起見,我們將使用GII代碼生成工具。在流覽器中打開 url: http://localhost:8080/index.php?r=gii 。
然後,點擊 “Model generator” 下的 “Start”按鈕。 填寫表名(“user”)和模型類(“MyUser”),單擊“Preview”按鈕,最後點擊 “Generate” 按鈕。


MyUser 檔憶經生成在 models 目錄。
數據緩存
-
yii\caching\DbCache − 使用一個資料庫表來存儲緩存數據
在yii\caching\DbCache::$cacheTable 必須指定創建一個表 -
yii\caching\ApcCache − 使用 PHP APC 擴展
-
yii\caching\FileCache − 使用檔來存儲緩存數據
-
yii\caching\DummyCache − 作為高速緩存占位其中確實沒有真正的緩存
這個組件的目的是為了簡化檢查高速緩衝記憶體的可用性的代碼。 -
yii\caching\MemCache − 使用 PHP memcache 擴展
-
yii\caching\WinCache − 使用PHP WinCache 擴展
-
yii\redis\Cache − 實現了基於Redis的資料庫緩存組件
-
yii\caching\XCache −使用 PHP XCache 擴展
-
get() − 從緩存中檢索指定鍵對應的數據值。如果數據值已過期/無效或者沒有找到值,則將返回 false
-
add() − 如果該鍵沒有在高速緩存中找到,則保存到緩存該鍵和對應的數據值
-
set() − 保存到緩存的鍵識別對應數據值
-
multiGet() − 從緩存中使用指定多個鍵檢索對應多個數據值
-
multiAdd() − 在高速緩存中存儲多個數據值。每個項由一個鍵標識。如果一個鍵在緩存中已經存在,則數據值將會被跳過。
-
multiSet() − 在高速緩存中存儲多個數據值。每個專案由一個鍵來標識
-
exists() − 返回在緩存指定鍵是否找到對應值
-
flush() − 從緩存中刪除所有的數據值
-
delete() − 通過從緩存中的關鍵識別移除對應數據值
-
yii\caching\DbDependency − 如果指定SQL語句的查詢結果發生改變,依賴也會發生改變。
-
yii\caching\ChainedDependency − 如果鏈上的依賴關係發生改變,依賴也會改變。
-
yii\caching\FileDependency − 如果檔的最後修改時間發生改變,依賴也會改變。
-
yii\caching\ExpressionDependency − 如果指定PHP運算式的結果發生改變,依賴也會改變。
<?php
$params = require(__DIR__ . '/params.php');
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this
//is required by cookie validation
'cookieValidationKey' => 'xuhuhu.com',
],
'cache' => [
'class' => 'yii\caching\FileCache',
],
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'mailer' => [
'class' => 'yii\swiftmailer\Mailer',
// send all mails to a file by default. You have to set
// 'useFileTransport' to false and configure a transport
// for the mailer to send real emails.
'useFileTransport' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'db' => require(__DIR__ . '/db.php'),
],
'modules' => [
'admin' => [
'class' => 'app\modules\admin\Admin',
],
],
'params' => $params,
];
if (YII_ENV_DEV) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'class' => 'yii\debug\Module',
];
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = [
'class' => 'yii\gii\Module',
];
}
return $config;
?>
public function actionTestCache() {
$cache = Yii::$app->cache;
// try retrieving $data from cache
$data = $cache->get("my_cached_data");
if ($data === false) {
// $data is not found in cache, calculate it from scratch
$data = date("Y-m-d H:i:s");
// store $data in cache so that it can be retrieved next time
$cache->set("my_cached_data", $data, 30);
}
// $data is available here
var_dump($data);
}

查詢緩存
public function actionQueryCaching() {
$duration = 10;
$result = MyUser::getDb()->cache(function ($db) {
return MyUser::find()->count();
}, $duration);
var_dump($result);
$user = new MyUser();
$user->name = "cached user name";
$user->email = "cacheduseremail@gmail.com";
$user->save();
echo "==========";
var_dump(MyUser::find()->count());
}
-
yii cache − 顯示可用緩存組件
-
yii cache/flush cache1 cache2 cache3 −刷新緩存組件:cache1,cache2 和 cache3
-
yii cache/flush-all − 刷新所有緩存組件



