Skip to content

Commit

Permalink
This adds a new cache key to add the current baseURL to the list of k…
Browse files Browse the repository at this point in the history
…eys. This prevents pagination links from being incorrectly cached on a website which has multiple domains.
  • Loading branch information
Drewdan committed Jul 27, 2021
1 parent b8ffa34 commit 4cc4f11
Show file tree
Hide file tree
Showing 42 changed files with 157 additions and 133 deletions.
7 changes: 7 additions & 0 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use Ramsey\Uuid\Uuid;

Expand Down Expand Up @@ -45,6 +46,7 @@ public function make(
$key .= $this->getOffsetClause();
$key .= $this->getLimitClause();
$key .= $this->getBindingsSlug();
$key .= $this->getPathSlug();
$key .= $keyDifferentiator;
$key .= $this->macroKey;
// dump($key);
Expand Down Expand Up @@ -362,4 +364,9 @@ protected function getBindingsSlug() : string

return Arr::query($this->model->query()->getBindings());
}

protected function getPathSlug() : string
{
return ':' . strtolower(URL::to('/'));
}
}
17 changes: 17 additions & 0 deletions tests/Feature/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Feature;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Artisan;
use GeneaLabs\LaravelModelCaching\Tests\FeatureTestCase;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;

Expand Down Expand Up @@ -82,4 +86,17 @@ public function testCustomPagination()

$response->see($page2ActiveLink);
}

public function testPaginationUrlIsCorrect() {
$this->baseUrl = 'https://test.local';

$this->visit("pagination-test2?custom-page=2")
->see('https://test.local/pagination-test2?custom-page=1');

$this->baseUrl = 'https://changed.local';

$this->visit("pagination-test2?custom-page=2")
->see('https://changed.local/pagination-test2?custom-page=1');

}
}
10 changes: 5 additions & 5 deletions tests/Integration/CachedBuilder/BelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function testLazyLoadingRelationship(): void
->books
->first()
->id;
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:stores:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:stores:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
];
Expand Down Expand Up @@ -52,7 +52,7 @@ public function testInvalidatingCacheWhenAttaching()
->books
->first()
->id;
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:stores:genealabslaravelmodelcachingtestsfixturesstore-testing:{$this->testingSqlitePath}testing.sqlite:books-first");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:stores:genealabslaravelmodelcachingtestsfixturesstore-testing:{$this->testingSqlitePath}testing.sqlite:books-first:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
];
Expand Down Expand Up @@ -85,7 +85,7 @@ public function testInvalidatingCacheWhenDetaching()
->books
->first()
->id;
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
];
Expand Down Expand Up @@ -116,7 +116,7 @@ public function testInvalidatingCacheWhenUpdating()
->books
->first()
->id;
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesstore",
];
Expand Down Expand Up @@ -146,7 +146,7 @@ public function testUncachedRelatedModelDoesntCache()
->books
->first()
->id;
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:book-store:genealabslaravelmodelcachingcachedbelongstomany-book_store.book_id_=_{$bookId}:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesuncachedstore",
];
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/CachedBuilder/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class BooleanTest extends IntegrationTestCase
{
public function testBooleanWhereCreatesCorrectCacheKey()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_1-authors.deleted_at_null");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-is_famous_=_1-authors.deleted_at_null:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
];
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilder/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class DateTimeTest extends IntegrationTestCase
public function testWhereClauseWorksWithCarbonDate()
{
$dateTime = now()->subYears(10);
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_{$dateTime}");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_{$dateTime}:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
];
Expand All @@ -38,7 +38,7 @@ public function testWhereClauseWorksWithDateTimeObject()
$dateTime = (new DateTime('@' . time()))
->sub(new DateInterval("P10Y"));
$dateTimeString = $dateTime->format("Y-m-d-H-i-s");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_{$dateTimeString}");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-publish_at_>_{$dateTimeString}:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
];
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/CachedBuilder/DeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function testDecrementingInvalidatesCache()
$book = (new Book)
->orderBy("id", "DESC")
->first();
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook_orderBy_id_desc-first");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook_orderBy_id_desc:http://localhost-first");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
];
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/CachedBuilder/FindOrFailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testFindOrFailCachesModels()
$author = (new Author)
->findOrFail(1);

$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-find_1");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null:http://localhost-find_1");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
];
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilder/FindTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testFindMultipleModelResultsCreatesCache()
{
$authors = (new Author)
->find([1, 2, 3]);
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-find_list_1_2_3");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null:http://localhost-find_list_1_2_3");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
];
Expand Down Expand Up @@ -66,7 +66,7 @@ public function testFindWithSingleElementArrayDoesntConflictWithNormalFind()
->find(1);
$author2 = (new Author)
->find([1]);

$this->assertNotEquals($author1, $author2);
$this->assertIsIterable($author2);
$this->assertEquals(Author::class, get_class($author1));
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilder/ForceDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testForceDeleteClearsCache()
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
])
->get(sha1(
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-id_=_1-authors.deleted_at_null"
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-id_=_1-authors.deleted_at_null:http://localhost"
))["value"];

(new Author)
Expand All @@ -29,7 +29,7 @@ public function testForceDeleteClearsCache()
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
])
->get(sha1(
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-id_=_1"
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-id_=_1:http://localhost"
))["value"]
?? null;

Expand Down
6 changes: 3 additions & 3 deletions tests/Integration/CachedBuilder/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testGetModelResultsCreatesCache()
$authors = (new Author)
->with('books', 'profile')
->get();
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:books-testing:{$this->testingSqlitePath}testing.sqlite:profile:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
Expand Down Expand Up @@ -49,7 +49,7 @@ public function testAccessingGetResultsViaArrayIndexDoesNotError()

public function testGetWithFieldArrayCachesResults()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id_name-authors.deleted_at_null");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id_name-authors.deleted_at_null:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
];
Expand All @@ -69,7 +69,7 @@ public function testGetWithFieldArrayCachesResults()

public function testGetWithFieldStringCachesResults()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id-authors.deleted_at_null");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor_id-authors.deleted_at_null:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
];
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilder/HasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HasManyTest extends IntegrationTestCase
{
public function testEagerloadedHasMany()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_inraw_1");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_inraw_1:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
];
Expand All @@ -34,7 +34,7 @@ public function testEagerloadedHasMany()

public function testLazyloadedHasMany()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:books:genealabslaravelmodelcachingtestsfixturesbook-books.author_id_=_1-books.author_id_notnull:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesbook",
];
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilder/HasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HasManyThroughTest extends IntegrationTestCase
{
public function testEagerloadedHasManyThrough()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:printers-first");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-testing:{$this->testingSqlitePath}testing.sqlite:printers:http://localhost-first");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprinter",
Expand Down Expand Up @@ -37,7 +37,7 @@ public function testEagerloadedHasManyThrough()

public function testLazyloadedHasManyThrough()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-authors.id_=_1-testing:{$this->testingSqlitePath}testing.sqlite:printers-limit_1");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:authors:genealabslaravelmodelcachingtestsfixturesauthor-authors.deleted_at_null-authors.id_=_1:http://localhost-testing:{$this->testingSqlitePath}testing.sqlite:printers-limit_1");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesauthor",
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturesprinter",
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilder/HasOneThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class HasOneThroughTest extends IntegrationTestCase
{
public function testEagerloadedHasOneThrough()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:suppliers:genealabslaravelmodelcachingtestsfixturessupplier-testing:{$this->testingSqlitePath}testing.sqlite:history-limit_1");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:suppliers:genealabslaravelmodelcachingtestsfixturessupplier-testing:{$this->testingSqlitePath}testing.sqlite:history-limit_1:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturessupplier",
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixtureshistory",
Expand Down Expand Up @@ -39,7 +39,7 @@ public function testEagerloadedHasOneThrough()

public function testLazyloadedHasOneThrough()
{
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:suppliers:genealabslaravelmodelcachingtestsfixturessupplier-testing:{$this->testingSqlitePath}testing.sqlite:history-limit_1");
$key = sha1("genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:suppliers:genealabslaravelmodelcachingtestsfixturessupplier-testing:{$this->testingSqlitePath}testing.sqlite:history-limit_1:http://localhost");
$tags = [
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixturessupplier",
"genealabs:laravel-model-caching:testing:{$this->testingSqlitePath}testing.sqlite:genealabslaravelmodelcachingtestsfixtureshistory",
Expand Down
Loading

0 comments on commit 4cc4f11

Please sign in to comment.