Laravel Responsecache

spatie/laravel-responsecache image

Laravel Responsecache stats

Downloads
3.7M
Stars
2,218
Open Issues
2
Forks
213

View on GitHub →

Speed up a Laravel application by caching the entire response

Laravel Response Cache by Spatie

The Laravel Response Cache package is designed to enhance your application's performance by caching entire HTTP responses. This can significantly speed up your Laravel application, especially for read-heavy sites.

Key Features

  • Automatic Caching: By default, the package caches all successful GET requests that return text-based content, such as HTML or JSON, for one week.
  • Customization: You can customize request caching behavior, cache duration, and even bypass the cache with specific headers.
  • Cache Profiles: Implement your own logic to determine what gets cached using custom cache profiles.
  • Selective Cache Clearing: Provides options to clear the entire cache, specific URIs, or based on model events.
  • Middleware Integration: Easily integrate with Laravel's middleware to specify routes that should or should not be cached.
  • Support for Cache Tags: If your cache driver supports tags, you can tag cached responses for more fine-grained cache management.
  • Dynamic Content Replacement: Use replacers to dynamically adjust cached content before it is served, such as updating CSRF tokens.

Installation

  1. With Composer:

    composer require spatie/laravel-responsecache
  2. Publish Configuration:

    php artisan vendor:publish --tag="responsecache-config"
  3. Register Middleware: Add the CacheResponse and DoNotCacheResponse middleware to your HTTP kernel.

Usage

  • Basic Usage: The package works out-of-the-box for caching all successful GET requests.
  • Clearing Cache:
    • Manually via ResponseCache::clear().
    • Using Artisan command: php artisan responsecache:clear.
  • Prevent Caching: Use the doNotCacheResponse middleware to exclude routes from being cached.
  • Custom Cache Profiles: Implement the CacheProfile interface to define custom caching logic.

Advanced Configurations

  • Modify config/responsecache.php to tweak settings like cache duration, cache bypass headers, cache profiles, and more.
  • Utilize cache tags and selectors for granular cache management.

Testing

Run the package's tests with:

composer test

More Information

For detailed documentation, you can watch a tutorial video provided by Spatie, or explore the full capabilities by reviewing the extensive options in the published config file. This package is a go-to solution for improving Laravel performance through response caching, offering extensive customization to fit various caching needs.

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Laravel Responsecache Related Articles

HTTP Cache Packages for Laravel image

HTTP Cache Packages for Laravel

Read article
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian