Caching added for api
This commit is contained in:
		| @@ -6,11 +6,14 @@ use Illuminate\Http\Request; | ||||
| use App\Article; | ||||
| use App\Http\Resources\ArticleResource; | ||||
| use App\Http\Controllers\Controller; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
|  | ||||
| class ArticlesAPIController extends Controller | ||||
| { | ||||
|     public function show(Article $article) : ArticleResource | ||||
|     public function show(Article $article): ArticleResource | ||||
|     { | ||||
|         return new ArticleResource($article); | ||||
|         return Cache::remember('article_'.$article->id, 300, function () use ($article){ | ||||
|             return new ArticleResource($article); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -6,9 +6,10 @@ use App\Article; | ||||
| use Illuminate\Http\Request; | ||||
| use App\Http\Resources\ArticleResource; | ||||
| use App\Http\Controllers\Controller; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
|  | ||||
| final class RecentArticles extends Controller | ||||
| {     | ||||
| { | ||||
|     /** | ||||
|      * Get the Latest articles made paginated | ||||
|      * | ||||
| @@ -16,10 +17,12 @@ final class RecentArticles extends Controller | ||||
|      */ | ||||
|     public function __invoke() | ||||
|     { | ||||
|         return [ | ||||
|             "articles" => Article::with('source', 'topics') | ||||
|             ->latest("published_date") | ||||
|             ->paginate(20) | ||||
|         ]; | ||||
|         return Cache::remember('articles.recent', 300, function () { | ||||
|             return [ | ||||
|                 "articles" => Article::with('source', 'topics') | ||||
|                     ->latest("published_date") | ||||
|                     ->paginate(20) | ||||
|             ]; | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -8,18 +8,21 @@ use App\Source; | ||||
| use Illuminate\Http\JsonResponse; | ||||
| use App\Http\Resources\ArticleResource; | ||||
| use App\Http\Controllers\Controller; | ||||
| use Illuminate\Support\Facades\Cache; | ||||
|  | ||||
| class SourcesAPIController extends Controller | ||||
| {     | ||||
| { | ||||
|     /** | ||||
|      * Return all the avaialble Sources. | ||||
|      * | ||||
|      */ | ||||
|     public function index() | ||||
|     { | ||||
|         return SourceResource::collection(Source::all()); | ||||
|         return Cache::remember('sources.index', 300, function () { | ||||
|             return SourceResource::collection(Source::all()); | ||||
|         }); | ||||
|     } | ||||
|      | ||||
|  | ||||
|     /** | ||||
|      * Return all the articles for a given source | ||||
|      * | ||||
| @@ -28,9 +31,11 @@ class SourcesAPIController extends Controller | ||||
|      */ | ||||
|     public function show(Source $source) | ||||
|     { | ||||
|         return response()->json([ | ||||
|             'source' => new SourceResource($source), | ||||
|             'articles' =>  $source->articles()->with('source')->latest('published_date')->paginate(8) | ||||
|         ]); | ||||
|         return Cache::remember($source->slug. '32', 300, function () use ($source) { | ||||
|             return response()->json([ | ||||
|                 'source' => new SourceResource($source), | ||||
|                 'articles' =>  $source->articles()->with('source')->latest('published_date')->paginate(8) | ||||
|             ]); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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(); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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) | ||||
|             ]); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user