Laravel एक एकीकृत, expressive caching system प्रदान करता है जिसमें कई backends (Redis, Memcached, file, database, array) एक ही API के पीछे काम करते हैं। Caching महंगे परिणामों को संग्रहीत करती है ताकि उन्हें दोबारा compute न करना पड़े — यह सबसे अधिक प्रभावशाली performance optimizations में से एक है।
Cache API
use Illuminate\Support\Facades\Cache;
Cache::put('key', $value, 3600); // store for 1 hour (seconds)
Cache::get('key'); // retrieve (null if missing)
Cache::get('key', 'default'); // with a default
Cache::has('key');
Cache::forget('key'); // delete (invalidate)
Cache::forever('key', $value); // store with no expiry
remember() — get-or-compute pattern (सबसे उपयोगी)
// if cached, return it; otherwise run the closure, cache the result, and return it
$users = Cache::remember('active_users', 3600, function () {
return User::where('active', true)->get(); // expensive query — cached for 1 hour
});
Cache::remember() सुंदर, आम pattern है: यदि cached value मौजूद हो तो उसे लौटाता है, अन्यथा उसे compute करता है (closure चलाकर), cache करता है, और लौटा देता है — सब एक ही call में। महंगे query परिणामों को cache करने के लिए आदर्श।
Configurable backends (production के लिए Redis)
config/cache.php → choose the driver:
redis → fast, SHARED across servers (production standard for multi-server apps)
memcached → fast, shared
file/database → simpler, for smaller/single-server setups
array → in-memory, for testing
The SAME Cache API works regardless of backend → easy to switch.
एकीकृत API का अर्थ है कि वही code किसी भी backend के साथ काम करता है — और Redis production के लिए मानक है (कई server instances में साझा cache)।
Cache invalidation (कठिन हिस्सा)
// when data changes, clear the affected cache or it serves stale data
public function updateUser($user, $data) {
$user->update($data);
Cache::forget('active_users'); // INVALIDATE so the next read recomputes
}
// strategies: TTL (accept brief staleness), explicit forget on writes, cache tags
Cache::tags(['users'])->flush(); // tagged invalidation (Redis/Memcached)
यह क्यों महत्वपूर्ण है
Caching, Laravel applications के लिए सबसे अधिक प्रभावशाली performance optimizations में से एक है, और framework का एकीकृत, expressive caching system इसे सुलभ बनाता है — इसलिए इसे समझना तेज़, scalable applications बनाने के लिए मूल्यवान है।
मुख्य मूल्य है बार-बार होने वाले महंगे काम से बचना: धीमी database queries, जटिल computations, या external API calls के परिणामों को cache करने का अर्थ है कि बाद के requests तेज़ी से सर्व होते हैं, जिससे latency और load में नाटकीय रूप से कमी आती है।
Laravel की caching elegant है, विशेष रूप से Cache::remember() get-or-compute pattern (यदि cached data मौजूद हो तो उसे लौटाना, या एक ही साफ़ call में compute करना, cache करना और लौटाना — query परिणामों को cache करने के लिए आदर्श), और इसका backends में एकीकृत API का अर्थ है कि वही code किसी भी driver के साथ काम करता है।
production के लिए Redis उपयोग करना जानना (कई server instances में साझा cache — महत्वपूर्ण क्योंकि horizontally scale करते समय सरल per-server caches साझा नहीं होते) एक महत्वपूर्ण architectural बिंदु है।
समान रूप से महत्वपूर्ण है caching की केंद्रीय कठिनाई को समझना — invalidation (underlying data बदलने पर cached data को clear या refresh करना, TTLs, writes पर explicit forget, या cache tags के माध्यम से, ताकि stale data सर्व करने से बचा जा सके) — क्योंकि इसे गलत करना classic caching विफलता है जो उपयोगकर्ताओं को पुरानी जानकारी दिखाती है।
Laravel के caching system को समझना — API, remember pattern, backend चयन (scale के लिए Redis), और विशेष रूप से invalidation strategies — performant, scalable Laravel applications बनाने के लिए महत्वपूर्ण senior-level ज्ञान है, क्योंकि caching बड़े performance लाभ देती है लेकिन सही बने रहने के लिए सावधानीपूर्वक invalidation की आवश्यकता होती है।
