-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from GeneaLabs/laravel-5.5
Laravel 5.5
- Loading branch information
Showing
13 changed files
with
175 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
return [ | ||
'store' => env('MODEL_CACHE_STORE'), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php namespace GeneaLabs\LaravelModelCaching\Providers; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
|
||
class Service extends ServiceProvider | ||
{ | ||
protected $defer = false; | ||
|
||
public function boot() | ||
{ | ||
$configPath = __DIR__ . '/../../config/laravel-model-caching.php'; | ||
$this->mergeConfigFrom($configPath, 'laravel-model-caching'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,55 @@ | ||
<?php namespace GeneaLabs\LaravelModelCaching\Traits; | ||
|
||
use GeneaLabs\LaravelModelCaching\CacheKey; | ||
use GeneaLabs\LaravelModelCaching\CacheTags; | ||
use GeneaLabs\LaravelModelCaching\CachedModel; | ||
use Illuminate\Cache\TaggableStore; | ||
|
||
trait Cachable | ||
{ | ||
protected $isCachable = true; | ||
|
||
protected function cache(array $tags = []) | ||
{ | ||
$cache = cache(); | ||
|
||
if (config('laravel-model-caching.store')) { | ||
$cache = $cache->store(config('laravel-model-caching.store')); | ||
} | ||
|
||
if (is_subclass_of($cache->getStore(), TaggableStore::class)) { | ||
if (is_a($this, CachedModel::class)) { | ||
array_push($tags, str_slug(get_called_class())); | ||
} | ||
|
||
$cache = $cache->tags($tags); | ||
} | ||
|
||
return $cache; | ||
} | ||
|
||
public function disableCache() | ||
{ | ||
session(['genealabs-laravel-model-caching-is-disabled' => true]); | ||
$this->isCachable = false; | ||
|
||
return $this; | ||
} | ||
|
||
public function flushCache(array $tags = []) | ||
{ | ||
$this->cache($tags)->flush(); | ||
} | ||
|
||
protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string | ||
{ | ||
return (new CacheKey($this->eagerLoad, $this->model, $this->query)) | ||
->make($columns, $idColumn); | ||
} | ||
|
||
protected function makeCacheTags() : array | ||
{ | ||
return (new CacheTags($this->eagerLoad, $this->model)) | ||
->make(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit; | ||
|
||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedAuthor; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedBook; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedProfile; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedPublisher; | ||
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\UncachedStore; | ||
use GeneaLabs\LaravelModelCaching\Tests\TestCase; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
|
||
class CachableTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
cache()->flush(); | ||
$publishers = factory(Publisher::class, 10)->create(); | ||
factory(Author::class, 10)->create() | ||
->each(function ($author) use ($publishers) { | ||
factory(Book::class, random_int(2, 10))->make() | ||
->each(function ($book) use ($author, $publishers) { | ||
$book->author()->associate($author); | ||
$book->publisher()->associate($publishers[rand(0, 9)]); | ||
$book->save(); | ||
}); | ||
factory(Profile::class)->make([ | ||
'author_id' => $author->id, | ||
]); | ||
}); | ||
|
||
$bookIds = (new Book)->all()->pluck('id'); | ||
factory(Store::class, 10)->create() | ||
->each(function ($store) use ($bookIds) { | ||
$store->books()->sync(rand($bookIds->min(), $bookIds->max())); | ||
}); | ||
cache()->flush(); | ||
} | ||
|
||
public function testSpecifyingAlternateCacheDriver() | ||
{ | ||
$configCacheStores = config('cache.stores'); | ||
$configCacheStores['customCache'] = ['driver' => 'array']; | ||
config(['cache.stores' => $configCacheStores]); | ||
config(['laravel-model-caching.store' => 'customCache']); | ||
$key = 'genealabslaravelmodelcachingtestsfixturesauthor'; | ||
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; | ||
|
||
$authors = (new Author) | ||
->all(); | ||
$defaultcacheResults = cache() | ||
->tags($tags) | ||
->get($key); | ||
$customCacheResults = cache() | ||
->store('customCache') | ||
->tags($tags) | ||
->get($key); | ||
$liveResults = (new UncachedAuthor) | ||
->all(); | ||
|
||
$this->assertEquals($customCacheResults, $authors); | ||
$this->assertNull($defaultcacheResults); | ||
$this->assertEmpty($liveResults->diffAssoc($customCacheResults)); | ||
} | ||
} |