Permalink
Browse files

Add caching strategy to optomize response times

- Add spatie/responsecache to cache all successful GET requests. By
  default all requests are cached for 1 week.
- Bust cache when an order or a product is created/updated/deleted. Uses
  observers on the models.
- Set redis as the default cache driver.
  • Loading branch information...
MitchellMcKenna committed Aug 18, 2017
1 parent be7e965 commit 837a859580d0fca4ce83143d731ab345246ab6fa
View
@@ -13,7 +13,7 @@ DB_USERNAME=homestead
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=file
CACHE_DRIVER=redis
SESSION_DRIVER=file
QUEUE_DRIVER=sync
View
@@ -13,7 +13,7 @@ DB_USERNAME=root
DB_PASSWORD=
BROADCAST_DRIVER=log
CACHE_DRIVER=file
CACHE_DRIVER=redis
SESSION_DRIVER=file
QUEUE_DRIVER=sync
View
@@ -20,6 +20,7 @@ class Kernel extends HttpKernel
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\Clockwork\Support\Laravel\ClockworkMiddleware::class,
\Spatie\ResponseCache\Middlewares\CacheResponse::class,
];
/**
@@ -58,5 +59,6 @@ class Kernel extends HttpKernel
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'doNotCacheResponse' => \Spatie\ResponseCache\Middlewares\DoNotCacheResponse::class,
];
}
@@ -0,0 +1,52 @@
<?php
namespace App\Observers;
use App\Order;
use Spatie\ResponseCache\ResponseCache;
class OrderObserver
{
protected $cache;
/**
* @param ResponseCache $cache
*/
public function __construct(ResponseCache $cache)
{
$this->cache = $cache;
}
/**
* Listen to the Order created event.
*
* @param Order $order
* @return void
*/
public function created(Order $order)
{
$this->cache->flush();
}
/**
* Listen to the Order created event.
*
* @param Order $order
* @return void
*/
public function updated(Order $order)
{
$this->cache->flush();
}
/**
* Listen to the Order deleted event.
*
* @param Order $order
* @return void
*/
public function deleted(Order $order)
{
$this->cache->flush();
}
}
@@ -0,0 +1,52 @@
<?php
namespace App\Observers;
use App\Product;
use Spatie\ResponseCache\ResponseCache;
class ProductObserver
{
protected $cache;
/**
* @param ResponseCache $cache
*/
public function __construct(ResponseCache $cache)
{
$this->cache = $cache;
}
/**
* Listen to the Order created event.
*
* @param Product $product
* @return void
*/
public function created(Product $product)
{
$this->cache->flush();
}
/**
* Listen to the Order created event.
*
* @param Product $product
* @return void
*/
public function updated(Product $product)
{
$this->cache->flush();
}
/**
* Listen to the Order deleted event.
*
* @param Product $product
* @return void
*/
public function deleted(Product $product)
{
$this->cache->flush();
}
}
@@ -2,6 +2,10 @@
namespace App\Providers;
use App\Observers\OrderObserver;
use App\Observers\ProductObserver;
use App\Order;
use App\Product;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -13,7 +17,8 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Order::observe(OrderObserver::class);
Product::observe(ProductObserver::class);
}
/**
View
@@ -11,7 +11,9 @@
"laravel/framework": "5.5.*",
"laravel/tinker": "~1.0",
"league/csv": "^9.0",
"league/fractal": "^0.16.0"
"league/fractal": "^0.16.0",
"predis/predis": "^1.1",
"spatie/laravel-responsecache": "dev-laravel-55"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^2.4",
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 837a859

Please sign in to comment.