|
|
@@ -20,7 +20,7 @@
|
|
|
namespace app\shopapi\logic;
|
|
|
|
|
|
use app\common\model\Info;
|
|
|
-
|
|
|
+use app\common\cache\InfoCache;
|
|
|
class InfoLogic
|
|
|
{
|
|
|
/**
|
|
|
@@ -35,12 +35,50 @@ class InfoLogic
|
|
|
*/
|
|
|
public static function detail($params)
|
|
|
{
|
|
|
- // 增加浏览量
|
|
|
+// // 增加浏览量
|
|
|
+// $info = Info::find($params['id']);
|
|
|
+// $visit = $info->visit ?? 1;
|
|
|
+// $info->visit = $visit + 1 ;
|
|
|
+// $info->save();
|
|
|
+// return Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
|
|
|
+// ->append(['category'])->find($params['id'])->toArray();
|
|
|
+ $infoCache = new InfoCache();
|
|
|
+
|
|
|
+ // 先尝试从缓存获取
|
|
|
+ $cachedInfo = $infoCache->getInfoDetail($params['id']);
|
|
|
+ if ($cachedInfo) {
|
|
|
+ // 缓存存在,仍需要增加浏览量
|
|
|
+ $info = Info::find($params['id']);
|
|
|
+ if ($info) {
|
|
|
+ $visit = $info->visit ?? 1;
|
|
|
+ $info->visit = $visit + 1;
|
|
|
+ $info->save();
|
|
|
+
|
|
|
+ // 更新缓存中的浏览量
|
|
|
+ $cachedInfo['visit'] = $visit + 1;
|
|
|
+ $infoCache->setInfoDetail($params['id'], $cachedInfo);
|
|
|
+ }
|
|
|
+ return $cachedInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 缓存不存在,从数据库查询
|
|
|
$info = Info::find($params['id']);
|
|
|
+ if (!$info) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 增加浏览量
|
|
|
$visit = $info->visit ?? 1;
|
|
|
- $info->visit = $visit + 1 ;
|
|
|
+ $info->visit = $visit + 1;
|
|
|
$info->save();
|
|
|
- return Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
|
|
|
- ->append(['category'])->find($params['id'])->toArray();
|
|
|
+
|
|
|
+ // 查询详细信息
|
|
|
+ $result = Info::field('id,cid,title,synopsis,image,address,phone,latitude,longitude,content,visit,create_time')
|
|
|
+ ->append(['category'])->find($params['id'])->toArray();
|
|
|
+
|
|
|
+ // 设置缓存,缓存1小时
|
|
|
+ $infoCache->setInfoDetail($params['id'], $result, 3600);
|
|
|
+
|
|
|
+ return $result;
|
|
|
}
|
|
|
}
|