Custom cache profiles | laravel-responsecache | Spatie

 SPATIE

  Laravel Response Cache
=========================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-responsecache](https://spatie.be/docs/laravel-responsecache/v8)  Advanced-usage  Custom cache profiles

 Version   v8

 Other versions for crawler [v8](https://spatie.be/docs/laravel-responsecache/v8)

- [ Introduction ](https://spatie.be/docs/laravel-responsecache/v8/introduction)
- [ Support us ](https://spatie.be/docs/laravel-responsecache/v8/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-responsecache/v8/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-responsecache/v8/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-responsecache/v8/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-responsecache/v8/changelog)
- [ About us ](https://spatie.be/docs/laravel-responsecache/v8/about-us)

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

- [ Caching responses ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/caching-responses)
- [ Flexible caching (SWR) ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/flexible-caching)
- [ Preventing caching ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/preventing-caching)
- [ Clearing the cache ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/clearing-the-cache)
- [ Using tags ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/using-tags)
- [ Events ](https://spatie.be/docs/laravel-responsecache/v8/basic-usage/events)

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

- [ Custom cache profiles ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/custom-cache-profiles)
- [ Creating a replacer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/creating-a-replacer)
- [ Customizing the serializer ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-serializer)
- [ Customizing the hasher ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/customizing-the-hasher)
- [ Configuration ](https://spatie.be/docs/laravel-responsecache/v8/advanced-usage/configuration)

 Custom cache profiles
=====================

A cache profile determines which requests should be cached, for how long, and how to differentiate between users. The default profile `CacheAllSuccessfulGetRequests` caches all successful GET requests (excluding AJAX) that return text based content.

You can create your own cache profile by implementing the `CacheProfile` interface.

```
namespace App\CacheProfiles;

use Illuminate\Http\Request;
use Spatie\ResponseCache\CacheProfiles\CacheProfile;
use Symfony\Component\HttpFoundation\Response;

class CustomCacheProfile implements CacheProfile
{
    public function enabled(Request $request): bool
    {
        return config('responsecache.enabled');
    }

    public function shouldCacheRequest(Request $request): bool
    {
        // Only cache GET and HEAD requests
        return $request->isMethod('GET') || $request->isMethod('HEAD');
    }

    public function shouldCacheResponse(Response $response): bool
    {
        return $response->isSuccessful();
    }

    public function cacheLifetimeInSeconds(Request $request): int
    {
        return 30 * 60;
    }

    public function useCacheNameSuffix(Request $request): string
    {
        // Return a unique string per user (or empty for shared cache)
        return auth()->user()?->id ?? '';
    }
}
```

Then register your custom profile in the config file.

```
// config/responsecache.php

'cache_profile' => App\CacheProfiles\CustomCacheProfile::class,
```

The `useCacheNameSuffix` method is used to differentiate cache entries between users. By default, it returns the authenticated user's ID, meaning each logged-in user gets their own cache. Return an empty string if you want all users to share the same cache.
