Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache duration configurability #76

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config/markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@
*/
'cache_store' => null,


/*
* When cache_store is enabled, this value will be used to determine
* how long the cache will be valid. If you set this to `null` the
* cache will never expire.
*
*/
'cache_duration' => null,

/*
* This class will convert markdown to HTML
*
Expand Down
6 changes: 5 additions & 1 deletion docs/advanced-usage/caching-results.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ weight: 2

Code highlighting is a resource intensive process. That's why the component ships with caching out of the box. By default, the component uses the default cache store.

To configure the store to use, or to disable caching, change the value of the `cache_store` param in the `markdown` config file.
To configure the store to use, or to disable caching, change the value of the `cache_store` param in the `markdown` config file. Caching is enabled by default.

Additionally, you can set the duration for how long the results are stored in the cache
by setting the `cache_duration` param in the `markdown` config file. By default the
results will be cached forever if caching is enabled.
9 changes: 8 additions & 1 deletion docs/installation-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ return [
*/
'cache_store' => null,

/*
* When cache_store is enabled, this value will be used to determine
* how long the cache will be valid. If you set this to `null` the
* cache will never expire.
*
*/
'cache_duration' => null,

/*
* This class will convert markdown to HTML
*
Expand Down Expand Up @@ -122,4 +130,3 @@ return [
],
];
```

1 change: 1 addition & 0 deletions src/MarkdownBladeComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function toHtml(string $markdown): string
blockRenderers: $config['block_renderers'],
inlineRenderers: $config['inline_renderers'],
inlineParsers: $config['inline_parsers'],
cacheDuration: $config['cache_duration'],
);

return $markdownRenderer->toHtml($markdown);
Expand Down
16 changes: 15 additions & 1 deletion src/MarkdownRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function __construct(
protected array $blockRenderers = [],
protected array $inlineRenderers = [],
protected array $inlineParsers = [],
protected int | null $cacheDuration = null,
) {
}

Expand Down Expand Up @@ -64,6 +65,13 @@ public function cacheStoreName(string | bool | null $cacheStoreName): self
return $this;
}

public function cacheDuration(int | null $cacheDuration): self
{
$this->cacheDuration = $cacheDuration;

return $this;
}

public function renderAnchors(bool $renderAnchors): self
{
$this->renderAnchors = $renderAnchors;
Expand Down Expand Up @@ -121,9 +129,15 @@ public function toHtml(string $markdown): string

$cacheKey = $this->getCacheKey($markdown);

if($this->cacheDuration === null) {
return cache()->rememberForever($cacheKey, function () use ($markdown) {
return $this->convertMarkdownToHtml($markdown);
});
}

return cache()
->store($this->cacheStoreName)
->rememberForever($cacheKey, function () use ($markdown) {
->remember($cacheKey, $this->cacheDuration, function () use ($markdown) {
return $this->convertMarkdownToHtml($markdown);
});
}
Expand Down
1 change: 1 addition & 0 deletions src/MarkdownServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function configurePackage(Package $package): void
blockRenderers: $config['block_renderers'],
inlineRenderers: $config['inline_renderers'],
inlineParsers: $config['inline_parsers'],
cacheDuration: $config['cache_duration'],
);
});
}
Expand Down
31 changes: 31 additions & 0 deletions tests/MarkdownBladeComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\LaravelMarkdown\Tests;

use Carbon\Carbon;
use League\CommonMark\Extension\GithubFlavoredMarkdownExtension;

it('the component can render markdown', function () {
Expand Down Expand Up @@ -140,6 +141,36 @@
expect(cache()->get($cacheKey))->not->toBeNull();
});

it('the component will cache results for the specified duration', function () {

$duration = 60;

config()->set('markdown.cache_duration', $duration);

$cacheKey = 'd1cd0dc15c848738f347cb539578252f';

$markdown = <<<BLADE
<x-markdown>
```php
echo 'Hello world';
```
</x-markdown>
BLADE;

expect(cache()->get($cacheKey))->toBeNull();
expect(config('markdown.cache_duration'))->toBe($duration);

(string)$this->blade($markdown);

expect(cache()->get($cacheKey))->not->toBeNull();

Carbon::setTestNow(now()->addSeconds($duration)->addSecond());

expect(cache()->get($cacheKey))->toBeNull();

});


it('caching can be disabled', function () {
config()->set('markdown.cache_store', false);

Expand Down
Loading