Skip to content

Commit 6b65b0d

Browse files
committed
Merge branch 'master' into develop
2 parents 8feace0 + 2691181 commit 6b65b0d

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.50] - 10 Mar 2018
8+
### Added
9+
- cache invalidation when `destroy()`ing models.
10+
11+
### Fixed
12+
- cache tag generation when calling `all()` queries that prevented proper
13+
cache invalidation.
14+
715
## [0.2.49] - 9 Mar 2018
816
### Fixed
917
- caching of `->first()` queries.

src/Traits/ModelCaching.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static function all($columns = ['*'])
1313

1414
$class = get_called_class();
1515
$instance = new $class;
16-
$tags = [str_slug(get_called_class())];
16+
$tags = $instance->makeCacheTags();
1717
$key = $instance->makeCacheKey();
1818

1919
return $instance->cache($tags)
@@ -24,6 +24,9 @@ public static function all($columns = ['*'])
2424

2525
public static function bootCachable()
2626
{
27+
static::deleted(function ($instance) {
28+
$instance->checkCooldownAndFlushAfterPersiting($instance);
29+
});
2730
static::saved(function ($instance) {
2831
$instance->checkCooldownAndFlushAfterPersiting($instance);
2932
});
@@ -41,6 +44,15 @@ public static function bootCachable()
4144
});
4245
}
4346

47+
public static function destroy($ids)
48+
{
49+
$class = get_called_class();
50+
$instance = new $class;
51+
$instance->flushCache();
52+
53+
return parent::destroy($ids);
54+
}
55+
4456
public function newEloquentBuilder($query)
4557
{
4658
if (! $this->isCachable()) {

tests/Integration/CachedBuilderMultipleQueryTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,28 @@ public function testCallingGetThenFirstQueriesReturnsDifferingResults()
3838
$this->assertInstanceOf(Author::class, $firstAuthor);
3939
$this->assertInstanceOf(Collection::class, $allAuthors);
4040
}
41+
42+
public function testUsingDestroyInvalidatesCache()
43+
{
44+
$allAuthors = (new Author)->get();
45+
$firstAuthor = $allAuthors->first();
46+
(new Author)->destroy($firstAuthor->id);
47+
$updatedAuthors = (new Author)->get()->keyBy("id");
48+
49+
$this->assertNotEquals($allAuthors, $updatedAuthors);
50+
$this->assertTrue($allAuthors->contains($firstAuthor));
51+
$this->assertFalse($updatedAuthors->contains($firstAuthor));
52+
}
53+
54+
public function testAllMethodCacheGetsInvalidated()
55+
{
56+
$allAuthors = (new Author)->all();
57+
$firstAuthor = $allAuthors->first();
58+
$firstAuthor->delete();
59+
$updatedAuthors = (new Author)->all();
60+
61+
$this->assertNotEquals($allAuthors, $updatedAuthors);
62+
$this->assertTrue($allAuthors->contains($firstAuthor));
63+
$this->assertFalse($updatedAuthors->contains($firstAuthor));
64+
}
4165
}

0 commit comments

Comments
 (0)