##Clearing the entire cache
You can clear the entire response cache programmatically.
use Spatie\ResponseCache\Facades\ResponseCache;
ResponseCache::clear();
You can also clear it using the artisan command.
php artisan responsecache:clear
##Forgetting specific URIs
You can forget specific URIs.
use Spatie\ResponseCache\Facades\ResponseCache;
ResponseCache::forget('/some-uri');
ResponseCache::forget(['/some-uri', '/other-uri']);
ResponseCache::forget('/some-uri', '/other-uri');
You can also forget a specific URI using the artisan command.
php artisan responsecache:clear --url=/some-uri
##Forgetting a selection of cached items
For more control, you can use selectCachedItems() to specify exactly which cached items should be forgotten.
use Spatie\ResponseCache\Facades\ResponseCache;
ResponseCache::selectCachedItems()
->withPutMethod()
->forUrls('/some-uri')
->forget();
ResponseCache::selectCachedItems()
->forUrls('/some-uri', '/other-uri')
->forget();
ResponseCache::selectCachedItems()
->usingSuffix('100')
->forUrls('/some-uri')
->forget();
ResponseCache::selectCachedItems()
->withPutMethod()
->withHeaders(['foo' => 'bar'])
->withCookies(['cookie1' => 'value'])
->withParameters(['param1' => 'value'])
->withRemoteAddress('127.0.0.1')
->usingSuffix('100')
->usingTags('tag1', 'tag2')
->forUrls('/some-uri', '/other-uri')
->forget();
##Using model events
You can clear the cache whenever a model changes by using model events.
namespace App\Traits;
use Spatie\ResponseCache\Facades\ResponseCache;
trait ClearsResponseCache
{
public static function bootClearsResponseCache(): void
{
self::created(fn () => ResponseCache::clear());
self::updated(fn () => ResponseCache::clear());
self::deleted(fn () => ResponseCache::clear());
}
}
##Clearing tagged content
If you're using cache tags, you can clear only responses with specific tags.
use Spatie\ResponseCache\Facades\ResponseCache;
ResponseCache::clear(['posts']);
ResponseCache::clear(['foo', 'bar']);