laravel-caching-strategies
Best practices for Laravel caching including cache patterns, tags, atomic locks, flexible cache, and cache invalidation strategies.
What this skill does
# Laravel Caching Strategies
## Core Cache Patterns
```php
use Illuminate\Support\Facades\Cache;
// ✅ remember - cache for a duration
$users = Cache::remember('users:active', now()->addMinutes(30), function () {
return User::where('active', true)->get();
});
// ✅ rememberForever - cache until manually cleared
$settings = Cache::rememberForever('app:settings', function () {
return Setting::all()->pluck('value', 'key');
});
// ✅ flexible - stale-while-revalidate pattern
// Fresh for 5 min, serves stale for up to 15 min while revalidating in background
$stats = Cache::flexible('dashboard:stats', [300, 900], function () {
return DashboardStats::calculate();
});
// ✅ put / get / forget
Cache::put('key', $value, now()->addHours(1));
$value = Cache::get('key', 'default');
Cache::forget('key');
// ❌ Querying the database when cache would suffice
$settings = Setting::all(); // Every request hits the DB
```
## Cache Tags for Grouped Invalidation
```php
// ✅ Tag related cache entries
Cache::tags(['posts', 'users'])->put("user:{$userId}:posts", $posts, 3600);
Cache::tags(['posts'])->put("post:{$postId}", $post, 3600);
Cache::tags(['users'])->put("user:{$userId}:profile", $profile, 3600);
// ✅ Flush all caches for a tag group
Cache::tags(['posts'])->flush(); // Clears all post-related caches
// ✅ Retrieve tagged cache
$posts = Cache::tags(['posts', 'users'])->get("user:{$userId}:posts");
// Note: Cache tags are only supported by redis and memcached drivers
```
## Atomic Locks
```php
use Illuminate\Support\Facades\Cache;
// ✅ Prevent concurrent execution
$lock = Cache::lock('processing:order:' . $orderId, 10); // 10 second lock
if ($lock->get()) {
try {
// Process order exclusively
$this->processOrder($orderId);
} finally {
$lock->release();
}
}
// ✅ Block and wait for lock (up to 5 seconds)
$lock = Cache::lock('report:generate', 30);
$lock->block(5, function () {
// Acquired lock, do work
$this->generateReport();
}); // Lock auto-released after closure
// ✅ Cross-process lock with owner token
$lock = Cache::lock('deployment', 120);
if ($lock->get()) {
$owner = $lock->owner();
// Pass $owner to another process
}
// In the other process
Cache::restoreLock('deployment', $owner)->release();
// ❌ Forgetting to release locks
$lock->get();
$this->doWork(); // If this throws, lock is never released
```
## Cache Memoization
```php
use Illuminate\Support\Facades\Cache;
// ✅ memo - in-memory cache for the current request lifecycle
// Avoids repeated cache store lookups within the same request
$config = Cache::memo('app:config', function () {
return Config::loadFromDatabase();
});
// Subsequent calls return the in-memory value without hitting Redis/Memcached
$config = Cache::memo('app:config', fn () => Config::loadFromDatabase());
```
## Model Caching Patterns
```php
class Product extends Model
{
// ✅ Cache on read with automatic invalidation
public static function findCached(int $id): ?self
{
return Cache::remember(
"product:{$id}",
now()->addHour(),
fn () => static::find($id)
);
}
// ✅ Invalidate cache on model changes
protected static function booted(): void
{
static::saved(function (Product $product) {
Cache::forget("product:{$product->id}");
Cache::tags(['products'])->flush();
});
static::deleted(function (Product $product) {
Cache::forget("product:{$product->id}");
Cache::tags(['products'])->flush();
});
}
}
// ✅ Cache query results with tags for group invalidation
class ProductService
{
public function getFeatured(): Collection
{
return Cache::tags(['products'])->remember(
'products:featured',
now()->addMinutes(30),
fn () => Product::where('featured', true)->with('category')->get()
);
}
}
```
## Cache Key Conventions
```php
// ✅ Use consistent, descriptive key patterns
"user:{$userId}:profile"
"post:{$postId}:comments:page:{$page}"
"tenant:{$tenantId}:settings"
"api:github:repos:{$username}"
"report:daily:{$date}"
// ✅ Include cache-busting identifiers when data shape changes
"v2:user:{$userId}:profile"
// ❌ Vague or collision-prone keys
"data"
"user"
"temp"
"cache_1"
```
## Common Pitfalls
```php
// ❌ Caching null results without handling them
$user = Cache::remember("user:{$id}", 3600, fn () => User::find($id));
// If user doesn't exist, null is cached for an hour
// ✅ Handle null explicitly
$user = Cache::remember("user:{$id}", 3600, function () use ($id) {
return User::find($id) ?? new NullUser();
});
// ❌ No invalidation strategy
Cache::forever('products:all', Product::all());
// Data becomes stale with no way to refresh
// ✅ Use TTL or event-based invalidation
Cache::remember('products:all', now()->addMinutes(15), fn () => Product::all());
// ❌ Caching too aggressively (serialization cost > query cost)
Cache::remember('user:count', 3600, fn () => User::count());
// Simple COUNT queries are often fast enough without caching
// ✅ Cache expensive operations
Cache::remember('dashboard:analytics', 3600, function () {
return DB::table('orders')
->selectRaw('DATE(created_at) as date, SUM(total) as revenue')
->groupByRaw('DATE(created_at)')
->orderBy('date')
->get();
});
// ❌ Cache stampede - many requests regenerate cache simultaneously
// ✅ Use flexible() for stale-while-revalidate or atomic locks for regeneration
```
## Checklist
- [ ] remember/rememberForever used instead of manual get/put
- [ ] flexible() used for high-traffic keys that tolerate brief staleness
- [ ] Cache tags used for grouped invalidation (Redis/Memcached only)
- [ ] Atomic locks used for exclusive operations
- [ ] Model caches invalidated on save/delete events
- [ ] Cache keys follow a consistent naming convention
- [ ] TTLs set appropriately (not too long, not too short)
- [ ] Null/empty results handled to avoid caching nothing
- [ ] Expensive queries cached, trivial queries left uncached
- [ ] Cache stampede prevented with flexible() or locks
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.