+ make getCacheTag a more smarts - accepts identifiers and returned cache tags with base class name and an addition - more tags with base class name and accepted identifiers
* getCacheTag return array * clear cache accepted unnecessary model argument - if it presented - then clear cache only by cache tag with passed model id + some helper methods, like a formatCacheTag, forceArray for string or collection conversion into array
This commit is contained in:
@ -1,26 +1,80 @@
|
|||||||
<?php namespace Wpstudio\Helpers\Classes\Observer;
|
<?php namespace Wpstudio\Helpers\Classes\Observer;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Cache;
|
||||||
|
|
||||||
abstract class BaseObserver implements Observer
|
abstract class BaseObserver implements Observer
|
||||||
{
|
{
|
||||||
protected static string $class;
|
protected static string $class;
|
||||||
|
|
||||||
/**
|
public static function clearCache(?Model $model = null): void
|
||||||
* Очистка кэша
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public static function clearCache(): void
|
|
||||||
{
|
{
|
||||||
$tags = [self::getCacheTag()];
|
Cache::tags(
|
||||||
|
static::getCacheTag(
|
||||||
\Cache::tags($tags)->flush();
|
!is_null($model) ? $model->getKey() : null,
|
||||||
|
[],
|
||||||
|
true
|
||||||
|
)
|
||||||
|
)->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Получение тэка кеша
|
* @param int|Collection|array|null $identifiers
|
||||||
* @return string
|
* @param array|null $additionalPieces
|
||||||
|
* @param bool $excludeBaseClassNameTagWithoutIdentifiers
|
||||||
|
* @desc $excludeBaseClassNameTagWithoutIdentifiers - выключает из результирующего массива тег кеша только с базовым классом, если переданы идентификаторы
|
||||||
|
* @desc То есть в итоговой выборке - не будет тега, без идентификатора, если вообще идентификаторы переданы
|
||||||
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function getCacheTag(): string
|
public static function getCacheTag($identifiers = null, ?array $additionalPieces = [], bool $excludeBaseClassNameTagWithoutIdentifiers = false): array
|
||||||
{
|
{
|
||||||
return static::getClass();
|
return static::getCacheTagsWithMaybeIdentifiers(static::getClass(), $identifiers, $additionalPieces, $excludeBaseClassNameTagWithoutIdentifiers);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function formatCacheTag(array $pieces): string
|
||||||
|
{
|
||||||
|
return implode('.', $pieces);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int|Collection|array|null $value
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private static function forceArray($value = null): array
|
||||||
|
{
|
||||||
|
return $value instanceof Collection ? $value->toArray() : (array)$value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $baseClassName
|
||||||
|
* @param int|Collection|array|null $iterateIdentifiers
|
||||||
|
* @param array|null $additionalTagPieces
|
||||||
|
* @param bool $excludeBaseClassNameTagWithoutIdentifiers
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private static function getCacheTagsWithMaybeIdentifiers(string $baseClassName, $iterateIdentifiers = [], ?array $additionalTagPieces = [], bool $excludeBaseClassNameTagWithoutIdentifiers = false): array
|
||||||
|
{
|
||||||
|
$iterateIdentifiers = static::forceArray($iterateIdentifiers);
|
||||||
|
|
||||||
|
$cacheTags = [];
|
||||||
|
|
||||||
|
if (!$excludeBaseClassNameTagWithoutIdentifiers || !$iterateIdentifiers) {
|
||||||
|
$cacheTags = (array)static::formatCacheTag(array_merge([$baseClassName], $additionalTagPieces));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($iterateIdentifiers) {
|
||||||
|
$cacheTags = array_merge(
|
||||||
|
$cacheTags,
|
||||||
|
array_map(
|
||||||
|
fn ($identifier) => static::formatCacheTag(
|
||||||
|
array_merge([$baseClassName, $identifier], $additionalTagPieces)
|
||||||
|
),
|
||||||
|
$iterateIdentifiers
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $cacheTags;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user