From d4ae21d04f8203c6d2b080616786b56c127d8beb Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Sat, 25 Nov 2017 16:09:36 +0100 Subject: [PATCH 1/7] Added laravel 5.5 install information to readme --- readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.md b/readme.md index 14eb8be..226b2f9 100644 --- a/readme.md +++ b/readme.md @@ -42,6 +42,10 @@ The installation process is very straight-forward. It's like any other laravel p composer require jkniest/htmlcache ``` +__If you are using laravel 5.5 or higher the installation is already finished. You now have multiple ways to use the middleware (see [Using](#using)).__ + +Otherwise, if you use laravel 5.4 or lower go to step 2: + 2) Add the package service provider in your packages configuration. Open up the `config/app.php` file and the following into your `providers` array: ```php 'providers' => [ From 5a2e61ca13b3f5fb7836f1d199327d0786d16a9e Mon Sep 17 00:00:00 2001 From: Kniest Jordan Date: Mon, 11 Dec 2017 10:54:05 +0100 Subject: [PATCH 2/7] Added styleci configuration and badge in readme --- .styleci.yml | 1 + readme.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .styleci.yml diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..c3bb259 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: laravel \ No newline at end of file diff --git a/readme.md b/readme.md index 226b2f9..d301c92 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # Laravel HTML Cache -[ ![Build](https://travis-ci.com/jkniest/HTMLCache.svg?token=V2HFFCLc6NVnxsqjqD9v&branch=develop) ](https://travis-ci.com/jkniest/HTMLCache) [![Latest Stable Version](https://poser.pugx.org/jkniest/htmlcache/v/stable)](https://packagist.org/packages/jkniest/htmlcache) [![Total Downloads](https://poser.pugx.org/jkniest/htmlcache/downloads)](https://packagist.org/packages/jkniest/htmlcache) [![License](https://poser.pugx.org/jkniest/htmlcache/license)](https://packagist.org/packages/jkniest/htmlcache) +[ ![Build](https://travis-ci.com/jkniest/HTMLCache.svg?token=V2HFFCLc6NVnxsqjqD9v&branch=develop) ](https://travis-ci.com/jkniest/HTMLCache) [![Latest Stable Version](https://poser.pugx.org/jkniest/htmlcache/v/stable)](https://packagist.org/packages/jkniest/htmlcache) [![Total Downloads](https://poser.pugx.org/jkniest/htmlcache/downloads)](https://packagist.org/packages/jkniest/htmlcache) [![License](https://poser.pugx.org/jkniest/htmlcache/license)](https://packagist.org/packages/jkniest/htmlcache) [ ![StyleCI](https://styleci.io/repos/100369160/shield?branch=develop&style=flat) ](https://styleci.io/repos/100369160) --- From 0a1a729916fd3f495d034b31c73df8298fe23a41 Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Mon, 11 Dec 2017 09:54:22 +0000 Subject: [PATCH 3/7] Apply fixes from StyleCI [ci skip] [skip ci] --- src/HtmlCacheServiceProvider.php | 9 ++++----- src/Http/Middleware/CacheHtml.php | 10 ++++------ src/config/htmlcache.php | 14 +++++++------- tests/BaseTestCase.php | 15 +++++++-------- tests/ExampleTest.php | 2 +- tests/Feature/CacheTest.php | 7 +++---- tests/Unit/CacheHtmlTest.php | 10 +++++----- 7 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/HtmlCacheServiceProvider.php b/src/HtmlCacheServiceProvider.php index 19b1939..6a5d209 100644 --- a/src/HtmlCacheServiceProvider.php +++ b/src/HtmlCacheServiceProvider.php @@ -5,10 +5,9 @@ use Illuminate\Support\ServiceProvider; /** - * The main service provider for this application + * The main service provider for this application. * * @category Core - * @package JKniest\HTMLCache * @author Jordan Kniest * @license MIT * @link https://jkniest.de @@ -16,16 +15,16 @@ class HtmlCacheServiceProvider extends ServiceProvider { /** - * Bootstrapping the package + * Bootstrapping the package. * * @return void */ public function boot() { $this->publishes([ - __DIR__ . '/config/htmlcache.php' => config_path('htmlcache.php') + __DIR__.'/config/htmlcache.php' => config_path('htmlcache.php'), ], 'htmlcache'); - $this->mergeConfigFrom(__DIR__ . '/config/htmlcache.php', 'htmlcache'); + $this->mergeConfigFrom(__DIR__.'/config/htmlcache.php', 'htmlcache'); } } diff --git a/src/Http/Middleware/CacheHtml.php b/src/Http/Middleware/CacheHtml.php index ebb34cc..0ba5931 100644 --- a/src/Http/Middleware/CacheHtml.php +++ b/src/Http/Middleware/CacheHtml.php @@ -12,7 +12,6 @@ * visit the page. The effects are that no database queries needs to be executed. * * @category Core - * @package JKniest\HTMLCache * @author Jordan Kniest * @license MIT * @link https://jkniest.de @@ -49,7 +48,7 @@ public function handle(Request $request, $next) /** * Generate the cache key for a given page. It will have the following syntax: - * PREFIX_PAGE_LOCALE_USERID + * PREFIX_PAGE_LOCALE_USERID. * * The prefix is configurable in the config file. The page is the current page the user * is visiting and the locale is the current locale (default: en). @@ -108,11 +107,10 @@ protected function getIgnored() */ protected function isEnabled(Request $request) { - return ( + return $request->method() === 'GET' && config('htmlcache.enabled') && - !$request->is(... $this->getIgnored()) - ); + ! $request->is(...$this->getIgnored()); } /** @@ -137,7 +135,7 @@ protected function getContent(Request $request, callable $next) $response = $next($request); if ($response->getStatusCode() !== 200) { - return null; + return; } return $response->getContent(); diff --git a/src/config/htmlcache.php b/src/config/htmlcache.php index 0fd8b0f..84aca6e 100644 --- a/src/config/htmlcache.php +++ b/src/config/htmlcache.php @@ -2,7 +2,7 @@ return [ - /** + /* * HTML cache enabled * * Should the html cache system be enabled? If this value is set to false it will @@ -13,7 +13,7 @@ */ 'enabled' => env('HTML_CACHE_ENABLED', true), - /** + /* * HTML cache prefix * * A cache prefix for the html cache. You should change this value if there are multiple @@ -25,7 +25,7 @@ */ 'prefix' => env('HTML_CACHE_PREFIX', 'html_'), - /** + /* * HTML cache time in minutes * * How many minutes should the response be cached? After this time (in minutes) the cache @@ -36,7 +36,7 @@ */ 'minutes' => env('HTML_CACHE_MINUTES', 360), - /** + /* * HTML cache user specific * * Should the cache be user specific? This would append the cache key by the user id. If the @@ -52,13 +52,13 @@ */ 'user_specific' => env('HTML_CACHE_USER_SPECIFIC', false), - /** + /* * HTML cache ignored routes * * These routes will be completely ignored by the caching. The only way to override these * values is to publish the configuration file. */ 'ignored' => [ - # /path/to/my/ignored/route - ] + // /path/to/my/ignored/route + ], ]; diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 2f76b7d..1828a47 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -2,11 +2,10 @@ namespace JKniest\Tests; -use Illuminate\Contracts\Debug\ExceptionHandler; +use Orchestra\Testbench\TestCase; use Illuminate\Support\Facades\Route; use JKniest\HtmlCache\HtmlCacheServiceProvider; use JKniest\HtmlCache\Http\Middleware\CacheHtml; -use Orchestra\Testbench\TestCase; class BaseTestCase extends TestCase { @@ -16,17 +15,17 @@ protected function setUp() Route::group(['middleware' => CacheHtml::class], function () { Route::any('/example', function () { - return 'Example value: ' . request('test'); + return 'Example value: '.request('test'); }); Route::get('/another', function () { - return 'Another value: ' . request('test'); + return 'Another value: '.request('test'); }); Route::get('/error', function () { - return response('Error: ' . request('test'), 500); + return response('Error: '.request('test'), 500); }); Route::get('/validation', function () { request()->validate([ - 'name' => 'required' + 'name' => 'required', ]); }); }); @@ -35,7 +34,7 @@ protected function setUp() protected function getPackageProviders($app) { return [ - HtmlCacheServiceProvider::class + HtmlCacheServiceProvider::class, ]; } -} \ No newline at end of file +} diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 365722f..52105e3 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -11,4 +11,4 @@ public function test_route() ->assertStatus(200) ->assertSee('Hello'); } -} \ No newline at end of file +} diff --git a/tests/Feature/CacheTest.php b/tests/Feature/CacheTest.php index 0adc0c5..4c326ce 100644 --- a/tests/Feature/CacheTest.php +++ b/tests/Feature/CacheTest.php @@ -2,8 +2,8 @@ namespace JKniest\Tests\Feature; -use Illuminate\Support\Facades\Config; use JKniest\Tests\BaseTestCase; +use Illuminate\Support\Facades\Config; class CacheTest extends BaseTestCase { @@ -140,7 +140,7 @@ public function it_can_ignore_specific_routes() { // Given: The route /another is ignored Config::set('htmlcache.ignored', [ - '/another' + '/another', ]); // When: The user visits the page with an attribute: Hello @@ -223,5 +223,4 @@ public function it_will_not_load_the_cache_of_a_page_if_the_error_bag_is_not_emp // And: The user should not see the Hello $response->assertDontSee('Hello'); } - -} \ No newline at end of file +} diff --git a/tests/Unit/CacheHtmlTest.php b/tests/Unit/CacheHtmlTest.php index 255ed6b..1f94b15 100644 --- a/tests/Unit/CacheHtmlTest.php +++ b/tests/Unit/CacheHtmlTest.php @@ -2,9 +2,9 @@ namespace JKniest\Tests\Unit; -use JKniest\HtmlCache\Http\Middleware\CacheHtml; -use Illuminate\Support\Facades\Config; use JKniest\Tests\BaseTestCase; +use Illuminate\Support\Facades\Config; +use JKniest\HtmlCache\Http\Middleware\CacheHtml; class CacheHtmlTest extends BaseTestCase { @@ -68,7 +68,7 @@ public function it_can_get_the_ignored_files() { // Given: A ignored route, named 'another' Config::set('htmlcache.ignored', [ - 'another' + 'another', ]); // When: We fetch the ignored routes @@ -84,7 +84,7 @@ public function it_removes_trailing_slashes_around_ignored_routes() // Given: A ignored route, named '/another/' Config::set('htmlcache.ignored', [ '/another/', - 'and/some/other/' + 'and/some/other/', ]); // When: We fetch the ignored routes @@ -106,4 +106,4 @@ public function mGetIgnored() { return $this->getIgnored(); } -} \ No newline at end of file +} From 86024da52f3138e621fed37fe4b6fd4de78cf934 Mon Sep 17 00:00:00 2001 From: Kniest Jordan Date: Sun, 7 Jan 2018 05:59:15 +0100 Subject: [PATCH 4/7] Now also caching all GET parameters --- src/Http/Middleware/CacheHtml.php | 27 ++++---- tests/BaseTestCase.php | 9 ++- tests/ExampleTest.php | 14 ---- tests/Feature/CacheTest.php | 104 +++++++++++++++++++++--------- tests/Unit/CacheHtmlTest.php | 8 +-- 5 files changed, 97 insertions(+), 65 deletions(-) delete mode 100644 tests/ExampleTest.php diff --git a/src/Http/Middleware/CacheHtml.php b/src/Http/Middleware/CacheHtml.php index 0ba5931..af791f3 100644 --- a/src/Http/Middleware/CacheHtml.php +++ b/src/Http/Middleware/CacheHtml.php @@ -19,15 +19,15 @@ class CacheHtml { /** - * Handle the incoming request. If the caching is disabled or the request is not a GET + * Handle the incoming request. If the caching is disabled or if the request is not a GET * request it will simply do nothing and only return the original response. * * If the caching is enabled and the user makes a GET request it tries to load the cached * version of this page. If there is no cached version the original response will be returned - * and cached for the next time. + * and cached for the next time a user visits the page. * - * @param Request $request The incoming request - * @param callable $next The next middleware + * @param Request $request The incoming request + * @param callable $next The next middleware * * @return Response */ @@ -50,8 +50,9 @@ public function handle(Request $request, $next) * Generate the cache key for a given page. It will have the following syntax: * PREFIX_PAGE_LOCALE_USERID. * - * The prefix is configurable in the config file. The page is the current page the user - * is visiting and the locale is the current locale (default: en). + * The prefix is configurable in the config file. The page is a hashed version of the current + * url (including GET parameters) that the user is visiting and the locale is the current + * locale (default: en). * * The user id is only set if the configuration value (htmlcache.user_specific) is set * to true. @@ -65,7 +66,7 @@ protected function getCacheKey(string $page) $prefix = config('htmlcache.prefix'); $locale = app()->getLocale(); - $page = str_replace('/', '_', trim($page, '/')); + $page = md5(trim($page, '/')); if (config('htmlcache.user_specific')) { $id = Auth::check() ? Auth::id() : -1; @@ -114,21 +115,21 @@ protected function isEnabled(Request $request) } /** - * Get the original or the cached response. If there is now cached version for the + * Get the original or the cached response. If there is no cached version for the * current page it will run the full request cycle and cache the given response, if * the returned status code is equals to 200. * - * Otherwise, if there is a cache version, it will not run the whole request cycle - * and simply return the cached html / response. + * Otherwise if there is a cache version, it will not run the whole request cycle + * and simply return the cached html response. * - * @param Request $request The incoming request - * @param callable $next The next middleware + * @param Request $request The incoming request + * @param callable $next The next middleware * * @return null|string */ protected function getContent(Request $request, callable $next) { - $key = $this->getCacheKey($request->path()); + $key = $this->getCacheKey($request->getRequestUri()); $time = config('htmlcache.minutes'); $content = Cache::remember($key, $time, function () use ($next, $request) { diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 1828a47..78b9a1c 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -14,14 +14,17 @@ protected function setUp() parent::setUp(); Route::group(['middleware' => CacheHtml::class], function () { - Route::any('/example', function () { + Route::get('/example', function () { + return 'Example value: '.session('test'); + }); + Route::any('/form', function () { return 'Example value: '.request('test'); }); Route::get('/another', function () { - return 'Another value: '.request('test'); + return 'Another value: '.session('test'); }); Route::get('/error', function () { - return response('Error: '.request('test'), 500); + return response('Error: '.session('test'), 500); }); Route::get('/validation', function () { request()->validate([ diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 52105e3..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -get('/example?test=Hello') - ->assertStatus(200) - ->assertSee('Hello'); - } -} diff --git a/tests/Feature/CacheTest.php b/tests/Feature/CacheTest.php index 4c326ce..f69382e 100644 --- a/tests/Feature/CacheTest.php +++ b/tests/Feature/CacheTest.php @@ -2,8 +2,8 @@ namespace JKniest\Tests\Feature; -use JKniest\Tests\BaseTestCase; use Illuminate\Support\Facades\Config; +use JKniest\Tests\BaseTestCase; class CacheTest extends BaseTestCase { @@ -14,33 +14,47 @@ protected function setUp() Config::set('htmlcache.prefix', 'test_'); } - /** @test */ + /** + * @test + * @throws \Exception + */ public function it_can_cache_the_result_of_a_response() { + // Given: The session data is 'Hello' + session(['test' => 'Hello']); + // When: The user visits the example page - $response = $this->get('/example?test=Hello'); + $path = 'example?test=Hello'; + $response = $this->get($path); // Then: They should see the word 'Hello' $response->assertStatus(200)->assertSee('Hello'); // And: A new cache entry should exists - $this->assertNotNull(cache('test_example_en')); + $key = 'test_'.md5($path).'_en'; + $this->assertNotNull(cache($key)); // Also: The content should be valid - $this->assertEquals('Example value: Hello', cache('test_example_en')); + $this->assertEquals('Example value: Hello', cache($key)); } /** @test */ public function the_cache_is_loaded_when_visiting_a_page_twice() { + // Given: A session data with the value 'Hello' + session(['test' => 'Hello']); + // When: The user visits the example page with the attribute Hello - $response = $this->get('/example?test=Hello'); + $response = $this->get('/example'); - // Then: They should see the word 'Hello' + // Then: They should see the word 'Hello' (because of the session value) $response->assertSee('Hello'); - // When: The user visits the page again with another attribute - $responseB = $this->get('/example?test=World'); + // Given: The session data changed to 'World' + session(['test' => 'World']); + + // When: The user visits the page again + $responseB = $this->get('/example'); // Then: They should not see World, but Hello $responseB->assertDontSee('World'); @@ -53,14 +67,20 @@ public function if_the_cache_is_disabled_the_normal_request_should_be_executed() // Given: The HTMLCache is disabled Config::set('htmlcache.enabled', false); + // Given: The session value "Hello" + session(['test' => 'Hello']); + // When: The user visits the example page with the attribute Hello - $response = $this->get('/example?test=Hello'); + $response = $this->get('/example'); // Then: They should see the word 'Hello' $response->assertSee('Hello'); - // When: The user visits the page again with another attribute - $responseB = $this->get('/example?test=World'); + // Given: The session value "World" + session(['test' => 'World']); + + // When: The user visits the page again + $responseB = $this->get('/example'); // Then: They should not see Hello, but World $responseB->assertDontSee('Hello'); @@ -71,13 +91,13 @@ public function if_the_cache_is_disabled_the_normal_request_should_be_executed() public function post_requests_are_ignored() { // When: The user sends a POST request to a given page - $response = $this->post('/example', ['test' => 'Hello']); + $response = $this->post('/form', ['test' => 'Hello']); // Then: They should see the word 'Hello' $response->assertSee('Hello'); // When: The user makes another POST request to the same url - $responseB = $this->post('/example', ['test' => 'World']); + $responseB = $this->post('/form', ['test' => 'World']); // Then: They should not see Hello, but World $responseB->assertDontSee('Hello'); @@ -88,13 +108,13 @@ public function post_requests_are_ignored() public function delete_requests_are_ignored() { // When: The user sends a DELETE request to a given page - $response = $this->delete('/example', ['test' => 'Hello']); + $response = $this->delete('/form', ['test' => 'Hello']); // Then: They should see the word 'Hello' $response->assertSee('Hello'); // When: The user makes another DELETE request to the same url - $responseB = $this->post('/example', ['test' => 'World']); + $responseB = $this->post('/form', ['test' => 'World']); // Then: They should not see Hello, but World $responseB->assertDontSee('Hello'); @@ -105,13 +125,13 @@ public function delete_requests_are_ignored() public function patch_requests_are_ignored() { // When: The user sends a PATCH request to a given page - $response = $this->patch('/example', ['test' => 'Hello']); + $response = $this->patch('/form', ['test' => 'Hello']); // Then: They should see the word 'Hello' $response->assertSee('Hello'); // When: The user makes another PATCH request to the same url - $responseB = $this->patch('/example', ['test' => 'World']); + $responseB = $this->patch('/form', ['test' => 'World']); // Then: They should not see Hello, but World $responseB->assertDontSee('Hello'); @@ -122,13 +142,13 @@ public function patch_requests_are_ignored() public function put_requests_are_ignored() { // When: The user sends a PUT request to a given page - $response = $this->put('/example', ['test' => 'Hello']); + $response = $this->put('/form', ['test' => 'Hello']); // Then: They should see the word 'Hello' $response->assertSee('Hello'); // When: The user makes another PUT request to the same url - $responseB = $this->put('/example', ['test' => 'World']); + $responseB = $this->put('/form', ['test' => 'World']); // Then: They should not see Hello, but World $responseB->assertDontSee('Hello'); @@ -143,14 +163,20 @@ public function it_can_ignore_specific_routes() '/another', ]); - // When: The user visits the page with an attribute: Hello - $response = $this->get('/another?test=hello'); + // Given: The session value is 'hello' + session(['test' => 'hello']); + + // When: The user visits the page + $response = $this->get('/another'); // Then: He should see the Hello $response->assertStatus(200)->assertSee('hello'); - // When: The user visits the same page with another attribute: World - $response = $this->get('/another?test=world'); + // Given: The session value is 'world' + session(['test' => 'world']); + + // When: The user visits the same page + $response = $this->get('/another'); // Then: He should see the World but not hello $response->assertStatus(200)->assertSee('world'); @@ -160,14 +186,20 @@ public function it_can_ignore_specific_routes() /** @test */ public function it_will_ignore_routes_that_are_not_returning_a_200_status_code() { + // Given: The session text is 'Hello' + session(['test' => 'Hello']); + // When: The user sends a GET request to a page which returns a 500 status code - $response = $this->get('/error?test=Hello'); + $response = $this->get('/error'); // Then: They should see the word 'Hello' $response->assertSee('Hello'); + // Given: The session text is 'World' + session(['test' => 'World']); + // When: The user makes another request to the same url with another parameter - $responseB = $this->get('/error?test=World'); + $responseB = $this->get('/error'); // Then: They should not see Hello, but World $responseB->assertDontSee('Hello'); @@ -193,17 +225,24 @@ public function it_will_not_cache_any_pages_if_the_error_bag_is_not_empty() $response->assertRedirect('/example'); // And: The cache key should not have been generated (or at least with the null content) - $this->assertNull(cache('test_example_en')); + $this->assertNull(cache('test_'.md5('/example').'_en')); } - /** @test */ + /** + * @test + * @throws \Exception + */ public function it_will_not_load_the_cache_of_a_page_if_the_error_bag_is_not_empty() { + // Given: The session data is 'Hello' + session(['test' => 'Hello']); + // Given: The user visited the example before - $response = $this->get('/example?test=Hello'); + $response = $this->get('/example'); // And: This page was being cached - $this->assertNotNull(cache('test_example_en')); + $md5 = md5('example'); + $this->assertNotNull(cache('test_'.$md5.'_en')); // Also: The user should see Hello $response->assertSee('Hello'); @@ -211,6 +250,9 @@ public function it_will_not_load_the_cache_of_a_page_if_the_error_bag_is_not_emp // Given: The user was earlier on the example page session()->setPreviousUrl(url('/example')); + // Given: The session data is 'World' + session(['test' => 'World']); + // When: The user sends a GET request to a page which will throw an validation exception $response = $this->get('/validation'); @@ -220,7 +262,7 @@ public function it_will_not_load_the_cache_of_a_page_if_the_error_bag_is_not_emp // And: The user should be redirected to the example page again $response->assertRedirect('/example'); - // And: The user should not see the Hello + // And: The user should not see the Hello, but world $response->assertDontSee('Hello'); } } diff --git a/tests/Unit/CacheHtmlTest.php b/tests/Unit/CacheHtmlTest.php index 1f94b15..9aa464e 100644 --- a/tests/Unit/CacheHtmlTest.php +++ b/tests/Unit/CacheHtmlTest.php @@ -18,7 +18,7 @@ public function it_can_generate_a_cache_key_based_on_the_page_and_prefix() $key = (new MockedCacheHtml)->mGetCacheKey('example'); // Then: The cache prefix should be: - $this->assertEquals('test_example_en', $key); + $this->assertEquals('test_'.md5('example').'_en', $key); } /** @test */ @@ -31,7 +31,7 @@ public function it_can_generate_a_cache_key_for_nested_resources() $key = (new MockedCacheHtml)->mGetCacheKey('example/123/another'); // Then: The cache prefix should be: - $this->assertEquals('test_example_123_another_en', $key); + $this->assertEquals('test_'.md5('example/123/another').'_en', $key); } /** @test */ @@ -44,7 +44,7 @@ public function it_removes_trailing_slashes() $key = (new MockedCacheHtml)->mGetCacheKey('/example/'); // Then: The cache prefix should be: - $this->assertEquals('test_example_en', $key); + $this->assertEquals('test_'.md5('example').'_en', $key); } /** @test */ @@ -60,7 +60,7 @@ public function it_uses_the_current_language() $key = (new MockedCacheHtml)->mGetCacheKey('example'); // Then: The cache prefix should be: - $this->assertEquals('test_example_de', $key); + $this->assertEquals('test_'.md5('example').'_de', $key); } /** @test */ From d7d31700f4b54b5d033371ecdc0a73b0206902f0 Mon Sep 17 00:00:00 2001 From: Jordan Kniest Date: Sun, 7 Jan 2018 04:59:39 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI [ci skip] [skip ci] --- tests/Feature/CacheTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/CacheTest.php b/tests/Feature/CacheTest.php index f69382e..cd9502f 100644 --- a/tests/Feature/CacheTest.php +++ b/tests/Feature/CacheTest.php @@ -2,8 +2,8 @@ namespace JKniest\Tests\Feature; -use Illuminate\Support\Facades\Config; use JKniest\Tests\BaseTestCase; +use Illuminate\Support\Facades\Config; class CacheTest extends BaseTestCase { From e5a6544c150dce9dd5af7879b1e414152a549208 Mon Sep 17 00:00:00 2001 From: Kniest Jordan Date: Sun, 7 Jan 2018 06:02:03 +0100 Subject: [PATCH 6/7] Added php 7.2 to travis configuration --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 60b7b7a..50d1d20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: php php: - 7.1 + - 7.2 cache: directories: From 37b64051874b9c7ece1edcd1721ad822032d0d72 Mon Sep 17 00:00:00 2001 From: Kniest Jordan Date: Sun, 7 Jan 2018 06:11:59 +0100 Subject: [PATCH 7/7] Updated readme and changelog --- LICENSE | 2 +- changelog.md | 6 ++++++ readme.md | 30 ++++++++++-------------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/LICENSE b/LICENSE index 04a665d..085c0ab 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2017 Jordan Kniest +Copyright Jordan Kniest Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the diff --git a/changelog.md b/changelog.md index 0e91345..0f0589a 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [1.0.3] - 2018-01-07 +### Changed +- GET parameters are now considered in the cache generation +- Offical PHP 7.2 support + ## [1.0.2] - 2017-11-21 ### Changed - Sites are not being cached if they do not have a 200 status code @@ -21,3 +26,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. [1.0.1]: https://github.com/jkniest/HTMLCache/compare/1.0.0...1.0.1 [1.0.2]: https://github.com/jkniest/HTMLCache/compare/1.0.1...1.0.2 +[1.0.3]: https://github.com/jkniest/HTMLCache/compare/1.0.2...1.0.3 diff --git a/readme.md b/readme.md index d301c92..931edf6 100644 --- a/readme.md +++ b/readme.md @@ -10,7 +10,7 @@ This package is made for you if you have a lot of static pages (or pages that do And it is highly customizable: You can even cache the same page for every user different, allowing that you can cache for example their account page or dashboard without worrying that another user can see these cached pages. -__One benefit against much other html caches:__ It will also cache the pages based on language, and (optionally) user id. And if there are special cases (for example a special GET-parameter) that needs to be used to generate multiple versions of the same page, the middleware can be easily modified. +__One benefit against much other html caches:__ It will also cache the pages based on language, and (optionally) user id. And if there are special cases (for example a specific session value) that needs to be used to generate multiple versions of the same page, the middleware can be easily modified. --- @@ -131,7 +131,7 @@ In a few cases the pages will not be cached: ## Configuration -You can nearly configure anything inside the `.env` file. +You can configure nearly anything inside the `.env` file. ### Enable / Disable cache @@ -191,21 +191,21 @@ This will create a new file in your project: `config/htmlcache.php`. There you c ## Clear cache -The html cache package uses the default laravel cache helpers. So you simple run the artisan command to clear the cache: +The html cache package uses the default laravel cache helpers. So you can run the artisan command to clear the cache: ```shell php artisan cache:clear ``` -This will remove every cached version of this plugin (and also of everything else). It is recommended to put this in your deployment workflow (for example in the deployment script in forge or as an deployment hook in envoyer) +This will remove every cached version of this plugin (and also everything else). It is recommended to put this in your deployment workflow (for example in the deployment script in forge or as a deployment hook in envoyer) --- ## Override middlware -It is possible to override the middleware. So you could override the cache-key generation. In this short tutorial we will add another field to the cache key generation (the post id). +It is possible to override the middleware. So you could override the cache-key generation. In this short tutorial we will add another field to the cache key generation (the current weekday). -Let's say you have a forum and you can access each page with a get parameter. For example: `http://my-forum.dev?page=3`. In the default implementation all pages would share the same cached results (which means that the pagination isn't working anymore). +Let's say you have a dashboard and all data on this dashboard will only update every weekday. In the default implementation you would have to set a maximum cache time (for example 1 day) but you can't be sure that the cache was generated exactly on 0am. The simplest solution would be to override the middleware and extends the cache-key generation. @@ -232,10 +232,11 @@ Now we can override any methods. The method `getCacheKey` handles the generation $prefix = config('htmlcache.prefix'); $locale = app()->getLocale(); - $page = str_replace('/', '_', trim($page, '/')); + $page = md5(trim($page, '/')); if (config('htmlcache.user_specific')) { $id = Auth::check() ? Auth::id() : -1; + return "{$prefix}{$page}_{$locale}_{$id}"; } @@ -250,9 +251,7 @@ Let's implement our own (in the HtmlCache middleware that we just created): { $key = parent::getCacheKey($page); - if (request('page') !== null) { - $key .= '_' . request('page'); - } + $key .= date('D'); return $key; } @@ -273,18 +272,9 @@ Of course you can always override any other method (like the `Handle` method its --- -## Roadmap - -These are features that are planned for to upcoming versions. If you have any suggestion please let me know via issues or e-mail me at `contact@jkniest.de` - -### Version 1.1.0 -- Add native pagination support (so that the page GET parameter will also be cached) - ---- - ## License -Copyright 2017 Jordan Kniest +Copyright Jordan Kniest Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the