Skip to content

Commit

Permalink
create a list which keeps all models that are using laracache trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafaznv committed May 24, 2023
1 parent cc7708a commit 9f7aa5a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ public function get(string $name, bool $withCacheData = false): mixed

public function update(string $name): CacheData
{
$this->updateLaraCacheModelsList();

return $this->updateCacheEntity($name);
}

public function updateAll(): void
{
$this->updateLaraCacheModelsList();

foreach ($this->model::cacheEntities() as $entity) {
$this->updateCacheEntity(
name: $entity->name,
Expand All @@ -52,6 +56,8 @@ public function deleteAll(bool $forever = false): void
public function refresh(CacheEvent $event): void
{
if ($this->model::$isEnabled) {
$this->updateLaraCacheModelsList();

foreach ($this->model::cacheEntities() as $entity) {
if ($entity->isQueueable) {
$this->initCache($entity, $entity->getTtl());
Expand Down
38 changes: 38 additions & 0 deletions src/Jobs/UpdateLaraCacheModelsList.php
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)
);
}
}
6 changes: 6 additions & 0 deletions src/Traits/InteractsWithCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Mostafaznv\LaraCache\DTOs\CacheEvent;
use Mostafaznv\LaraCache\DTOs\CacheStatus;
use Mostafaznv\LaraCache\Exceptions\CacheEntityDoesNotExist;
use Mostafaznv\LaraCache\Jobs\UpdateLaraCacheModelsList;

trait InteractsWithCache
{
Expand Down Expand Up @@ -123,6 +124,11 @@ private function storeCache(CacheData $cache, CacheEntity $entity, int $ttl): vo
$this->updateCacheEntitiesList($entity);
}

private function updateLaraCacheModelsList(): void
{
UpdateLaraCacheModelsList::dispatch($this->model);
}

private function updateCacheEntity(string $name, ?CacheEvent $event = null, CacheEntity $entity = null): CacheData
{
$entity = $this->findCacheEntity($name, $entity);
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/QueueCacheTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<?php

use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Bus;
use Mostafaznv\LaraCache\DTOs\CacheStatus;
use Illuminate\Support\Facades\Queue;
use Mostafaznv\LaraCache\Jobs\RefreshCache;
use Mostafaznv\LaraCache\Jobs\UpdateLaraCacheModelsList;
use Mostafaznv\LaraCache\Tests\TestSupport\TestModels\QueueTestModel;
use Illuminate\Support\Str;

beforeEach(function() {
Bus::fake([
UpdateLaraCacheModelsList::class
]);

if (!Str::contains($this->getName(), '[without-model]')) {
createQueueModel();
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Feature/UpdateLaraCacheModelsListTest.php
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);
});

0 comments on commit 9f7aa5a

Please sign in to comment.