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

Laravel Cache Laravel Package

stevenmaguire/laravel-cache

A Laravel package that extends the framework's caching layer with additional cache drivers and helpers, making it easy to plug alternative backends into your application's cache configuration.

View on GitHub
Deep Wiki
Context7

Getting Started

The package ships a single EloquentCacheTrait you add to an Eloquent model to get cache-backed reads. Start by installing it and adding the trait to one model:

composer require <vendor>/<package>
use Vendor\Package\Concerns\EloquentCacheTrait;

class Post extends Model
{
    use EloquentCacheTrait;
}

Look first at the trait itself (src/Concerns/EloquentCacheTrait.php) and any config file published via php artisan vendor:publish. As of 1.0.0 the trait no longer declares abstract methods, so a model can use it with zero boilerplate — no getCacheKey() / getCacheTtl() implementations required. That makes your first use case trivially small: cache a slow read query on a small reference model (categories, settings, countries) and verify with Cache::get() / your cache driver's CLI that keys appear and invalidate on save.

Implementation Patterns

Trait-per-model, not global. Apply EloquentCacheTrait only to models that are read-heavy and write-light. Because 1.0.0 removed the abstract methods, there is no compile-time contract forcing you to define cache behaviour — so treat configuration as an explicit, documented decision per model rather than something the trait enforces for you.

Explicit key strategy. With no abstract getCacheKey() in 1.0.0, define your own convention (e.g. a cacheKey() method or a $cacheKeyPrefix property on the model) and keep it in one place. Consistent keys are what make targeted invalidation possible; ad-hoc keys are what make stale data unavoidable.

Invalidation on write. Wire cache busting into saved / deleted model events (or a booted() hook on the trait's consumer) rather than scattering Cache::forget() calls through controllers. If you have relationships, decide deliberately whether a child write busts the parent's key — most staleness bugs live exactly there.

Read-through in repositories/queries. Use the trait's cached accessors at the query layer, not in Blade or API resources, so cached and uncached code paths return the same shape and you can swap them behind a feature flag.

Integration tips. Point the package at a shared cache store (Redis/Memcached) in multi-server deployments — the default file/array driver will silently give you per-node caches. Keep TTLs short for anything user-facing and combine TTL expiry with event-based invalidation as a safety net.

Gotchas and Tips

1.0.0 removed abstract methods from EloquentCacheTrait. This is the headline change. In practice it is additive for most consumers — existing models that already implemented the methods keep working, and new models no longer need them. But two things to check when upgrading:

  • If you were calling parent::getCacheKey() (or similar) from an overriding method, that parent implementation no longer exists — remove the call.
  • If your code relied on the trait's abstract methods to enforce that every caching model defines a key/TTL, that guardrail is gone. Add a booted() assertion, a base model, or a static analyser rule (PHPStan/Larastan) if you want that safety back.

Silent no-op risk. With no abstract contract, forgetting to configure a key or TTL fails quietly rather than loudly. Assert in tests that a second identical read hits the cache (e.g. Cache::spy() or query-count assertions with DB::listen).

Stale reads after bulk operations. Model::where(...)->update(...) and DB::table(...) writes bypass model events entirely, so event-based invalidation never fires. Route bulk writes through a job that flushes the relevant keys, or accept short TTLs.

Serialisation and toArray(). Cached models are serialised; if you cache collections of Eloquent models, remember that relationships are not lazy-loaded on the hydrated result. Eager-load before caching.

Cache driver drift between environments. Tests running on the array driver will pass while production (Redis, shared across apps) returns another app's data. Namespace keys with an app/env prefix.

Debugging. php artisan cache:clear is a blunt instrument — when hunting staleness, log the generated key on write and on read and diff them. A key mismatch (different prefix, different ordering of parameters) is the most common root cause.

Extension points. Since the trait is now leaner, prefer wrapping it: put shared key/TTL logic in your own CachesReads trait or base model that uses EloquentCacheTrait, so package upgrades stay isolated from your application's caching policy.

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.
shitoudev/permissionmanager
johannesclimacus/laravel-tui
hocvt/seotools
vitalybaev/sypexgeo
amphibee/themosis-ignition
gito-me/fast-excel
frankyso/laravel-ipaymu
geolize-studio/laravel-chile
boomerangui/chat
jinas/dhivatar
store-doc/laravel-storage-driver
plus-it/laravel-translation-manager
diephp/perhaps
diephp/telegraph
osmaviation/spreadsheet
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console