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

@@ -7,6 +7,7 @@ use App\Article;
use Illuminate\Support\Carbon;
use App\Http\Resources\ArticleResource;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
class TodaysPick extends Controller
{
@@ -19,27 +20,29 @@ class TodaysPick extends Controller
*/
public function __invoke()
{
return Article::with('topics', 'source')
->whereDate('published_date', Carbon::today())
->inRandomOrder()
->take(8)
->get()
->transform(function ($article) {
return [
"id" => $article->id,
"title" => $article->title,
"url" => $article->url,
"author" => $article->author,
"featured_image" => $article->featured_image,
"published_date" => $article->published_date,
"meta" => $article->meta,
"source" => $article->source,
"topics" => $article->topics,
"body" => $article->body,
];
})
->unique('source.name')
->values()
->toArray();
return Cache::remember('articles.todayspick', 300, function () {
return Article::with('topics', 'source')
->whereDate('published_date', Carbon::today())
->inRandomOrder()
->take(8)
->get()
->transform(function ($article) {
return [
"id" => $article->id,
"title" => $article->title,
"url" => $article->url,
"author" => $article->author,
"featured_image" => $article->featured_image,
"published_date" => $article->published_date,
"meta" => $article->meta,
"source" => $article->source,
"topics" => $article->topics,
"body" => $article->body,
];
})
->unique('source.name')
->values()
->toArray();
});
}
}