moonsflyer 4 månader sedan
förälder
incheckning
038323f2ee
2 ändrade filer med 99 tillägg och 5 borttagningar
  1. 56 0
      app/common/cache/InfoCache.php
  2. 43 5
      app/shopapi/logic/InfoLogic.php

+ 56 - 0
app/common/cache/InfoCache.php

@@ -0,0 +1,56 @@
+<?php
+
+namespace app\common\cache;
+
+/**
+ * Info信息缓存类
+ * Class InfoCache
+ * @package app\common\cache
+ */
+class InfoCache extends BaseCache
+{
+    protected $tagName = 'info';
+    
+    /**
+     * @notes 获取Info详情缓存
+     * @param $id
+     * @return false|mixed
+     * @author Tab
+     * @date 2021/7/14 15:13
+     */
+    public function getInfoDetail($id)
+    {
+        $cacheKey = $this->tagName . '_detail_' . $id;
+        $info = $this->get($cacheKey);
+        if($info) {
+            return $info;
+        }
+        return false;
+    }
+    
+    /**
+     * @notes 设置Info详情缓存
+     * @param $id
+     * @param $data
+     * @param int $expire 缓存过期时间,默认1小时
+     * @author Tab
+     * @date 2021/7/14 15:13
+     */
+    public function setInfoDetail($id, $data, $expire = 3600)
+    {
+        $cacheKey = $this->tagName . '_detail_' . $id;
+        $this->set($cacheKey, $data, $expire);
+    }
+    
+    /**
+     * @notes 删除Info详情缓存
+     * @param $id
+     * @author Tab
+     * @date 2021/7/14 15:13
+     */
+    public function deleteInfoDetail($id)
+    {
+        $cacheKey = $this->tagName . '_detail_' . $id;
+        $this->delete($cacheKey);
+    }
+}

+ 43 - 5
app/shopapi/logic/InfoLogic.php

@@ -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;
     }
 }