-
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from renoki-co/feature/invalidate-cache-on-mod…
…el-events [feature] Automatic cache flushing
- Loading branch information
Showing
8 changed files
with
366 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Model Factories | ||
|-------------------------------------------------------------------------- | ||
| | ||
| This directory should contain each of the model factory definitions for | ||
| your application. Factories provide a convenient way to generate new | ||
| model instances for testing / seeding your application's database. | ||
| | ||
*/ | ||
|
||
use Illuminate\Support\Str; | ||
|
||
$factory->define(\Rennokki\QueryCache\Test\Models\Page::class, function () { | ||
return [ | ||
'name' => 'Page'.Str::random(5), | ||
]; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Rennokki\QueryCache; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class FlushQueryCacheObserver | ||
{ | ||
/** | ||
* Handle the Model "created" event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
public function created(Model $model) | ||
{ | ||
$this->invalidateCache($model); | ||
} | ||
|
||
/** | ||
* Handle the Model "updated" event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
public function updated(Model $model) | ||
{ | ||
$this->invalidateCache($model); | ||
} | ||
|
||
/** | ||
* Handle the Model "deleted" event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
public function deleted(Model $model) | ||
{ | ||
$this->invalidateCache($model); | ||
} | ||
|
||
/** | ||
* Handle the Model "forceDeleted" event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
public function forceDeleted(Model $model) | ||
{ | ||
$this->invalidateCache($model); | ||
} | ||
|
||
/** | ||
* Handle the Model "restored" event. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
*/ | ||
public function restored(Model $model) | ||
{ | ||
$this->invalidateCache($model); | ||
} | ||
|
||
/** | ||
* Invalidate the cache for a model. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return void | ||
* @throws Exception | ||
*/ | ||
protected function invalidateCache(Model $model): void | ||
{ | ||
if (! $model->getCacheTagsToInvalidateOnUpdate()) { | ||
throw new Exception('Automatic invalidation for '.$class.' works only if at least one tag to be invalidated is specified.'); | ||
} | ||
|
||
$class = get_class($model); | ||
|
||
$class::flushQueryCache( | ||
$model->getCacheTagsToInvalidateOnUpdate() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Rennokki\QueryCache\Test; | ||
|
||
use Cache; | ||
use Rennokki\QueryCache\Test\Models\Page; | ||
|
||
class FlushCacheOnUpdateTest extends TestCase | ||
{ | ||
public function test_flush_cache_on_create() | ||
{ | ||
$page = factory(Page::class)->create(); | ||
$storedPage = Page::cacheFor(now()->addHours(1))->first(); | ||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNotNull($cache); | ||
|
||
$this->assertEquals( | ||
$cache->first()->id, | ||
$storedPage->id | ||
); | ||
|
||
Page::create([ | ||
'name' => '9GAG', | ||
]); | ||
|
||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNull($cache); | ||
} | ||
|
||
public function test_flush_cache_on_update() | ||
{ | ||
$page = factory(Page::class)->create(); | ||
$storedPage = Page::cacheFor(now()->addHours(1))->first(); | ||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNotNull($cache); | ||
|
||
$this->assertEquals( | ||
$cache->first()->id, | ||
$storedPage->id | ||
); | ||
|
||
$page->update([ | ||
'name' => '9GAG', | ||
]); | ||
|
||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNull($cache); | ||
} | ||
|
||
public function test_flush_cache_on_delete() | ||
{ | ||
$page = factory(Page::class)->create(); | ||
$storedPage = Page::cacheFor(now()->addHours(1))->first(); | ||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNotNull($cache); | ||
|
||
$this->assertEquals( | ||
$cache->first()->id, | ||
$storedPage->id | ||
); | ||
|
||
$page->delete(); | ||
|
||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNull($cache); | ||
} | ||
|
||
public function test_flush_cache_on_force_deletion() | ||
{ | ||
$page = factory(Page::class)->create(); | ||
$storedPage = Page::cacheFor(now()->addHours(1))->first(); | ||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNotNull($cache); | ||
|
||
$this->assertEquals( | ||
$cache->first()->id, | ||
$storedPage->id | ||
); | ||
|
||
$page->forceDelete(); | ||
|
||
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "pages" limit 1a:0:{}'); | ||
|
||
$this->assertNull($cache); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Rennokki\QueryCache\Test\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Rennokki\QueryCache\Traits\QueryCacheable; | ||
|
||
class Page extends Model | ||
{ | ||
use QueryCacheable; | ||
|
||
protected static $flushCacheOnUpdate = true; | ||
|
||
protected $cacheUsePlainKey = true; | ||
|
||
protected $fillable = [ | ||
'name', | ||
]; | ||
|
||
protected function getCacheBaseTags(): array | ||
{ | ||
return [ | ||
'test', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class Pages extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('pages', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('pages'); | ||
} | ||
} |