Prechádzať zdrojové kódy

Caching queries/models. Query event handling.

Frederic G. MARAND 7 rokov pred
rodič
commit
e16e3b8866

+ 13 - 0
app/Listeners/QueryExecutedListener.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace App\Listeners;
+
+use Illuminate\Database\Events\QueryExecuted;
+
+class QueryExecutedListener
+{
+    public function handle(QueryExecuted $event)
+    {
+        dd($event);
+    }
+}

+ 5 - 3
app/Providers/EventServiceProvider.php

@@ -2,6 +2,8 @@
 
 namespace App\Providers;
 
+use App\Listeners\QueryExecutedListener;
+use Illuminate\Database\Events\QueryExecuted;
 use Illuminate\Support\Facades\Event;
 use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
 
@@ -13,9 +15,9 @@ class EventServiceProvider extends ServiceProvider
      * @var array
      */
     protected $listen = [
-        'App\Events\Event' => [
-            'App\Listeners\EventListener',
-        ],
+//        QueryExecuted::class => [
+//            QueryExecutedListener::class
+//        ],
     ];
 
     /**

+ 11 - 0
app/Task.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Task extends Model
+{
+    protected $fillable = ['title', 'body', 'slug'];
+
+}

+ 34 - 0
database/migrations/2017_09_10_114559_create_tasks_table.php

@@ -0,0 +1,34 @@
+<?php
+
+use Illuminate\Support\Facades\Schema;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Database\Migrations\Migration;
+
+class CreateTasksTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('tasks', function (Blueprint $table) {
+            $table->increments('id');
+            $table->string('title');
+            $table->text('body');
+            $table->string('slug');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('tasks');
+    }
+}

+ 5 - 7
routes/web.php

@@ -11,11 +11,9 @@
 |
 */
 
-$this->get('/', function (Illuminate\Contracts\Cache\Factory $cache) {
-    /** @var \Illuminate\Contracts\Cache\Store $store */
-    $store = $cache->store();
-    $cid = 'some cid';
-    $store->put($cid, 'value', 10);
-    $v = $store->get($cid);
-    return $v;
+$this->get('/tasks', function () {
+    return Cache::remember('all-tasks', 1, function () {
+        $tasks = \App\Task::all();
+        return $tasks;
+    });
 });