diff --git a/src/CachedModel.php b/src/CachedModel.php index 790c45b..a2620f6 100644 --- a/src/CachedModel.php +++ b/src/CachedModel.php @@ -29,6 +29,7 @@ public function newEloquentBuilder($query) public static function boot() { parent::boot(); + $class = get_called_class(); $instance = new $class; diff --git a/src/Console/Commands/Flush.php b/src/Console/Commands/Flush.php new file mode 100644 index 0000000..cd80818 --- /dev/null +++ b/src/Console/Commands/Flush.php @@ -0,0 +1,17 @@ +option('model'); + $model = new $option; + $model->flushCache(); + $this->info("Cache for model '{$option}' flushed."); + } +} diff --git a/src/Providers/Service.php b/src/Providers/Service.php index 64afdbb..41c3e0f 100644 --- a/src/Providers/Service.php +++ b/src/Providers/Service.php @@ -1,5 +1,6 @@ mergeConfigFrom($configPath, 'laravel-model-caching'); + $this->commands(Flush::class); } } diff --git a/tests/Unit/Console/Commands/FlushTest.php b/tests/Unit/Console/Commands/FlushTest.php new file mode 100644 index 0000000..3a03912 --- /dev/null +++ b/tests/Unit/Console/Commands/FlushTest.php @@ -0,0 +1,85 @@ +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 testGivenModelIsFlushed() + { + $authors = (new Author)->all(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor'; + $tags = ['genealabslaravelmodelcachingtestsfixturesauthor']; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + $result = $this->artisan('modelCache:flush', ['--model' => Author::class]); + $flushedResults = cache() + ->tags($tags) + ->get($key); + + $this->assertEquals($authors, $cachedResults); + $this->assertEmpty($flushedResults); + $this->assertEquals($result, 0); + } + + public function testGivenModelWithRelationshipIsFlushed() + { + $authors = (new Author)->with('books')->get(); + $key = 'genealabslaravelmodelcachingtestsfixturesauthor-books'; + $tags = [ + 'genealabslaravelmodelcachingtestsfixturesauthor', + 'genealabslaravelmodelcachingtestsfixturesbook', + ]; + + $cachedResults = cache() + ->tags($tags) + ->get($key); + $result = $this->artisan( + 'modelCache:flush', + ['--model' => Author::class] + ); + $flushedResults = cache() + ->tags($tags) + ->get($key); + + $this->assertEquals($authors, $cachedResults); + $this->assertEmpty($flushedResults); + $this->assertEquals($result, 0); + } +} diff --git a/tests/Unit/DisabledCachedBuilderTest.php b/tests/Unit/DisabledCachedBuilderTest.php index 249a994..f6eccd8 100644 --- a/tests/Unit/DisabledCachedBuilderTest.php +++ b/tests/Unit/DisabledCachedBuilderTest.php @@ -169,6 +169,7 @@ public function testCursorModelResultsIsNotCached() public function testFindModelResultsIsNotCached() { $author = (new Author) + ->with('books') ->disableCache() ->find(1); $key = 'genealabslaravelmodelcachingtestsfixturesauthor_1';