Authored by wuxiao

接口缓存修改

... ... @@ -2,10 +2,15 @@
return [
'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
'components' => [
'cache' => [
'fileCache' => [
'class' => 'yii\caching\FileCache',
'keyPrefix' => 'YOHOLive',
],
'cache' => [
'class' => 'soa\components\pagecache\Redis',
'redis' => 'redis',
'keyPrefix' => 'YOHOLive',
],
'redis' => [
'class' => 'yii\redis\Connection',
'hostname' => '123.56.138.21',
... ... @@ -20,19 +25,14 @@ return [
'port' => 6379,
'database' => 0,
],
'pageCache' => [
'class' => 'soa\components\pagecache\Redis',
'redis' => 'redis',
'keyPrefix' => 'YOHOLive',
],
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=yii2advanced',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'schemaCache' => 'pageCache',
'queryCache' => 'pageCache',
'schemaCache' => 'cache',
'queryCache' => 'cache',
],
'urlManager' => [
'enablePrettyUrl' => true,
... ...
... ... @@ -9,6 +9,16 @@ use Yii;
*/
class Redis extends \yii\redis\Cache
{
public function init()
{
if (isset(Yii::$app->params['enableCache']) && !Yii::$app->params['enableCache'])
{
Yii::$app->db->enableQueryCache = false;
Yii::$app->db->enableSchemaCache = false;
}
parent::init();
}
/**
* @inheritdoc
... ...
... ... @@ -42,7 +42,7 @@ class BaseController extends Controller
$this->app = $app;
}
$this->cache = Yii::$app->pageCache;
$this->cache = Yii::$app->cache;
return parent::beforeAction($action);
}
... ...
... ... @@ -87,16 +87,19 @@ class LivingController extends BaseController
->getRawSql()]);
$this->cache->set($key, json_encode($ret), Yii::$app->params['defaultCacheExpire'], $dependency);
}else{
Yii::$app->db->cache(function() use (&$ret){
$models = Yii::$app->db->cache(function(){
return RoomNums::find()->indexBy('room_id')->all();
},60);
if ($models){
$_ret = json_decode($ret,true);
$models = RoomNums::find()->indexBy('room_id')->all();
foreach ($_ret as $k=>$row){
if (!empty($models[$row['room_id']]->online_num)){
$row['audience_num'] = (int)$models[$row['room_id']]->online_num;//在线人数
}
$_ret[$k] = $row;
}
}, 60);
$ret = $_ret;
}
}
... ... @@ -170,16 +173,19 @@ class LivingController extends BaseController
->getRawSql()]);
$this->cache->set($key, json_encode($ret), Yii::$app->params['defaultCacheExpire'], $dependency);
}else{
Yii::$app->db->cache(function() use (&$ret){
$models = Yii::$app->db->cache(function(){
return Video::find()->indexBy('id')->all();
},60);
if ($models){
$_ret = json_decode($ret,true);
$models = Video::find()->indexBy('id')->all();
foreach ($_ret as $k=>$row){
if (!empty($models[$row['video_id']])){
$row['audience_num'] = (int)($models[$row['video_id']]->audience_num+$models[$row['video_id']]->replay_num);
}
$_ret[$k] = $row;
}
}, 60);
$ret = $_ret;
}
}
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
... ... @@ -262,17 +268,17 @@ class LivingController extends BaseController
->bindValue(':room_id', $room_id)
->getRawSql()]);
$this->cache->set($key, json_encode($ret), Yii::$app->params['defaultCacheExpire'], $dependency);
}else{
Yii::$app->db->cache(function() use ($room_id,&$ret){
if (!$model = RoomNums::findOne(['room_id'=>$room_id])){
return;
}
}else{
$model = Yii::$app->db->cache(function() use ($room_id){
return RoomNums::findOne(['room_id'=>$room_id]);
},60);
if ($model){
$_ret = json_decode($ret,true);
$ret = array_merge($_ret,[
'like_num'=>(int)$model->like_num,
'audience_num'=>(int)$model->online_num,
]);
}, 60);
}
}
$this->renderJson(Yii::$app->params['success_code'],'',$ret);
... ...