Преглед изворни кода

Controller output caching using route middleware.

Frederic G. MARAND пре 7 година
родитељ
комит
22b20442b1

+ 2 - 0
app/Http/Kernel.php

@@ -2,6 +2,7 @@
 
 namespace App\Http;
 
+use App\Http\Middleware\RouteCache;
 use Illuminate\Foundation\Http\Kernel as HttpKernel;
 
 class Kernel extends HttpKernel
@@ -57,5 +58,6 @@ class Kernel extends HttpKernel
         'can' => \Illuminate\Auth\Middleware\Authorize::class,
         'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
         'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
+        'cache' => RouteCache::class,
     ];
 }

+ 32 - 0
app/Http/Middleware/RouteCache.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace App\Http\Middleware;
+
+use Closure;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Str;
+
+class RouteCache
+{
+    /**
+     * Handle an incoming request.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \Closure  $next
+     * @return mixed
+     */
+    public function handle($request, Closure $next)
+    {
+        $response = Cache::remember($this->makeCacheKey($request), 1, function () use($next, $request) {
+           return $next($request);
+        });
+
+        return $response;
+    }
+
+    protected function makeCacheKey(Request $request) {
+        $url = Str::slug($request->url());
+        return "route:$url";
+    }
+}

+ 3 - 1
app/Listeners/QueryExecutedListener.php

@@ -8,6 +8,8 @@ class QueryExecutedListener
 {
     public function handle(QueryExecuted $event)
     {
-        dd($event);
+        // If triggered, will be shown in addition to the controller response.
+        // Don't use dd() as it includes a die() so the results are not stored.
+        echo $event->sql;
     }
 }

+ 3 - 15
app/Providers/EventServiceProvider.php

@@ -15,20 +15,8 @@ class EventServiceProvider extends ServiceProvider
      * @var array
      */
     protected $listen = [
-//        QueryExecuted::class => [
-//            QueryExecutedListener::class
-//        ],
+        QueryExecuted::class => [
+            QueryExecutedListener::class
+        ],
     ];
-
-    /**
-     * Register any events for your application.
-     *
-     * @return void
-     */
-    public function boot()
-    {
-        parent::boot();
-
-        //
-    }
 }

+ 5 - 5
resources/views/welcome.blade.php

@@ -83,11 +83,11 @@
                 </div>
 
                 <div class="links">
-                    <a href="https://laravel.com/docs">Documentation</a>
-                    <a href="https://laracasts.com">Laracasts</a>
-                    <a href="https://laravel-news.com">News</a>
-                    <a href="https://forge.laravel.com">Forge</a>
-                    <a href="https://github.com/laravel/laravel">GitHub</a>
+                    <ul>
+                    @foreach ($tasks as $task)
+                        {{ $task->title }}
+                    @endforeach
+                    </ul>
                 </div>
             </div>
         </div>

+ 12 - 5
routes/web.php

@@ -11,9 +11,16 @@
 |
 */
 
+// Cannot cache a view result as such, as it contains a Closure, which is
+// not serializable.
+
 $this->get('/tasks', function () {
-    return Cache::remember('all-tasks', 1, function () {
-        $tasks = \App\Task::all();
-        return $tasks;
-    });
-});
+    $tasks = \App\Task::all();
+    return view('welcome', compact('tasks'))->__toString();
+})->middleware('cache');
+
+$this->get('/tasks/first', function () {
+    $tasks = [\App\Task::first()];
+    return view('welcome', compact('tasks'))->__toString();
+})->middleware('cache');
+