Skip to content

Commit

Permalink
Merge pull request #12 from mostafaznv/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mostafaznv authored May 24, 2023
2 parents 652acda + 9f7aa5a commit 8b321dd
Show file tree
Hide file tree
Showing 39 changed files with 1,792 additions and 50 deletions.
111 changes: 102 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ Manually updating the cache entities of models after dispatching model events (c


----
I develop in an open-source journey 🚀, I wish I lived in an environment where financial situation was fine and I could only focus on the path, but as you may know, life isn't perfect. <br>So if you end up using my packages, please consider making a donation, any amount would go along way and is much appreciated. 🍺
I am on an open-source journey 🚀, and I wish I could solely focus on my development path without worrying about my financial situation. However, as life is not perfect, I have to consider other factors.

Therefore, if you decide to use my packages, please kindly consider making a donation. Any amount, no matter how small, goes a long way and is greatly appreciated. 🍺

[![Donate](https://mostafaznv.github.io/donate/donate.svg)](https://mostafaznv.github.io/donate)

Expand Down Expand Up @@ -116,6 +118,10 @@ I develop in an open-source journey 🚀, I wish I lived in an environment where
- [Delete all Model Entities](#delete-all-model-entities)
- [Delete all Model Entities Forever](#delete-all-model-entities-forever)
- [Delete all LaraCache Entities](#delete-all-laracache-entities)
- [Artisan Commands](#artisan-commands)
- [Update Cache](#update-cache)
- [Delete Cache](#delete-cache)
- [Group Operations](#group-operations)
- [Config Properties](#config-properties)
- [Complete Example](#complete-example)

Expand Down Expand Up @@ -289,15 +295,100 @@ LaraCache::deleteAll(forever: true);
## Artisan Commands
This feature allows you to update or delete multiple cache entities of one or more models from the console command. This means you can programmatically control the cache data outside the caching cycle.
You can also create groups of models and their entities in the config file and easily update or delete all their entities at once.
### Update Cache
```shell
# updates all entities of article model
php artisan laracache:update -m Article
# updates specified entities of article model
php artisan laracache:update -m Article -e latest -e featured
# updates all entities of article and product models
php artisan laracache:update -m Article -m Product
# defines model with full namespace
php artisan laracache:update -m Domain\Article\Models\Article
```
### Delete Cache
```shell
# deletes all entities of article model
php artisan laracache:delete -m Article
# deletes specified entities of article model
php artisan laracache:delete -m Article -e latest -e featured
# deletes all entities of article and product models
php artisan laracache:delete -m Article -m Product
# defines model with full namespace
php artisan laracache:delete -m Domain\Article\Models\Article
```
> **Note**: If you don't specify any entity, all entities will be operated.

> **Note**: If you specify multiple models, you can't specify any entity and all entities of all models will be operated.
### Group Operations
```shell
# updates all entities of models that are in group-1
php artisan laracache:update-group group-1
# deletes all entities of models that are in group-1
php artisan laracache:delete-group group-1
```
This is an example of a group configuration:
```php
# config/laracache.php
return [
// ...
'groups' => [
'group-1' => [
[
'model' => \App\Models\User::class,
'entities' => [
'users.latest', 'users.featured'
],
],
[
'model' => \App\Models\Article::class,
'entities' => [],
]
],
'group-2' => [
[
'model' => \App\Models\Article::class,
'entities' => [
'featured-list', 'latest'
],
],
[
'model' => \App\Models\User::class,
'entities' => ['users.latest'],
]
],
]
];
```
## 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. |
| 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 |
## Complete Example
Expand Down Expand Up @@ -386,7 +477,9 @@ class Article extends Model
```
----
I develop in an open-source journey 🚀, I wish I lived in an environment where financial situation was fine and I could only focus on the path, but as you may know, life isn't perfect. <br>So if you end up using my packages, please consider making a donation, any amount would go along way and is much appreciated. 🍺
I am on an open-source journey 🚀, and I wish I could solely focus on my development path without worrying about my financial situation. However, as life is not perfect, I have to consider other factors.
Therefore, if you decide to use my packages, please kindly consider making a donation. Any amount, no matter how small, goes a long way and is greatly appreciated. 🍺
[![Donate](https://mostafaznv.github.io/donate/donate.svg)](https://mostafaznv.github.io/donate)
Expand Down
40 changes: 40 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,44 @@
*/

'queue' => false,

/*
|--------------------------------------------------------------------------
| Groups
|--------------------------------------------------------------------------
|
| You can group some entities and perform some actions on them.
|
| Example:
| 'groups' => [
| 'group-1' => [
| [
| 'model' => \App\Models\User::class,
| 'entities' => [
| 'featured', 'latest', 'popular'
| ],
| ],
| [
| 'model' => \App\Models\Article::class,
| 'entities' => [],
| ]
| ],
|
| 'group-2' => [
| [
| 'model' => \App\Models\Product::class,
| 'entities' => [
| 'latest', 'popular'
| ],
| ],
| [
| 'model' => \App\Models\Article::class,
| 'entities' => [],
| ]
| ],
| ],
|
*/

'groups' => [],
];
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

<php>
<env name="APP_NAME" value="laracache-app"/>
<server name="QUEUE_CONNECTION" value="database"/>
</php>
</phpunit>
60 changes: 60 additions & 0 deletions src/Actions/DeleteCacheAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Mostafaznv\LaraCache\Actions;

use Mostafaznv\LaraCache\Actions\Support\UpdateDeleteCache;
use Mostafaznv\LaraCache\DTOs\CommandData;

class DeleteCacheAction extends UpdateDeleteCache
{
public function run(CommandData $data): void
{
if (count($data->models) > 1) {
foreach ($data->models as $model) {
$this->deleteAll($model);
}
}
else {
$model = $data->models[0];

empty($data->entities)
? $this->deleteAll($model)
: $this->delete($model, $data->entities);
}
}

/**
* @param \Mostafaznv\LaraCache\Traits\LaraCache $model
* @return void
*/
private function deleteAll(string $model): void
{
$entities = [];

foreach ($model::cacheEntities() as $entity) {
$entities[] = $entity->name;
}

$this->delete($model, $entities);
}

/**
* @param \Mostafaznv\LaraCache\Traits\LaraCache $model
* @param array $entities
* @return void
*/
private function delete(string $model, array $entities): void
{
$this->console?->warn(
sprintf('>> Deleting cache entities in [%s] model', class_basename($model))
);

foreach ($entities as $entity) {
$this->console?->line('' . $this->title($entity));

$model::cache()->delete($entity);

$this->console?->info('Deleted');
}
}
}
17 changes: 17 additions & 0 deletions src/Actions/DeleteGroupCacheAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Mostafaznv\LaraCache\Actions;

use Mostafaznv\LaraCache\Actions\Support\UpdateDeleteCache;

class DeleteGroupCacheAction extends UpdateDeleteCache
{
public function run(string $group): void
{
$group = $this->group($group);

foreach ($group as $item) {
DeleteCacheAction::make($this->console)->run($item);
}
}
}
70 changes: 70 additions & 0 deletions src/Actions/Support/UpdateDeleteCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Mostafaznv\LaraCache\Actions\Support;

use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Mostafaznv\LaraCache\DTOs\CommandData;
use Mostafaznv\LaraCache\Exceptions\CacheGroupNotExist;
use Mostafaznv\LaraCache\Exceptions\CacheGroupValueIsNotValid;

abstract class UpdateDeleteCache
{
public function __construct(protected ?Command $console) {}

public static function make(?Command $console = null): self
{
return new static($console);
}


protected function title(string $string): string
{
return Str::title(
Str::slug(
title: Str::replace(['.', '-', '_'], ' ', $string),
separator: ' '
)
);
}

/**
* @param string $group
* @return CommandData[]
*/
protected function group(string $group): array
{
$groupName = $group;
$group = config("laracache.groups.$group");

if (is_null($group)) {
throw CacheGroupNotExist::make($groupName);
}

if (is_array($group)) {
$data = [];

foreach ($group as $item) {
$data[] = $this->makeCommandDataFromGroupItem($item, $groupName);
}

return $data;
}
else {
throw CacheGroupValueIsNotValid::make($groupName);
}
}

private function makeCommandDataFromGroupItem(mixed $item, string $groupName): CommandData
{
if (isset($item['model']) and is_string($item['model']) and isset($item['entities'])) {
return CommandData::make(
models: [$item['model']],
entities: $item['entities']
);
}
else {
throw CacheGroupValueIsNotValid::make($groupName);
}
}
}
Loading

0 comments on commit 8b321dd

Please sign in to comment.