Cache converted responses | laravel-markdown-response | Spatie

 SPATIE

  Laravel Markdown Response
============================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-markdown-response](https://spatie.be/docs/laravel-markdown-response/v1)  Basic-usage  Cache converted responses

 Version   v1

 Other versions for crawler [v1](https://spatie.be/docs/laravel-markdown-response/v1)

- [ Introduction ](https://spatie.be/docs/laravel-markdown-response/v1/introduction)
- [ Requirements ](https://spatie.be/docs/laravel-markdown-response/v1/requirements)
- [ Install the package ](https://spatie.be/docs/laravel-markdown-response/v1/installation-setup)
- [ Questions &amp; issues ](https://spatie.be/docs/laravel-markdown-response/v1/questions-issues)
- [ Support us ](https://spatie.be/docs/laravel-markdown-response/v1/support-us)
- [ About us ](https://spatie.be/docs/laravel-markdown-response/v1/about-us)
- [ Changelog ](https://spatie.be/docs/laravel-markdown-response/v1/changelog)

Basic usage
-----------

- [ Serve markdown to AI agents ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/serve-markdown-to-ai-agents)
- [ Cache converted responses ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/cache-converted-responses)
- [ Choose a conversion driver ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/choose-a-conversion-driver)
- [ Test your setup ](https://spatie.be/docs/laravel-markdown-response/v1/basic-usage/test-your-setup)

Advanced usage
--------------

- [ Clean up HTML with preprocessors ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/clean-up-html-with-preprocessors)
- [ Clean up markdown with postprocessors ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/clean-up-markdown-with-postprocessors)
- [ Customize request detection ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/customize-request-detection)
- [ Listen to conversion events ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/listen-to-conversion-events)
- [ Convert HTML directly ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/convert-html-directly)
- [ CDN and cache layers ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/cdn-and-cache-layers)
- [ Response headers ](https://spatie.be/docs/laravel-markdown-response/v1/advanced-usage/response-headers)

 Cache converted responses
=========================

###  On this page

1. [ Understand cache keys ](#content-understand-cache-keys)
2. [ Clear the cache ](#content-clear-the-cache)
3. [ Customize cache key generation ](#content-customize-cache-key-generation)

Converting HTML to markdown on every request would be wasteful, so the package caches results by default. When a cached response exists, it's returned directly without running any controllers or conversion logic.

Caching is enabled with a one-hour TTL out of the box. You can adjust this via environment variables:

```
MARKDOWN_RESPONSE_CACHE_ENABLED=true
MARKDOWN_RESPONSE_CACHE_STORE=redis
MARKDOWN_RESPONSE_CACHE_TTL=3600
```

Set `MARKDOWN_RESPONSE_CACHE_STORE` to any cache store configured in `config/cache.php`. When left empty, the default cache store is used.

Understand cache keys
-----------------------------------------------------------------------------------------------------------------------

The cache key is generated from the request's host, path, and query string. Common tracking parameters like `utm_source`, `gclid`, and `fbclid` are stripped, so `https://example.com/about` and `https://example.com/about?utm_source=google` share the same cached response.

URLs ending in `.md` share the same cache key as their non-suffixed counterpart. So `/about.md` and `/about` (with `Accept: text/markdown`) hit the same cache entry.

You can customize the list of ignored parameters in the config file:

```
// config/markdown-response.php

'cache' => [
    'ignored_query_parameters' => [
        'utm_source',
        'utm_medium',
        'utm_campaign',
        // ...
    ],
],
```

Clear the cache
-----------------------------------------------------------------------------------------------------

You can clear the markdown cache with an artisan command:

```
php artisan markdown-response:clear
```

> Note: this flushes the entire configured cache store. If you're using a shared cache store, consider using a dedicated store for markdown responses.

Customize cache key generation
--------------------------------------------------------------------------------------------------------------------------------------------------

If the default cache key strategy doesn't fit your needs, you can extend the `GeneratesCacheKey` class and point to it in the config:

```
// config/markdown-response.php

'cache' => [
    'key_generator' => App\Actions\CustomCacheKey::class,
],
```

```
namespace App\Actions;

use Illuminate\Http\Request;
use Spatie\MarkdownResponse\Actions\GeneratesCacheKey;

class CustomCacheKey extends GeneratesCacheKey
{
    public function __invoke(Request $request): string
    {
        return 'markdown-response:' . hash('xxh128', $request->fullUrl());
    }
}
```
