Skip to content

Commit

Permalink
Merge pull request #17 from mostafaznv/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mostafaznv authored Jun 13, 2023
2 parents 540dedd + 6eb3009 commit 73581d4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ php artisan laracache:update -m Article -e latest -e featured
php artisan laracache:update -m Article -m Product
# defines model with full namespace
php artisan laracache:update -m Domain\Article\Models\Article
php artisan laracache:update -m "Domain\Article\Models\Article"
```
### Delete Cache
Expand All @@ -330,7 +330,7 @@ php artisan laracache:delete -m Article -e latest -e featured
php artisan laracache:delete -m Article -m Product
# defines model with full namespace
php artisan laracache:delete -m Domain\Article\Models\Article
php artisan laracache:delete -m "Domain\Article\Models\Article"
```
> **Note**: If you don't specify any entity, all entities will be operated.
Expand Down
1 change: 1 addition & 0 deletions src/DTOs/CacheEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Mostafaznv\LaraCache\Utils\CacheEnum;

/**
* @method static self RETRIEVED()
* @method static self CREATED()
* @method static self UPDATED()
* @method static self DELETED()
Expand Down
8 changes: 8 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\RefreshCache;
use Mostafaznv\LaraCache\Jobs\UpdateLaraCacheModelsList;

trait InteractsWithCache
Expand Down Expand Up @@ -162,6 +163,13 @@ private function retrieve(string $name): CacheData
$cache = CacheData::fromCache($entity, $this->prefix);

if ($cache->status->equals(CacheStatus::NOT_CREATED())) {
if ($entity->isQueueable) {
$this->initCache($entity, $entity->getTtl());
RefreshCache::dispatch($this->model, $entity->name, CacheEvent::RETRIEVED());

return CacheData::fromCache($entity, $this->prefix, $entity->ttl);
}

return $this->updateCacheEntity($name, null, $entity);
}

Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/QueueCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Mostafaznv\LaraCache\Jobs\UpdateLaraCacheModelsList;
use Mostafaznv\LaraCache\Tests\TestSupport\TestModels\QueueTestModel;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

beforeEach(function() {
Bus::fake([
Expand Down Expand Up @@ -61,6 +62,53 @@
->and($isCreated)->toBeTrue();
});

it('will return default value and dispatch cache creation job on retrieving entity [without-model]', function() {
Queue::fake();
DB::table('test_models')
->insert([
'name' => 'queue-test-name',
'content' => 'content',
'created_at' => now()
]);

$cache = QueueTestModel::cache()->get('latest', true);
$isCreating = $cache->status->equals(CacheStatus::CREATING());

expect($isCreating)->toBeTrue()
->and($cache->value)->toBe(-1);

expect(Queue::assertPushed(RefreshCache::class));
});

it('will create cache in background on retrieving entity [without-model]', function() {
$name = 'queue-test-name';
$before = now();

DB::table('test_models')
->insert([
'name' => $name,
'content' => 'content',
'created_at' => now()
]);

$cache = QueueTestModel::cache()->get('latest', true);
$isCreating = $cache->status->equals(CacheStatus::CREATING());

expect($isCreating)->toBeTrue()
->and($cache->value)->toBe(-1);

Artisan::call('queue:work --once');

$cache = QueueTestModel::cache()->get('latest', true);
$isCreated = $cache->status->equals(CacheStatus::CREATED());
$after = now();

expect($before->diffInSeconds($after) >= 1)->toBeTrue()
->and($cache->value)->toBeInstanceOf(QueueTestModel::class)
->and($cache->value->name)->toBe($name)
->and($isCreated)->toBeTrue();
});

it('will change cache status to creating on model update [without-model]', function() {
$model = createQueueModel();

Expand Down

0 comments on commit 73581d4

Please sign in to comment.