-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create a list which keeps all models that are using laracache trait
- Loading branch information
1 parent
cc7708a
commit 9f7aa5a
Showing
5 changed files
with
89 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,38 @@ | ||
<?php | ||
|
||
namespace Mostafaznv\LaraCache\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class UpdateLaraCacheModelsList implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
private string $driver; | ||
private string $key; | ||
|
||
public const LARACACHE_MODELS_LIST = 'laracache.models'; | ||
|
||
public function __construct(private string $model) | ||
{ | ||
$this->driver = config('laracache.driver') ?: config('cache.default'); | ||
$this->key = self::LARACACHE_MODELS_LIST; | ||
} | ||
|
||
|
||
public function handle(): void | ||
{ | ||
$list = Cache::driver($this->driver)->get($this->key, []); | ||
$list[] = $this->model; | ||
|
||
Cache::driver($this->driver)->forever( | ||
key: 'laracache.models', | ||
value: array_unique($list) | ||
); | ||
} | ||
} |
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
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,33 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Cache; | ||
use Mostafaznv\LaraCache\Tests\TestSupport\TestModels\TestModel; | ||
use Mostafaznv\LaraCache\Tests\TestSupport\TestModels\TestModel2; | ||
|
||
beforeEach(function() { | ||
config()->set('queue.default', 'sync'); | ||
|
||
createModel(); | ||
createModel2(); | ||
}); | ||
|
||
|
||
it('will update the list of models which use laracache', function() { | ||
$models = Cache::get('laracache.models'); | ||
|
||
expect($models) | ||
->toBeArray() | ||
->toHaveCount(2) | ||
->toMatchArray([ | ||
TestModel::class, | ||
TestModel2::class | ||
]); | ||
}); | ||
|
||
it('wont duplicate models', function() { | ||
createModel(); | ||
$models = Cache::get('laracache.models'); | ||
|
||
expect($models)->toBeArray()->toHaveCount(2); | ||
}); | ||
|