Explorar o código

cache 必须存储数组 resolve

visuddhinanda hai 4 días
pai
achega
c7b3d21b51
Modificáronse 1 ficheiros con 19 adicións e 9 borrados
  1. 19 9
      api-v13/app/Http/Controllers/VocabularyController.php

+ 19 - 9
api-v13/app/Http/Controllers/VocabularyController.php

@@ -19,22 +19,32 @@ class VocabularyController extends Controller
         //
         //
         switch ($request->input("view")) {
         switch ($request->input("view")) {
             case 'key':
             case 'key':
-                $key = $request->input("key");
-                $result = Cache::remember(
-                    "/dict_vocabulary/{$key}",
+                $key = (string) $request->input('key', '');
+                if ($key === '') {
+                    return $this->ok(['rows' => [], 'count' => 0]);
+                }
+
+                $payload = Cache::remember(
+                    'v13:dict_vocabulary:' . md5($key),
                     config('mint.cache.expire'),
                     config('mint.cache.expire'),
                     function () use ($key) {
                     function () use ($key) {
-                        $query = Vocabulary::where('word', 'like', $key . "%")
-                            ->orWhere('word_en', 'like', $key . "%")
+                        $rows = Vocabulary::where('word', 'like', $key . '%')
+                            ->orWhere('word_en', 'like', $key . '%')
                             ->orderBy('strlen')
                             ->orderBy('strlen')
                             ->orderBy('word')
                             ->orderBy('word')
-                            ->take(50);
-                        return $query->get();
+                            ->take(50)
+                            ->get();
+
+                        return [
+                            'rows'  => VocabularyResource::collection($rows)->resolve(),
+                            'count' => $rows->count(),
+                        ];
                     }
                     }
                 );
                 );
-                return $this->ok(['rows' => VocabularyResource::collection($result), 'count' => count($result)]);
-                break;
+
+                return $this->ok($payload);
         }
         }
+        return $this->ok(['rows'=>[],'count'=>0]);
     }
     }
 
 
     /**
     /**