Skip to content

Commit

Permalink
add configurable markers
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 10, 2024
1 parent fe128c2 commit 9248b05
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"laravel/helpers": "^1.7",
"lorisleiva/cron-translator": "^0.4.5",
"spatie/emoji": "^4.1",
"spatie/laravel-package-tools": "^1.14.0",
"spatie/once": "^3.1"
"spatie/laravel-package-tools": "^1.14.0"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down
17 changes: 17 additions & 0 deletions config/permanent-cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'driver' => env('PERMANENT_CACHE_DRIVER', 'redis'),

'components' => [
// Add markers around the rendered value of Cached Components,
// this helps to identify the cached value in the rendered HTML.

// Which is useful for debugging and testing, but also for updating
// the cached value inside another cache when using nested caches
'markers' => [
'enabled' => env('PERMANENT_CACHE_MARKERS_ENABLED', true),
'hash' => env('PERMANENT_CACHE_MARKERS_HASH', env('APP_ENV') === 'production'),
],
],
];
36 changes: 32 additions & 4 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\HtmlString;
use Illuminate\Support\Str;
use ReflectionClass;
use Vormkracht10\PermanentCache\Events\PermanentCacheUpdated;
Expand Down Expand Up @@ -69,6 +70,8 @@ final public function handle($event = null): void
? Blade::renderComponent($this)
: $this->run($event);

$value = $this->markValue($value);

Cache::driver($driver)->forever($cacheKey, (object) [
'value' => $value,
'updated_at' => now(),
Expand Down Expand Up @@ -128,9 +131,11 @@ final public static function updateAndGet($parameters = [])
{
$instance = app()->make(static::class, $parameters);

return dispatch(
dispatch(
$instance
)->onConnection('sync');

return static::get($parameters);
}

/**
Expand All @@ -147,9 +152,10 @@ final public static function get($parameters = [], $default = null, bool $update
$cache = Cache::driver($driver);

if (
$update && ! $cache->has($cacheKey)
$update &&
! $cache->has($cacheKey)
) {
static::update($parameters ?? []);
return static::updateAndGet($parameters ?? []);
}

return $cache->get($cacheKey, $default)?->value;
Expand Down Expand Up @@ -261,7 +267,7 @@ public static function getCacheKey(?array $parameters = [], ?string $store = nul
$cacheKey = $store;
}

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

if ($parameters) {
Expand All @@ -270,4 +276,26 @@ public static function getCacheKey(?array $parameters = [], ?string $store = nul

return [$cacheDriver, $cacheKey];
}

public function getMarker(array $parameters = [], $close = false): string
{
[$cacheDriver, $cacheKey] = $this::store($parameters ?? $this->getParameters());

$marker = $cacheDriver.':'.$cacheKey;

if (config('permanent-cache.components.markers.hash')) {
$marker = md5($marker);
}

return '<!--'.($close ? '/' : '').$marker.'-->';
}

public function markValue($value): string
{
if (config('permanent-cache.components.markers.enabled')) {
$value = $this->getMarker().$value.$this->getMarker(close: true);
}

return (string) $value;
}
}
3 changes: 2 additions & 1 deletion src/PermanentCacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public function configurePackage(Package $package): void
->hasCommands(
PermanentCachesStatusCommand::class,
UpdatePermanentCachesCommand::class
);
)
->hasConfigFile();
}

public function registeringPackage()
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/CachedComponent/CachedComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
});

test('test cached component with parameters is cached correctly', function () {
config(['permanent-cache.components.markers.enabled' => false]);

$randomString = str_random();

$cachedComponentWithParameters = new CachedComponent(value: $randomString);
Expand Down

0 comments on commit 9248b05

Please sign in to comment.