From 96d6671b8fcbd7c86657b775dfce92664d7e7ce8 Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 10:35:01 +0200 Subject: [PATCH 01/10] add markers to cached component --- config/permanent-cache.php | 10 ++++++++++ src/CachedComponent.php | 26 ++++++++++++++++++++++++-- src/PermanentCacheServiceProvider.php | 3 ++- 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 config/permanent-cache.php diff --git a/config/permanent-cache.php b/config/permanent-cache.php new file mode 100644 index 0000000..32f03d7 --- /dev/null +++ b/config/permanent-cache.php @@ -0,0 +1,10 @@ + [ + 'markers' => [ + 'enabled' => env('PERMANENT_CACHE_MARKERS_ENABLED', true), + 'hash' => env('PERMANENT_CACHE_MARKERS_HASH', false), + ], + ], +]; diff --git a/src/CachedComponent.php b/src/CachedComponent.php index 325a719..cb27e17 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -19,11 +19,33 @@ public function resolveView() $this->isUpdating || $this->shouldBeUpdating() ) { - return parent::resolveView(); + return $this->setMarkers(parent::resolveView()); } if (null !== $cachedValue = $this->get($this->getParameters())) { - return new HtmlString((string) $cachedValue); + return new HtmlString($this->setMarkers((string) $cachedValue)); } } + + public function getMarker(): string + { + [$cacheDriver, $cacheKey] = $this::store($this->getParameters()); + + $marker = $cacheDriver.':'.$cacheKey; + + if(config('permanent-cache.components.markers.hash')) { + $marker = md5($marker); + } + + return ''; + } + + public function setMarkers($value): string + { + if(! config('permanent-cache.components.markers.enabled')) { + return $value; + } + + return $this->getMarker().$value.$this->getMarker(); + } } diff --git a/src/PermanentCacheServiceProvider.php b/src/PermanentCacheServiceProvider.php index cfa3528..4afb9fb 100644 --- a/src/PermanentCacheServiceProvider.php +++ b/src/PermanentCacheServiceProvider.php @@ -10,7 +10,8 @@ class PermanentCacheServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { - $package->name('laravel-permanent-cache'); + $package->name('laravel-permanent-cache') + ->hasConfigFile(); } public function registeringPackage() From 5ea2410d56a742c7486c19b4f852c0cb807c8ba6 Mon Sep 17 00:00:00 2001 From: markvaneijk Date: Sat, 27 Apr 2024 08:35:21 +0000 Subject: [PATCH 02/10] Fix styling --- src/CachedComponent.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CachedComponent.php b/src/CachedComponent.php index cb27e17..6be143b 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -33,7 +33,7 @@ public function getMarker(): string $marker = $cacheDriver.':'.$cacheKey; - if(config('permanent-cache.components.markers.hash')) { + if (config('permanent-cache.components.markers.hash')) { $marker = md5($marker); } @@ -42,7 +42,7 @@ public function getMarker(): string public function setMarkers($value): string { - if(! config('permanent-cache.components.markers.enabled')) { + if (! config('permanent-cache.components.markers.enabled')) { return $value; } From 01173b5ca3cb32f7656b8ed7f0afd31490f6817b Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 10:41:02 +0200 Subject: [PATCH 03/10] add comments --- config/permanent-cache.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/permanent-cache.php b/config/permanent-cache.php index 32f03d7..0aff5fb 100644 --- a/config/permanent-cache.php +++ b/config/permanent-cache.php @@ -2,6 +2,10 @@ return [ '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', false), From d62788ad051273c6bd8c185188948ea481f8517e Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 10:44:05 +0200 Subject: [PATCH 04/10] wip --- config/permanent-cache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/permanent-cache.php b/config/permanent-cache.php index 0aff5fb..92af95a 100644 --- a/config/permanent-cache.php +++ b/config/permanent-cache.php @@ -2,8 +2,9 @@ return [ 'components' => [ - // Add markers around the rendered value of Cached Components - // This helps to identify the cached value in the rendered HTML + // 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' => [ From 228fda5ea095ae975fa5cbd7ae7f252cca814f0d Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 10:44:27 +0200 Subject: [PATCH 05/10] wip --- config/permanent-cache.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/permanent-cache.php b/config/permanent-cache.php index 92af95a..0882044 100644 --- a/config/permanent-cache.php +++ b/config/permanent-cache.php @@ -2,8 +2,8 @@ return [ 'components' => [ - // Add markers around the rendered value of Cached Components. - // This helps to identify the cached value in the rendered HTML. + // 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 From 85a0e9d770fe9c3b4e71d03908a41a0c34165783 Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 10:47:20 +0200 Subject: [PATCH 06/10] wip --- config/permanent-cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/permanent-cache.php b/config/permanent-cache.php index 0882044..d699333 100644 --- a/config/permanent-cache.php +++ b/config/permanent-cache.php @@ -8,7 +8,7 @@ // 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), + 'enabled' => env('PERMANENT_CACHE_MARKERS_ENABLED', false), 'hash' => env('PERMANENT_CACHE_MARKERS_HASH', false), ], ], From 35f8141231818280489939e3ec0cb273988f8763 Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 10:51:46 +0200 Subject: [PATCH 07/10] wip --- src/CachedComponent.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CachedComponent.php b/src/CachedComponent.php index 6be143b..c7e2fc0 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -19,11 +19,11 @@ public function resolveView() $this->isUpdating || $this->shouldBeUpdating() ) { - return $this->setMarkers(parent::resolveView()); + return $this->renderOutput(parent::resolveView()); } if (null !== $cachedValue = $this->get($this->getParameters())) { - return new HtmlString($this->setMarkers((string) $cachedValue)); + return new HtmlString($this->renderOutput((string) $cachedValue)); } } @@ -40,7 +40,7 @@ public function getMarker(): string return ''; } - public function setMarkers($value): string + public function renderOutput($value): string { if (! config('permanent-cache.components.markers.enabled')) { return $value; From 4d47fe57d54eb36493c6b977ce721bd4c44a3455 Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 15:11:54 +0200 Subject: [PATCH 08/10] wip --- src/CachedComponent.php | 12 +++++++----- src/CachesValue.php | 9 ++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/CachedComponent.php b/src/CachedComponent.php index c7e2fc0..555692f 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -19,11 +19,13 @@ public function resolveView() $this->isUpdating || $this->shouldBeUpdating() ) { - return $this->renderOutput(parent::resolveView()); + return $this->renderOutput( + parent::resolveView() + ); } if (null !== $cachedValue = $this->get($this->getParameters())) { - return new HtmlString($this->renderOutput((string) $cachedValue)); + return (new HtmlString($this->renderOutput((string) $cachedValue))); } } @@ -40,12 +42,12 @@ public function getMarker(): string return ''; } - public function renderOutput($value): string + public function renderOutput($value): HtmlString { if (! config('permanent-cache.components.markers.enabled')) { - return $value; + return new HtmlString($value); } - return $this->getMarker().$value.$this->getMarker(); + return new HtmlString($this->getMarker().$value.$this->getMarker()); } } diff --git a/src/CachesValue.php b/src/CachesValue.php index 380c48a..b78ca30 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -74,7 +74,7 @@ final public function handle($event = null): void PermanentCacheUpdating::dispatch($this); $value = is_subclass_of(static::class, CachedComponent::class) - ? Blade::renderComponent($this) + ? (string) Blade::renderComponent($this) : $this->run($event); if (is_null($value)) { @@ -152,8 +152,11 @@ final public static function get($default = null, bool $update = false): mixed $cache = Cache::driver($driver); - if ($update && ! $cache->has($cacheKey)) { - static::update($parameters ?? [])->onConnection('sync'); + if ( + $update || + ! $cache->has($cacheKey) + ) { + static::update($parameters ?? [])?->onConnection('sync'); } return $cache->get($cacheKey, $default); From 1e6525cdea7961e2018a3671b1b9f9fd88c0175a Mon Sep 17 00:00:00 2001 From: markvaneijk Date: Sat, 27 Apr 2024 13:12:22 +0000 Subject: [PATCH 09/10] Fix styling --- src/CachedComponent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CachedComponent.php b/src/CachedComponent.php index 555692f..f274558 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -25,7 +25,7 @@ public function resolveView() } if (null !== $cachedValue = $this->get($this->getParameters())) { - return (new HtmlString($this->renderOutput((string) $cachedValue))); + return new HtmlString($this->renderOutput((string) $cachedValue)); } } From 1adef0305799541425bc5e935848c71f15bda636 Mon Sep 17 00:00:00 2001 From: Mark van Eijk Date: Sat, 27 Apr 2024 21:19:01 +0200 Subject: [PATCH 10/10] wip --- src/CachedComponent.php | 6 +++--- src/CachesValue.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CachedComponent.php b/src/CachedComponent.php index 555692f..43eab76 100644 --- a/src/CachedComponent.php +++ b/src/CachedComponent.php @@ -44,10 +44,10 @@ public function getMarker(): string public function renderOutput($value): HtmlString { - if (! config('permanent-cache.components.markers.enabled')) { - return new HtmlString($value); + if (config('permanent-cache.components.markers.enabled')) { + $value = $this->getMarker().$value.$this->getMarker(); } - return new HtmlString($this->getMarker().$value.$this->getMarker()); + return new HtmlString($value); } } diff --git a/src/CachesValue.php b/src/CachesValue.php index b78ca30..ad0e47e 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -94,7 +94,7 @@ public function getParameters() ->getProperties(\ReflectionProperty::IS_PUBLIC)) ->filter(fn (\ReflectionProperty $p) => $p->class === static::class) ->mapWithKeys(fn (\ReflectionProperty $p) => [$p->name => $p->getValue($this)]) - ->toArray(); + ->all(); } /**