Skip to content

Commit

Permalink
Configurable onQueue and onConnection #18
Browse files Browse the repository at this point in the history
  • Loading branch information
mostafaznv committed Jun 21, 2023
1 parent 0838598 commit 1a0ba93
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 61 deletions.
47 changes: 25 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,20 @@ Therefore, if you decide to use my packages, please kindly consider making a don

## CacheEntity Methods

| method | Arguments | description |
|----------------------|-----------------------------------------|-------------------------------------------------------------------------------|
| setDriver | driver (type: `string`) | Specifies custom driver for cache entity |
| isQueueable | status (type: `bool`, default: 'true') | Specifies if cache operation should perform in the background or not |
| refreshAfterCreate | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after create a record |
| refreshAfterUpdate | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after update a record |
| refreshAfterDelete | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after delete a record |
| refreshAfterRestore | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after restore a record |
| forever | | Specifies that the cache should be valid forever |
| validForRestOfDay | | Specify that cache entity should be valid till end of day |
| validForRestOfWeek | | Specify that cache entity should be valid till end of week |
| ttl | seconds (type: `int`) | Specifies cache time to live in second |
| setDefault | defaultValue (type: `mixed`) | Specifies default value for the case that cache entity doesn't have any value |
| cache | Closure | **Main** part of each cache entity. defines cache content |
| method | Arguments | description |
|----------------------|-------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| setDriver | driver (type: `string`) | Specifies custom driver for cache entity |
| isQueueable | status (type: `bool`, default: 'true')<br>onConnection (type: `string`, default: '')<br>onQueue (type: `string`, default: '') | This option specifies whether the cache operation should be performed in the background or not.<br>**Note**: By using the `onConnection` and `onQueue` arguments, you have the ability to specify custom connection and queue names for each cache entity. |
| refreshAfterCreate | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after create a record |
| refreshAfterUpdate | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after update a record |
| refreshAfterDelete | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after delete a record |
| refreshAfterRestore | status (type: `bool`, default: `true`) | Specifies if the cache should refresh after restore a record |
| forever | | Specifies that the cache should be valid forever |
| validForRestOfDay | | Specify that cache entity should be valid till end of day |
| validForRestOfWeek | | Specify that cache entity should be valid till end of week |
| ttl | seconds (type: `int`) | Specifies cache time to live in second |
| setDefault | defaultValue (type: `mixed`) | Specifies default value for the case that cache entity doesn't have any value |
| cache | Closure | **Main** part of each cache entity. defines cache content |
## Disable/Enable Cache
Expand Down Expand Up @@ -391,14 +391,16 @@ To unlock the cache management capabilities provided by Nova LaraCache, please r
## Config Properties
| method | Type | description |
|-------------------|------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| driver | string (default: `null`) | The default mechanism for handling cache storage.<br>If you keep this option `null`, LaraCache will use the default cache storage from `config/cache.php` |
| laracache-list | string (default: `laracache.list`) | LaraCache uses a separate list to store name of all entities. using these keys, we can perform some actions to all entities (such as update or delete them) |
| first-day-of-week | integer (default: `0`) | In some regions, saturday is first day of the week and in another regions it may be different. you can change the first day of a week by changing this property |
| last-day-of-week | integer (default: `6`) | In some regions, friday is last day of the week and in another regions it may be different. you can change the last day of a week by changing this property |
| queue | bool (default: `false`) | Sometimes caching process is very heavy, so you have to queue the process and do it in background. |
| groups | array (default: `[]`) | You can group some entities and perform some operations on them |
| method | Type | description |
|-------------------|------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| driver | string (default: `null`) | The default mechanism for handling cache storage.<br>If you keep this option `null`, LaraCache will use the default cache storage from `config/cache.php` |
| laracache-list | string (default: `laracache.list`) | LaraCache uses a separate list to store name of all entities. using these keys, we can perform some actions to all entities (such as update or delete them) |
| first-day-of-week | integer (default: `0`) | In some regions, saturday is first day of the week and in another regions it may be different. you can change the first day of a week by changing this property |
| last-day-of-week | integer (default: `6`) | In some regions, friday is last day of the week and in another regions it may be different. you can change the last day of a week by changing this property |
| queue.status | bool (default: `false`) | Sometimes caching process is very heavy, so you have to queue the process and do it in background. |
| queue.name | string (default: `default`) | You have the option to set custom name for the queue process. This name will be used when invoking the `onQueue` method while dispatching the queue job. |
| queue.connection | string (default: `null`) | You have the option to set custom connection for the queue process. This connection will be used when invoking the `onConnection` method while dispatching the queue job. |
| groups | array (default: `[]`) | You can group some entities and perform some operations on them |
## Complete Example
Expand Down Expand Up @@ -437,6 +439,7 @@ class Article extends Model
}),
CacheEntity::make('list.week')
->isQueueable(true, 'redis', 'low')
->validForRestOfWeek()
->cache(function() {
return Article::query()->latest()->get();
Expand Down
19 changes: 5 additions & 14 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@

'laracache-list' => 'laracache.list',


/*
|--------------------------------------------------------------------------
| Cache Name
|--------------------------------------------------------------------------
|
| The default queue for heavy jobs
|
*/

'queue-name' => 'default',


/*
|--------------------------------------------------------------------------
| First Day of Week
Expand Down Expand Up @@ -73,7 +60,11 @@
|
*/

'queue' => false,
'queue' => [
'status' => false,
'name' => 'default',
'connection' => null
],

/*
|--------------------------------------------------------------------------
Expand Down
5 changes: 4 additions & 1 deletion src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public function refresh(CacheEvent $event): void
foreach ($this->model::cacheEntities() as $entity) {
if ($entity->isQueueable) {
$this->initCache($entity, $entity->getTtl());
RefreshCache::dispatch($this->model, $entity->name, $event);

RefreshCache::dispatch($this->model, $entity->name, $event)
->onConnection($entity->queueConnection)
->onQueue($entity->queueName);
}
else {
$this->updateCacheEntity($entity->name, $event, $entity);
Expand Down
40 changes: 38 additions & 2 deletions src/CacheEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class CacheEntity
*/
public bool $isQueueable;

/**
* Queue name
*
* @var string
*/
public string $queueName;

/**
* Queue connection
*
* @var string
*/
public string $queueConnection;

/**
* Indicate if cache should exist till end of day
*
Expand Down Expand Up @@ -103,7 +117,19 @@ public function __construct(string $name)
{
$this->name = $name;
$this->driver = config('laracache.driver') ?? config('cache.default');
$this->isQueueable = config('laracache.queue') ?? false;

$queue = config('laracache.queue');

if (is_array($queue)) {
$this->isQueueable = $queue['status'] ?? false;
$this->queueName = $queue['name'] ?? 'default';
$this->queueConnection = $queue['connection'] ?? config('queue.default');
}
else {
$this->isQueueable = (bool)$queue;
$this->queueName = 'default';
$this->queueConnection = config('queue.default');
}
}

/**
Expand Down Expand Up @@ -134,12 +160,22 @@ public function setDriver(string $driver): CacheEntity
* Specify if cache operation should perform in background or not
*
* @param bool $status
* @param string $onConnection
* @param string $onQueue
* @return $this
*/
public function isQueueable(bool $status = true): CacheEntity
public function isQueueable(bool $status = true, string $onConnection = '', string $onQueue = ''): CacheEntity
{
$this->isQueueable = $status;

if ($onConnection) {
$this->queueConnection = $onConnection;
}

if ($onQueue) {
$this->queueName = $onQueue;
}

return $this;
}

Expand Down
4 changes: 1 addition & 3 deletions src/Jobs/RefreshCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public function __construct(
private string $model,
private string $name,
private CacheEvent $event
) {
$this->queue = config('laracache.queue-name');
}
) {}

public function handle(): void
{
Expand Down
4 changes: 3 additions & 1 deletion src/Jobs/UpdateLaraCacheModelsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public function __construct(private string $model)
{
$this->driver = config('laracache.driver') ?: config('cache.default');
$this->key = self::LARACACHE_MODELS_LIST;
$this->queue = config('laracache.queue-name');

$this->connection = config('laracache.queue.connection', 'default');
$this->queue = config('laracache.queue.name', 'default');
}


Expand Down
5 changes: 4 additions & 1 deletion src/Traits/InteractsWithCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ private function retrieve(string $name): CacheData
if ($cache->status->equals(CacheStatus::NOT_CREATED())) {
if ($entity->isQueueable) {
$this->initCache($entity, $entity->getTtl());
RefreshCache::dispatch($this->model, $entity->name, CacheEvent::RETRIEVED());

RefreshCache::dispatch($this->model, $entity->name, CacheEvent::RETRIEVED())
->onConnection($entity->queueConnection)
->onQueue($entity->queueName);

return CacheData::fromCache($entity, $this->prefix, $entity->ttl);
}
Expand Down
50 changes: 49 additions & 1 deletion tests/Feature/CacheEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
expect($entity->driver)->toBe('fake-driver');
});

it('will set is queueable by default', function() {
it('will set is-queueable by default if laracache.queue is boolean', function() {
expect($this->entity->isQueueable)->toBeFalse();

config()->set('laracache.queue', true);
Expand All @@ -38,6 +38,16 @@
expect($entity->isQueueable)->toBeTrue();
});

it('will set is-queueable by default', function() {
expect($this->entity->isQueueable)->toBeFalse();

config()->set('laracache.queue.status', true);

$entity = CacheEntity::make('test-name');

expect($entity->isQueueable)->toBeTrue();
});

it('will set custom queue status', function() {
expect($this->entity->isQueueable)->toBeFalse();

Expand All @@ -48,6 +58,44 @@
expect($this->entity->isQueueable)->toBeFalse();
});

it('will set queue name and connection by default', function() {
expect($this->entity->queueName)->toBe('default')
->and($this->entity->queueConnection)->toBe('database');
});

it('will set queue name and connection by default with deprecated config file', function() {
config()->set('laracache.queue', true);

$entity = CacheEntity::make('test-name');

expect($entity->queueName)->toBe('default')
->and($entity->queueConnection)->toBe('database');
});

it('will set custom queue name and connection using config file', function() {
expect($this->entity->queueName)->toBe('default')
->and($this->entity->queueConnection)->toBe('database');

config()->set('laracache.queue.name', 'test-queue');
config()->set('laracache.queue.connection', 'test-connection');

$entity = CacheEntity::make('test-name');

expect($entity->queueName)->toBe('test-queue')
->and($entity->queueConnection)->toBe('test-connection');
});

it('will set custom queue name and connection using entity method', function() {
expect($this->entity->queueName)->toBe('default')
->and($this->entity->queueConnection)->toBe('database');

$entity = CacheEntity::make('test-name')
->isQueueable(true, 'test-connection', 'test-queue');

expect($entity->queueName)->toBe('test-queue')
->and($entity->queueConnection)->toBe('test-connection');
});

it('will set specific driver for entity', function() {
expect($this->entity->driver)->toBe('array');

Expand Down
Loading

0 comments on commit 1a0ba93

Please sign in to comment.