Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 9, 2024
1 parent 3d5bea6 commit a0270ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 29 deletions.
37 changes: 14 additions & 23 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
use ReflectionClass;
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdated;
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdating;
Expand Down Expand Up @@ -43,13 +44,6 @@ trait CachesValue
*/
protected $expression = null;

/**
* The parameters this cache should be stored with.
*
* @var array<string, mixed>
*/
protected $parameters = [];

/**
* Indicates whether this cache is currently updating or not.
*
Expand All @@ -75,12 +69,8 @@ final public function handle($event = null): void
? Blade::renderComponent($this)
: $this->run($event);

$expression = (new ReflectionClass(static::class))->getProperty('expression')->getDefaultValue();

Cache::driver($driver)->forever($cacheKey, (object) [
'value' => $value,
'size' => strlen($value),
'expression' => $expression,
'updated_at' => now(),
]);

Expand All @@ -105,23 +95,16 @@ public function getParameters()
*/
private static function store($parameters): array
{
$class = static::class;

$store = (new ReflectionClass($class))
->getProperty('store')
->getDefaultValue();

return self::parseCacheString($class, $store, $parameters);
return self::getCacheKey($parameters);
}

public function isCached($parameters = []): bool
{
[$driver, $cacheKey] = self::store($parameters ?? []);

$cache = Cache::driver($driver);
$cached = $cache->get($cacheKey);

return $cached && filled($cached?->value);
return $cache->has($cacheKey);
}

/**
Expand Down Expand Up @@ -255,17 +238,25 @@ private static function getUpdateMethodString(): string
/**
* @return array{string, string}
*/
private static function parseCacheString($class, ?string $store, ?array $parameters = []): array
public static function getCacheKey(?array $parameters = [], ?string $store = null, ?string $class = null): array
{
if ($store && strpos($store, ':')) {
$class ??= static::class;
$store ??= (new ReflectionClass($class))
->getProperty('store')
->getDefaultValue();

if (
! is_null($store) &&
strpos($store, ':')
) {
$cacheDriver = substr($store, 0, strpos($store, ':'));
$cacheKey = substr($store, strpos($store, ':') + 1);
} else {
$cacheKey = $store;
}

$cacheDriver ??= config('cache.default');
$cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(\Str::snake($class)));
$cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(Str::snake($class)));

if ($parameters) {
$cacheKey .= ':'.http_build_query($parameters);
Expand Down
16 changes: 12 additions & 4 deletions src/Commands/PermanentCachesStatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Vormkracht10\PermanentCache\Commands;

use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\Schedule;
use Lorisleiva\CronTranslator\CronTranslator;
use ReflectionClass;
use Spatie\Emoji\Emoji;
use Symfony\Component\Console\Helper\TableSeparator;
use Vormkracht10\PermanentCache\Facades\PermanentCache;
Expand Down Expand Up @@ -30,6 +33,11 @@ public function handle()
{
$caches = PermanentCache::configuredCaches();

$frequencies = collect(app(Schedule::class)->events())
->mapWithKeys(function ($schedule) {
return [$schedule->description => CronTranslator::translate($schedule->expression)];
});

foreach ($caches as $c) {
$cache = $caches->current();
$parameters = $caches->getInfo();
Expand All @@ -46,9 +54,9 @@ public function handle()
$row = [
$cache->isCached($parameters) ? Emoji::checkMarkButton() : Emoji::crossMark(),
$cache->getName(),
is_object($cached) ? readable_size($cached->size) : 'N/A',
is_object($cached) ? $cached->expression : 'N/A',
is_object($cached) ? $cached->updated_at->diffForHumans() : 'N/A',
$cached ? readable_size(strlen(serialize($cached))) : 'N/A',
$cached?->updated_at?->diffForHumans() ?: 'N/A',
$frequencies[$cache->getName()] ?? 'N/A',
];

if ($this->option('parameters')) {
Expand All @@ -62,7 +70,7 @@ public function handle()
array_pop($cachesTable);

$this->table(
[null, 'Cache', 'Size', 'Frequency', 'Last Updated'] + ($this->option('parameters') ? ['Parameters'] : []),
[null, 'Cache', 'Size', 'Last Updated', 'Frequency'] + ($this->option('parameters') ? ['Parameters'] : []),
$cachesTable,
'box',
);
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/UpdatePermanentCachesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Vormkracht10\PermanentCache\Commands;

use Illuminate\Console\Command;
use ReflectionClass;
use Spatie\Emoji\Emoji;
use SplObjectStorage;
use Symfony\Component\Console\Helper\ProgressBar;
Expand Down Expand Up @@ -52,7 +51,6 @@ public function handle()
$progressBar = $this->output->createProgressBar($caches->count());

$progressBar->setFormat('custom');

$progressBar->setMessage('Starting...');

$progressBar->start();
Expand Down

0 comments on commit a0270ce

Please sign in to comment.