Caching added for api

This commit is contained in:
2020-08-24 16:26:35 +05:00
parent 9e8fc28d3a
commit 4bc1f5448e
5 changed files with 59 additions and 42 deletions

View File

@@ -8,6 +8,7 @@ use App\Http\Resources\TopicResource;
use Illuminate\Http\JsonResponse;
use App\Http\Resources\ArticleResource;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
class TopicsAPIController extends Controller
{
@@ -21,7 +22,7 @@ class TopicsAPIController extends Controller
{
return TopicResource::collection(Topic::inRandomOrder()->take(12)->get());
}
/**
* Load all the articles for a given topics
*
@@ -29,9 +30,11 @@ class TopicsAPIController extends Controller
*/
public function show(Topic $topic)
{
return response()->json([
'topic' => new TopicResource($topic),
'articles' => $topic->articles()->with('source')->latest('published_date')->paginate(8)
]);
return Cache::remember('topic_'.$topic->slug, 300, function () use ($topic) {
return response()->json([
'topic' => new TopicResource($topic),
'articles' => $topic->articles()->with('source')->latest('published_date')->paginate(8)
]);
});
}
}