Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Blade Cache Directive

Blade Cache Directive Laravel Package

ryangjchandler/blade-cache-directive

A lightweight Laravel package that adds an @cache Blade directive, letting you cache rendered fragments of views—partials, loops, or expensive blocks—with a TTL and optional key. Drop it into existing templates to cut rendering time without extra setup.

View on GitHub
Deep Wiki
Context7

Cache chunks of your Blade markup with ease. 🔪

Frequently asked questions about Blade Cache Directive
How do I install ryangjchandler/blade-cache-directive?
Run composer require ryangjchandler/blade-cache-directive in your Laravel project. There is no config file to publish and no manual service provider registration is needed thanks to package discovery. Once installed, the @cache directive is available in every Blade template.
How do I use the @cache directive in a Blade template?
Wrap the markup you want cached between @cache and @endcache. Pass a time-to-live in seconds as the first argument, for example @cache(3600) followed by your partial or loop and then @endcache. The rendered fragment is stored in Laravel's default cache store and reused until it expires.
Can I give a cached Blade fragment a custom cache key?
Yes. The directive accepts an optional second argument that is used as the cache key, for example @cache(3600, 'nav-'.$locale). Use a key that uniquely identifies the fragment's content, including any locale, user, or tenant variation, so different versions never collide.
Which Laravel and PHP versions does this package support?
Laravel 10, 11, and 12 on PHP 8.x are a safe target, and release v1.1.1 widened the Composer constraint to allow Laravel 13.x. That bump is a forward-compatibility signal rather than proof of CI coverage, so run your own tests before adopting a new major. Pin the package version if you need guaranteed support.
Does @cache support cache tags for invalidation?
The directive is a thin wrapper around Laravel's Cache facade, so tag support depends entirely on your cache store. Redis and Memcached support tags, while the file and database drivers do not. If you plan to flush fragments by tag, confirm your configured store supports it before you rely on it.
Is it safe to cache user-specific content with @cache?
Only if you build a discriminating key. A fragment cached under a generic key such as the route or template name will be served to every visitor, leaking one user's content to another. Include the user ID, tenant, or locale in the key, or leave personalized fragments uncached.
How do I clear or invalidate a cached Blade fragment?
Call Cache::forget() with the same key you passed to @cache, for example from a model observer when the underlying data changes. If you rely on the TTL alone, the fragment simply expires after the number of seconds you supplied. Tag-based flushing only works on stores that support tags, such as Redis or Memcached.
Which cache stores work with the @cache directive?
Any store configured for Laravel's cache works, including file, database, Redis, Memcached, and array. The directive just calls the framework cache, so performance and eviction behaviour follow whatever driver you have configured. For anything beyond simple TTL expiry, Redis or Memcached is the practical choice.
How is @cache different from Cache::remember in a view composer?
Functionally it is the same operation: the package wraps Cache::remember around the compiled Blade fragment. The difference is ergonomics, since you can cache directly in the template instead of moving the logic into a view composer or a @php block. Teams already standardized on view composers may see it as a second idiom for the same job.
Is ryangjchandler/blade-cache-directive maintained and production ready?
It is a small, single-purpose package with a single maintainer, so the bus factor is worth noting. The code surface is tiny and depends only on Laravel's cache contract and the Blade compiler, which keeps breakage risk low. Pin the version and add tests covering your cache keys and TTL expectations before shipping.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections