缓存机制和Url美化

jianfly.com 2019-01-31 1856次浏览

web.php配置 默认文件缓存

使用redis缓存

'cache' => [
'class' => 'yii\redis\Cache',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => '2',
],
],

CommonController中设置缓存

public function init()
{
//菜单缓存
$cache= Yii::$app->cache;  //拿到cache对象
$key = 'menu';  //设置key
if(!$menu = $cache->get($key)) {  //判断redis中有没有数据
$menu = Category::getMenu();  //进行查询
$cache->set($key, $menu, 3600*2);  //缓存2小时
}
$this->view->params['menu'] = $menu;

//购物车缓存
$key = "cart";  //指定key
if(!$data = $cache->get($key)) {  //如果从缓存中获取不到
$data = [];
$data['products'] = [];
$total = 0;
$userid= Yii::$app->user->id;
$carts = Cart::find()->where('userid = :uid', [':uid' => $userid])->asArray()->all();
foreach($carts as $k=>$pro) {
$product = Product::find()->where('productid = :pid', [':pid' => $pro['productid']])->one();
$data['products'][$k]['cover'] = $product->cover;
$data['products'][$k]['title'] = $product->title;
$data['products'][$k]['productnum'] = $product->productnum;
$data['products'][$k]['price'] = $product->price;
$data['products'][$k]['productid'] = $product->productid;
$data['products'][$k]['cartid'] = $product->cartid;
$total += $data['products'][$k]['price'] * $data['products'][$k]['productnum'];
}
$data['total'] = $total;  //查询拼接数据结束
$dep = new \yii\caching\DbDependency([
'sql' => 'seelct max(updatetime) from {{%cart}} where userid = :uid',
'params'=> [':uid' => Yii::$app->user->id],
]);  //依赖对象
$cache->set($key, $data, 60, $dep);  //缓存60s,依赖
$this->view->params['cart'] = $data;

$dep = new \yii\caching\DbDenpendency([
'sql' => 'select max(updatetime) from {{%product}} where ison = "1" ',
]);
//对商品做  查询缓存
$tui = Product::getDb()->cache(function(){
return Product::find()->where('istui = "1" and ison = "1" ')->orderby('createtime desc')->limit(3)->all();
}, 60, $dep);
$new= Product::getDb()->cache(function(){
return Product::find()->where(' ison = "1" ')->orderby('createtime desc')->limit(3)->all();
}, 60, $dep);
$hot= Product::getDb()->cache(function(){
return Product::find()->where('ishot = "1" and ison = "1" ')->orderby('createtime desc')->limit(3)->all();
}, 60, $dep);
$sale= Product::getDb()->cache(function(){
return Product::find()->where('issale = "1" and ison = "1" ')->orderby('createtime desc')->limit(3)->all();
}, 60, $dep);
$this->view->params['tui'] = (array)$tui;
$this->view->params['new'] = (array)$new;
$this->view->params['hot'] = (array)$hot;
$this->view->params['sale'] = (array)$sale;
}
}

session存储到redis中

web.php中配置

'session' => [
'class' => 'yii\redis\Session',
'redis' => [
'hostname' => 'localhost',
'port' => 6379,
'database' => 3
],
'keyPrefix' => 'imooc_sess_',
],

url美化

web.php中开启urlManager

'urlManager' => [
'enablePrettyUrl' => true,  //url美化
'showScriptName' => false,  //不显示index.php脚本文件
'suffix' => '.html',  //伪静态
'rules' => [
'<controller:(index|cart|order)>' => '<controller>index',   //index,cart,order访问时请求对应controller中的index方法
'auth' => 'member/auth',  //访问auth对应member/auth
'product-category-<cateid:\d+>' => 'product/index',  //会自动吸收参数
'product-<productid:\d+>' => 'product/detail',
'order-check-<orderid:\d+>' => 'order/check',
[
'pattern' => 'imoocback',
'route' => '/admin/dafault/index',
'suffix' => '.html',
],  //imoocbck对应到后台
],
],

设置error模板

image