Skip to content

Commit 12aace3

Browse files
committed
Add functionality to flush model-specific cache
Fixes #45
1 parent b09e1a6 commit 12aace3

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

src/CachedModel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function newEloquentBuilder($query)
2929
public static function boot()
3030
{
3131
parent::boot();
32+
3233
$class = get_called_class();
3334
$instance = new $class;
3435

src/Console/Commands/Flush.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Console\Commands;
2+
3+
use Illuminate\Console\Command;
4+
5+
class Flush extends Command
6+
{
7+
protected $signature = 'modelCache:flush {--model=}';
8+
protected $description = 'Flush cache for a given model.';
9+
10+
public function handle()
11+
{
12+
$option = $this->option('model');
13+
$model = new $option;
14+
$model->flushCache();
15+
}
16+
}

src/Providers/Service.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace GeneaLabs\LaravelModelCaching\Providers;
22

3+
use GeneaLabs\LaravelModelCaching\Console\Commands\Flush;
34
use Illuminate\Support\ServiceProvider;
45

56
class Service extends ServiceProvider
@@ -10,5 +11,6 @@ public function boot()
1011
{
1112
$configPath = __DIR__ . '/../../config/laravel-model-caching.php';
1213
$this->mergeConfigFrom($configPath, 'laravel-model-caching');
14+
$this->commands(Flush::class);
1315
}
1416
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit;
2+
3+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
4+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Book;
5+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Profile;
6+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Publisher;
7+
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Store;
8+
use GeneaLabs\LaravelModelCaching\Tests\TestCase;
9+
use Illuminate\Foundation\Testing\RefreshDatabase;
10+
11+
class FlushTest extends TestCase
12+
{
13+
use RefreshDatabase;
14+
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
cache()->flush();
20+
$publishers = factory(Publisher::class, 10)->create();
21+
factory(Author::class, 10)->create()
22+
->each(function ($author) use ($publishers) {
23+
factory(Book::class, random_int(2, 10))->make()
24+
->each(function ($book) use ($author, $publishers) {
25+
$book->author()->associate($author);
26+
$book->publisher()->associate($publishers[rand(0, 9)]);
27+
$book->save();
28+
});
29+
factory(Profile::class)->make([
30+
'author_id' => $author->id,
31+
]);
32+
});
33+
34+
$bookIds = (new Book)->all()->pluck('id');
35+
factory(Store::class, 10)->create()
36+
->each(function ($store) use ($bookIds) {
37+
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
38+
});
39+
cache()->flush();
40+
}
41+
42+
public function testGivenModelIsFlushed()
43+
{
44+
$authors = (new Author)->all();
45+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
46+
$tags = ['genealabslaravelmodelcachingtestsfixturesauthor'];
47+
48+
$cachedResults = cache()
49+
->tags($tags)
50+
->get($key);
51+
$this->artisan('modelCache:flush', ['--model' => Author::class]);
52+
$flushedResults = cache()
53+
->tags($tags)
54+
->get($key);
55+
56+
$this->assertEquals($authors, $cachedResults);
57+
$this->assertEmpty($flushedResults);
58+
}
59+
60+
public function testGivenModelWithRelationshipIsFlushed()
61+
{
62+
$authors = (new Author)->with('books')->get();
63+
$key = 'genealabslaravelmodelcachingtestsfixturesauthor-books';
64+
$tags = [
65+
'genealabslaravelmodelcachingtestsfixturesauthor',
66+
'genealabslaravelmodelcachingtestsfixturesbook',
67+
];
68+
69+
$cachedResults = cache()
70+
->tags($tags)
71+
->get($key);
72+
$this->artisan('modelCache:flush', ['--model' => Author::class]);
73+
$flushedResults = cache()
74+
->tags($tags)
75+
->get($key);
76+
77+
$this->assertEquals($authors, $cachedResults);
78+
$this->assertEmpty($flushedResults);
79+
}
80+
}

0 commit comments

Comments
 (0)