Skip to content

Commit d41921f

Browse files
committed
Add ability to flush model-cache via command
1 parent 1b6933c commit d41921f

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

src/Console/Commands/Flush.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
class Flush extends Command
66
{
77
protected $signature = 'modelCache:flush {--model=}';
8-
protected $description = 'Flush cache for a given model.';
8+
protected $description = 'Flush cache for a given model. If no model is given, entire model-cache is flushed.';
99

1010
public function handle()
1111
{
1212
$option = $this->option('model');
1313

1414
if (! $option) {
15-
$this->error("You must specify a model to flush a model's cache:");
16-
$this->line("modelCache:flush --model=App\\Model");
15+
cache()
16+
->store(config('laravel-model-caching:store'))
17+
->flush();
1718

18-
return 1;
19+
return 0;
1920
}
2021

2122
$model = new $option;

tests/Unit/Console/Commands/FlushTest.php

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ class FlushTest extends UnitTestCase
1313
{
1414
use RefreshDatabase;
1515

16+
protected $cache;
17+
1618
public function setUp()
1719
{
1820
parent::setUp();
1921

20-
cache()->flush();
22+
$this->cache = cache()->store(config('laravel-model-caching:store'));
23+
24+
$this->cache->flush();
2125
$publishers = factory(Publisher::class, 10)->create();
2226
factory(Author::class, 10)->create()
2327
->each(function ($author) use ($publishers) {
@@ -37,7 +41,7 @@ public function setUp()
3741
->each(function ($store) use ($bookIds) {
3842
$store->books()->sync(rand($bookIds->min(), $bookIds->max()));
3943
});
40-
cache()->flush();
44+
$this->cache->flush();
4145
}
4246

4347
public function testGivenModelIsFlushed()
@@ -46,11 +50,11 @@ public function testGivenModelIsFlushed()
4650
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
4751
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
4852

49-
$cachedResults = cache()
53+
$cachedResults = $this->cache
5054
->tags($tags)
5155
->get($key)['value'];
5256
$result = $this->artisan('modelCache:flush', ['--model' => Author::class]);
53-
$flushedResults = cache()
57+
$flushedResults = $this->cache
5458
->tags($tags)
5559
->get($key)['value'];
5660

@@ -68,14 +72,14 @@ public function testGivenModelWithRelationshipIsFlushed()
6872
'genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook',
6973
];
7074

71-
$cachedResults = cache()
75+
$cachedResults = $this->cache
7276
->tags($tags)
7377
->get($key)['value'];
7478
$result = $this->artisan(
7579
'modelCache:flush',
7680
['--model' => Author::class]
7781
);
78-
$flushedResults = cache()
82+
$flushedResults = $this->cache
7983
->tags($tags)
8084
->get($key)['value'];
8185

@@ -94,10 +98,52 @@ public function testNonCachedModelsCannotBeFlushed()
9498
$this->assertEquals($result, 1);
9599
}
96100

97-
public function testModelOptionIsSpecified()
101+
public function testAllModelsAreFlushed()
98102
{
99-
$result = $this->artisan('modelCache:flush', []);
103+
(new Author)->all();
104+
(new Book)->all();
105+
(new Store)->all();
100106

101-
$this->assertEquals($result, 1);
107+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
108+
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
109+
$cachedAuthors = $this->cache
110+
->tags($tags)
111+
->get($key)['value'];
112+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook');
113+
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook'];
114+
$cachedBooks = $this->cache
115+
->tags($tags)
116+
->get($key)['value'];
117+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore');
118+
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore'];
119+
$cachedStores = $this->cache
120+
->tags($tags)
121+
->get($key)['value'];
122+
123+
$this->assertNotEmpty($cachedAuthors);
124+
$this->assertNotEmpty($cachedBooks);
125+
$this->assertNotEmpty($cachedStores);
126+
127+
$this->artisan('modelCache:flush', []);
128+
129+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor');
130+
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesauthor'];
131+
$cachedAuthors = $this->cache
132+
->tags($tags)
133+
->get($key)['value'];
134+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook');
135+
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesbook'];
136+
$cachedBooks = $this->cache
137+
->tags($tags)
138+
->get($key)['value'];
139+
$key = sha1('genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore');
140+
$tags = ['genealabs:laravel-model-caching:genealabslaravelmodelcachingtestsfixturesstore'];
141+
$cachedStores = $this->cache
142+
->tags($tags)
143+
->get($key)['value'];
144+
145+
$this->assertEmpty($cachedAuthors);
146+
$this->assertEmpty($cachedBooks);
147+
$this->assertEmpty($cachedStores);
102148
}
103149
}

0 commit comments

Comments
 (0)