From aa883e96cf0dd8bbdac8aad8f60c8fd9656b13ba Mon Sep 17 00:00:00 2001 From: Charles Teh Date: Tue, 21 Nov 2023 23:41:16 +0800 Subject: [PATCH] Add (cache) --- app/Http/Kernel.php | 3 + composer.json | 1 + composer.lock | 86 ++++++++++++++++- config/responsecache.php | 94 +++++++++++++++++++ .../views/front/partials/search.blade.php | 6 +- routes/web.php | 13 +-- 6 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 config/responsecache.php diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 5f386d6..29a956d 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -67,5 +67,8 @@ class Kernel extends HttpKernel 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, 'horizonBasicAuth' => \App\Http\Middleware\HorizonBasicAuthMiddleware::class, + + 'cacheResponse' => \Spatie\ResponseCache\Middlewares\CacheResponse::class, + 'doNotCacheResponse' => \Spatie\ResponseCache\Middlewares\DoNotCacheResponse::class, ]; } diff --git a/composer.json b/composer.json index aa2a20e..a65bafb 100644 --- a/composer.json +++ b/composer.json @@ -33,6 +33,7 @@ "silviolleite/laravelpwa": "^2.0", "spatie/laravel-feed": "^4.3", "spatie/laravel-googletagmanager": "^2.6", + "spatie/laravel-responsecache": "^7.4", "spatie/laravel-sitemap": "^6.3", "symfony/dom-crawler": "^6.3", "tightenco/ziggy": "^1.6", diff --git a/composer.lock b/composer.lock index 8e4351c..b026ee7 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7419246584579187134315a95c74fa42", + "content-hash": "3745e68a419d803a47fdc1dd8f721dca", "packages": [ { "name": "artesaos/seotools", @@ -6298,6 +6298,90 @@ ], "time": "2023-08-23T09:04:39+00:00" }, + { + "name": "spatie/laravel-responsecache", + "version": "7.4.10", + "source": { + "type": "git", + "url": "https://github.com/spatie/laravel-responsecache.git", + "reference": "cf0305f73fcc49dacfadd0f2228887a92fa736ac" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/spatie/laravel-responsecache/zipball/cf0305f73fcc49dacfadd0f2228887a92fa736ac", + "reference": "cf0305f73fcc49dacfadd0f2228887a92fa736ac", + "shasum": "" + }, + "require": { + "illuminate/cache": "^8.71|^9.0|^10.0", + "illuminate/console": "^8.71|^9.0|^10.0", + "illuminate/container": "^8.71|^9.0|^10.0", + "illuminate/http": "^8.71|^9.0|^10.0", + "illuminate/support": "^8.71|^9.0|^10.0", + "nesbot/carbon": "^2.63", + "php": "^8.0", + "spatie/laravel-package-tools": "^1.9" + }, + "require-dev": { + "laravel/framework": "^9.0|^10.0", + "mockery/mockery": "^1.4", + "orchestra/testbench": "^6.23|^7.0|^8.0", + "pestphp/pest": "^1.22", + "phpunit/phpunit": "^9.4" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Spatie\\ResponseCache\\ResponseCacheServiceProvider" + ], + "aliases": { + "ResponseCache": "Spatie\\ResponseCache\\Facades\\ResponseCache" + } + } + }, + "autoload": { + "psr-4": { + "Spatie\\ResponseCache\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Freek Van der Herten", + "email": "freek@spatie.be", + "homepage": "https://spatie.be", + "role": "Developer" + } + ], + "description": "Speed up a Laravel application by caching the entire response", + "homepage": "https://github.com/spatie/laravel-responsecache", + "keywords": [ + "cache", + "laravel", + "laravel-responsecache", + "performance", + "response", + "spatie" + ], + "support": { + "source": "https://github.com/spatie/laravel-responsecache/tree/7.4.10" + }, + "funding": [ + { + "url": "https://spatie.be/open-source/support-us", + "type": "custom" + }, + { + "url": "https://github.com/spatie", + "type": "github" + } + ], + "time": "2023-10-28T18:47:12+00:00" + }, { "name": "spatie/laravel-sitemap", "version": "6.4.0", diff --git a/config/responsecache.php b/config/responsecache.php new file mode 100644 index 0000000..e0f04b3 --- /dev/null +++ b/config/responsecache.php @@ -0,0 +1,94 @@ + env('RESPONSE_CACHE_ENABLED', true), + + /* + * The given class will determinate if a request should be cached. The + * default class will cache all successful GET-requests. + * + * You can provide your own class given that it implements the + * CacheProfile interface. + */ + 'cache_profile' => Spatie\ResponseCache\CacheProfiles\CacheAllSuccessfulGetRequests::class, + + /* + * Optionally, you can specify a header that will force a cache bypass. + * This can be useful to monitor the performance of your application. + */ + 'cache_bypass_header' => [ + 'name' => env('CACHE_BYPASS_HEADER_NAME', null), + 'value' => env('CACHE_BYPASS_HEADER_VALUE', null), + ], + + /* + * When using the default CacheRequestFilter this setting controls the + * default number of seconds responses must be cached. + */ + 'cache_lifetime_in_seconds' => env('RESPONSE_CACHE_LIFETIME', 60 * 60 * 24 * 7), + + /* + * This setting determines if a http header named with the cache time + * should be added to a cached response. This can be handy when + * debugging. + */ + 'add_cache_time_header' => env('APP_DEBUG', false), + + /* + * This setting determines the name of the http header that contains + * the time at which the response was cached + */ + 'cache_time_header_name' => env('RESPONSE_CACHE_HEADER_NAME', 'laravel-responsecache'), + + /* + * This setting determines if a http header named with the cache age + * should be added to a cached response. This can be handy when + * debugging. + * ONLY works when "add_cache_time_header" is also active! + */ + 'add_cache_age_header' => env('RESPONSE_CACHE_AGE_HEADER', false), + + /* + * This setting determines the name of the http header that contains + * the age of cache + */ + 'cache_age_header_name' => env('RESPONSE_CACHE_AGE_HEADER_NAME', 'laravel-responsecache-age'), + + /* + * Here you may define the cache store that should be used to store + * requests. This can be the name of any store that is + * configured in app/config/cache.php + */ + 'cache_store' => env('RESPONSE_CACHE_DRIVER', 'file'), + + /* + * Here you may define replacers that dynamically replace content from the response. + * Each replacer must implement the Replacer interface. + */ + 'replacers' => [ + \Spatie\ResponseCache\Replacers\CsrfTokenReplacer::class, + ], + + /* + * If the cache driver you configured supports tags, you may specify a tag name + * here. All responses will be tagged. When clearing the responsecache only + * items with that tag will be flushed. + * + * You may use a string or an array here. + */ + 'cache_tag' => '', + + /* + * This class is responsible for generating a hash for a request. This hash + * is used to look up a cached response. + */ + 'hasher' => \Spatie\ResponseCache\Hasher\DefaultHasher::class, + + /* + * This class is responsible for serializing responses. + */ + 'serializer' => \Spatie\ResponseCache\Serializers\DefaultSerializer::class, +]; diff --git a/resources/views/front/partials/search.blade.php b/resources/views/front/partials/search.blade.php index 0588873..11e365a 100644 --- a/resources/views/front/partials/search.blade.php +++ b/resources/views/front/partials/search.blade.php @@ -1,10 +1,10 @@ diff --git a/routes/web.php b/routes/web.php index efc7d37..dd080bd 100644 --- a/routes/web.php +++ b/routes/web.php @@ -15,15 +15,16 @@ Route::feeds('feeds'); -Route::get('/', [App\Http\Controllers\Front\FrontHomeController::class, 'home'])->name('front.home'); +Route::get('/', [App\Http\Controllers\Front\FrontHomeController::class, 'home'])->name('front.home')->middleware('cacheResponse:3600'); -Route::get('/terms', [App\Http\Controllers\Front\FrontHomeController::class, 'terms'])->name('front.terms'); -Route::get('/privacy', [App\Http\Controllers\Front\FrontHomeController::class, 'privacy'])->name('front.privacy'); +Route::get('/terms', [App\Http\Controllers\Front\FrontHomeController::class, 'terms'])->name('front.terms')->middleware('cacheResponse:2630000'); -Route::get('/disclaimer', [App\Http\Controllers\Front\FrontHomeController::class, 'disclaimer'])->name('front.disclaimer'); +Route::get('/privacy', [App\Http\Controllers\Front\FrontHomeController::class, 'privacy'])->name('front.privacy')->middleware('cacheResponse:2630000'); -Route::get('/bites', [App\Http\Controllers\Front\FrontListController::class, 'index'])->name('front.all'); +Route::get('/disclaimer', [App\Http\Controllers\Front\FrontHomeController::class, 'disclaimer'])->name('front.disclaimer')->middleware('cacheResponse:2630000'); + +Route::get('/bites', [App\Http\Controllers\Front\FrontListController::class, 'index'])->name('front.all')->middleware('cacheResponse:3600'); Route::post('/search', [App\Http\Controllers\Front\FrontListController::class, 'search'])->name('front.search'); @@ -33,4 +34,4 @@ Route::get('/{category_slug}', [App\Http\Controllers\Front\FrontListController::class, 'category']) ->where('category_slug', '^(updates|opinions|features|new-launches|how-tos|reviews)$') - ->name('front.category'); + ->name('front.category')->middleware('cacheResponse:3600');