Просмотр исходного кода

feat(user): 添加签到接口缓存逻辑避免频繁查询

- 新增用户签到分数缓存键值设置
- 实现签到接口缓存命中时直接返回默认数据
- 添加剩余秒数计算用于缓存过期时间设置
- 在缓存未命中时执行原有签到逻辑查询
shichen месяцев назад: 3
Родитель
Сommit
1b64c32925
1 измененных файлов с 14 добавлено и 0 удалено
  1. 14 0
      app/controller/api/user/User.php

+ 14 - 0
app/controller/api/user/User.php

@@ -3323,6 +3323,20 @@ class User extends BaseController
3323 3323
     public function new_sign_info()
3324 3324
     {
3325 3325
         $uid = $this->request->uid();
3326
+        $key = 'user_sign_score_' . $uid;
3327
+        if (Cache::get($key)) {
3328
+            $data = [
3329
+                'today' => 0,
3330
+                'day'   => 0
3331
+            ];
3332
+            return app('json')->success($data);
3333
+        }
3334
+
3335
+        $now = time();
3336
+        $todayEnd = strtotime('today 23:59:59');
3337
+        $remainingSeconds = $todayEnd - $now;
3338
+        Cache::set($key, 1, $remainingSeconds);
3339
+
3326 3340
         // 查询今天是否已经签到
3327 3341
         $today_signin_info = Db::name('user_sign_score')->where(array('uid' => $uid))->whereDay('signin_time')->find();
3328 3342
         // 查询昨天是否已经签到了