Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Mar 10, 2018
2 parents 8feace0 + 2691181 commit 6b65b0d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.2.50] - 10 Mar 2018
### Added
- cache invalidation when `destroy()`ing models.

### Fixed
- cache tag generation when calling `all()` queries that prevented proper
cache invalidation.

## [0.2.49] - 9 Mar 2018
### Fixed
- caching of `->first()` queries.
Expand Down
14 changes: 13 additions & 1 deletion src/Traits/ModelCaching.php
Expand Up @@ -13,7 +13,7 @@ public static function all($columns = ['*'])

$class = get_called_class();
$instance = new $class;
$tags = [str_slug(get_called_class())];
$tags = $instance->makeCacheTags();
$key = $instance->makeCacheKey();

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

public static function bootCachable()
{
static::deleted(function ($instance) {
$instance->checkCooldownAndFlushAfterPersiting($instance);
});
static::saved(function ($instance) {
$instance->checkCooldownAndFlushAfterPersiting($instance);
});
Expand All @@ -41,6 +44,15 @@ public static function bootCachable()
});
}

public static function destroy($ids)
{
$class = get_called_class();
$instance = new $class;
$instance->flushCache();

return parent::destroy($ids);
}

public function newEloquentBuilder($query)
{
if (! $this->isCachable()) {
Expand Down
24 changes: 24 additions & 0 deletions tests/Integration/CachedBuilderMultipleQueryTest.php
Expand Up @@ -38,4 +38,28 @@ public function testCallingGetThenFirstQueriesReturnsDifferingResults()
$this->assertInstanceOf(Author::class, $firstAuthor);
$this->assertInstanceOf(Collection::class, $allAuthors);
}

public function testUsingDestroyInvalidatesCache()
{
$allAuthors = (new Author)->get();
$firstAuthor = $allAuthors->first();
(new Author)->destroy($firstAuthor->id);
$updatedAuthors = (new Author)->get()->keyBy("id");

$this->assertNotEquals($allAuthors, $updatedAuthors);
$this->assertTrue($allAuthors->contains($firstAuthor));
$this->assertFalse($updatedAuthors->contains($firstAuthor));
}

public function testAllMethodCacheGetsInvalidated()
{
$allAuthors = (new Author)->all();
$firstAuthor = $allAuthors->first();
$firstAuthor->delete();
$updatedAuthors = (new Author)->all();

$this->assertNotEquals($allAuthors, $updatedAuthors);
$this->assertTrue($allAuthors->contains($firstAuthor));
$this->assertFalse($updatedAuthors->contains($firstAuthor));
}
}

0 comments on commit 6b65b0d

Please sign in to comment.