Skip to content

Commit

Permalink
Add connection name to cache prefix
Browse files Browse the repository at this point in the history
Fixes #99
  • Loading branch information
mikebronner committed Mar 10, 2018
1 parent e9210ff commit b4e003d
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 181 deletions.
11 changes: 3 additions & 8 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
<?php namespace GeneaLabs\LaravelModelCaching;

use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;

class CacheKey
{
use CachePrefixing;

protected $eagerLoad;
protected $model;
protected $query;

protected function getCachePrefix() : string
{
return "genealabs:laravel-model-caching:"
. (config("laravel-model-caching.cache-prefix")
? config("laravel-model-caching.cache-prefix", "") . ":"
: "");
}

public function __construct(
array $eagerLoad,
Model $model,
Expand Down
21 changes: 11 additions & 10 deletions src/CacheTags.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
<?php namespace GeneaLabs\LaravelModelCaching;

use GeneaLabs\LaravelModelCaching\CachedBuilder;
use GeneaLabs\LaravelModelCaching\Traits\CachePrefixing;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;

class CacheTags
{
use CachePrefixing;

protected $eagerLoad;
protected $model;
protected $query;

public function __construct(array $eagerLoad, Model $model)
{
public function __construct(
array $eagerLoad,
Model $model,
Builder $query
) {
$this->eagerLoad = $eagerLoad;
$this->model = $model;
}

protected function getCachePrefix() : string
{
return "genealabs:laravel-model-caching:"
. (config('laravel-model-caching.cache-prefix')
? config('laravel-model-caching.cache-prefix', '') . ":"
: "");
$this->query = $query;
}

public function make() : array
Expand Down
9 changes: 6 additions & 3 deletions src/Console/Commands/Flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ protected function flushModelCache(string $option) : int
return 0;
}

protected function getAllTraitsUsedByClass(string $classname, bool $autoload = true) : Collection
{
protected function getAllTraitsUsedByClass(
string $classname,
bool $autoload = true
) : Collection {
$traits = collect();

if (class_exists($classname, $autoload)) {
Expand All @@ -60,7 +62,8 @@ protected function getAllTraitsUsedByClass(string $classname, bool $autoload = t
$parentClass = get_parent_class($classname);

if ($parentClass) {
$traits = $traits->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
$traits = $traits
->merge($this->getAllTraitsUsedByClass($parentClass, $autoload));
}

return $traits;
Expand Down
18 changes: 18 additions & 0 deletions src/Traits/CachePrefixing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php namespace GeneaLabs\LaravelModelCaching\Traits;

trait CachePrefixing
{
protected function getCachePrefix() : string
{
return "genealabs:laravel-model-caching:"
. $this->getDatabaseConnectionName() . ":"
. (config("laravel-model-caching.cache-prefix")
? config("laravel-model-caching.cache-prefix", "") . ":"
: "");
}

protected function getDatabaseConnectionName() : string
{
return $this->query->connection->getName();
}
}
6 changes: 5 additions & 1 deletion src/Traits/Caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ protected function makeCacheKey(

protected function makeCacheTags() : array
{
$tags = (new CacheTags($this->eagerLoad ?? [], $this->model ?? $this))
$eagerLoad = $this->eagerLoad ?? [];
$model = $this->model ?? $this;
$query = $this->query ?? app(Builder::class);

$tags = (new CacheTags($eagerLoad, $model, $query))
->make();

return $tags;
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/CachedBuilderPaginationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function testPaginationIsCached()
$authors = (new Author)
->paginate(3);

$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
];

$cachedResults = $this->cache()
Expand Down
230 changes: 115 additions & 115 deletions tests/Integration/CachedBuilderTest.php

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions tests/Integration/CachedModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class CachedModelTest extends IntegrationTestCase
public function testAllModelResultsCreatesCache()
{
$authors = (new Author)->all();
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
];

$cachedResults = $this
Expand All @@ -39,8 +39,8 @@ public function testAllModelResultsCreatesCache()

public function testScopeDisablesCaching()
{
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
$authors = (new Author)
->where("name", "Bruno")
->disableCache()
Expand All @@ -56,8 +56,8 @@ public function testScopeDisablesCaching()

public function testScopeDisablesCachingWhenCalledOnModel()
{
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
$authors = (new PrefixedAuthor)
->disableCache()
->where("name", "Bruno")
Expand All @@ -74,8 +74,8 @@ public function testScopeDisablesCachingWhenCalledOnModel()
public function testScopeDisableCacheDoesntCrashWhenCachingIsDisabledInConfig()
{
config(['laravel-model-caching.disabled' => true]);
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
$authors = (new PrefixedAuthor)
->where("name", "Bruno")
->disableCache()
Expand All @@ -94,9 +94,9 @@ public function testAllMethodCachingCanBeDisabledViaConfig()
config(['laravel-model-caching.disabled' => true]);
$authors = (new Author)
->all();
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
];
config(['laravel-model-caching.disabled' => false]);

Expand All @@ -119,10 +119,10 @@ public function testWhereHasIsBeingCached()
})
->get();

$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook_exists_and_books.author_id_=_authors.id-id_=_1-author');
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook_exists_and_books.author_id_=_authors.id-id_=_1-author');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook',
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
];

$cachedResults = $this
Expand Down
38 changes: 19 additions & 19 deletions tests/Integration/Console/Commands/FlushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class FlushTest extends IntegrationTestCase
public function testGivenModelIsFlushed()
{
$authors = (new Author)->all();
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];

$cachedResults = $this->cache
->tags($tags)
Expand All @@ -38,8 +38,8 @@ public function testExtendedModelIsFlushed()
$authors = (new PrefixedAuthor)
->get();

$key = sha1('genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor');
$tags = ['genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor');
$tags = ['genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'];

$cachedResults = $this
->cache
Expand All @@ -59,10 +59,10 @@ public function testExtendedModelIsFlushed()
public function testGivenModelWithRelationshipIsFlushed()
{
$authors = (new Author)->with('books')->get();
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-books');
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-books');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook',
];

$cachedResults = $this->cache
Expand Down Expand Up @@ -97,18 +97,18 @@ public function testAllModelsAreFlushed()
(new Book)->all();
(new Store)->all();

$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
$cachedAuthors = $this->cache
->tags($tags)
->get($key)['value'];
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook'];
$cachedBooks = $this->cache
->tags($tags)
->get($key)['value'];
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore'];
$cachedStores = $this->cache
->tags($tags)
->get($key)['value'];
Expand All @@ -119,18 +119,18 @@ public function testAllModelsAreFlushed()

$this->artisan('modelCache:flush');

$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];
$cachedAuthors = $this->cache
->tags($tags)
->get($key)['value'];
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesbook'];
$cachedBooks = $this->cache
->tags($tags)
->get($key)['value'];
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesstore'];
$cachedStores = $this->cache
->tags($tags)
->get($key)['value'];
Expand Down
4 changes: 2 additions & 2 deletions tests/Integration/DisabledCachedBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ public function testPaginationIsCached()
->disableCache()
->paginate(3);

$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor-paginate_by_3_page_1');
$tags = [
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
];

$cachedResults = $this->cache()
Expand Down
16 changes: 8 additions & 8 deletions tests/Integration/Traits/CachableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function testSpecifyingAlternateCacheDriver()
// TODO: make sure the alternate cache is actually loaded
config(['cache.stores' => $configCacheStores]);
config(['laravel-model-caching.store' => 'customCache']);
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
$key = sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor');
$tags = ['genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'];

$authors = (new Author)
->all();
Expand All @@ -53,9 +53,9 @@ public function testSetCachePrefixAttribute()
$results = $this->
cache()
->tags([
'genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor',
'genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor',
])
->get(sha1('genealabs:laravel-model-caching:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'))['value'];
->get(sha1('genealabs:laravel-model-caching:testing:test-prefix:genealabslaravelmodelcachingtestsfixturesprefixedauthor'))['value'];

$this->assertNotNull($results);
}
Expand All @@ -69,9 +69,9 @@ public function testAllReturnsCollection()
$cachedResults = $this
->cache()
->tags([
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
])
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
->get(sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'))['value'];
$liveResults = (new UncachedAuthor)->all();

$this->assertInstanceOf(Collection::class, $authors);
Expand All @@ -87,9 +87,9 @@ public function testsCacheFlagDisablesCaching()
$cachedAuthors = $this
->cache()
->tags([
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor',
'genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor',
])
->get(sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'));
->get(sha1('genealabs:laravel-model-caching:testing:genealabslaravelmodelcachingtestsfixturesauthor'));

config(['laravel-model-caching.disabled' => false]);

Expand Down

0 comments on commit b4e003d

Please sign in to comment.