Yii2 缓存

参考:http://www.yiiframework.com/doc-2.0/guide-caching-overview.html

配置

'components' => [
	'cache' => [
            'class' => 'yii\caching\FileCache',
        ],

数据缓存

$cache = \Yii::$app->cache;
$cache['aa'] = '123';
echo $cache['aa'];

片段缓存

<h1>朝代</h1>
<?php if($this->beginCache($id, ['duration' => 3600])) { ?>
<?php
echo ListView::widget([
	'dataProvider' => $dataProvider,
	'itemView' => '_item',
]);
?>
<?php $this->endCache(); } ?>

$dependency = [
    'class' => 'yii\caching\DbDependency',
    'sql' => 'SELECT MAX(updated_at) FROM post',
];

if ($this->beginCache($id, ['dependency' => $dependency])) {

    // ... generate content here ...

    $this->endCache();
}

if ($this->beginCache($id, ['variations' => [Yii::$app->language]])) {

    // ... generate content here ...

    $this->endCache();
}

if ($this->beginCache($id1)) {

    // ...content generation logic...

    if ($this->beginCache($id2, $options2)) {

        // ...content generation logic...

        $this->endCache();
    }

    // ...content generation logic...

    $this->endCache();
}

页面缓存

class TestController extends Controller
{
    public function actionIndex()
    {
		//$db = \Yii::$app->db;
		sleep(2);
		$query = Dynasty::find();
		$dataProvider = new ActiveDataProvider([
							'query' => $query,
							'pagination' => [
								'pageSize' => 15,
							],
						]);
		return $this->render('index', [
            'dataProvider' => $dataProvider
        ]);
    }
	public function behaviors()
	{
		return [
			[
				'class' => 'yii\filters\PageCache',
				'duration' => 60,
                                'variations'=> [$_GET['page']],
			],
		];
	}
}

动态内容

...别的HTML内容...
<?php if($this->beginCache($id)) { ?>
...被缓存的片段内容...
    <?php $this->renderDynamic($callback); ?>
...被缓存的片段内容...
<?php $this->endCache(); } ?>
...别的HTML内容...


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值