Eloquent Query Cache
Github Repo
3.x
3.x
  • ⚡Introduction
  • 🎉Support
  • Getting Started
    • 🚀Installation
    • 🙌Showcase
  • Caching
    • ⚡Query Caching
    • 👥Relationships Caching
    • 📑Cache Tags
    • 🛑Cache Invalidation
      • 🌍Global Cache Invalidation
      • 🍸Automatic Invalidation
      • 🤔Many-to-Many Automatic Cache Invalidation
  • Advanced
    • ✨Implement Caching
      • 🔑Generating your own key
      • 🎇Implementing cache for other functions than get()
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Caching

Cache Invalidation

In the previous example with the bookshelves, you might as well invalidate tags.

$shelfOneBooks = Book::where('shelf_id', 1)
    ->cacheTags(['shelf:1'])
    ->get();

$shelfTwoBooks = Book::where('shelf_id', 2)
    ->cacheTags(['shelf:2'])
    ->get();

Flushing is done by calling flushQueryCache with the respective tags.

Book::flushQueryCache(['shelf:1']);

You can as well invalidate more tags at once if needed:

Book::flushQueryCache(['shelf:1', 'shelf:2']);

Tags vs SQL Hash Key

Please be careful that the same tags do not mean the same cached key. In the following example, we might have a users table and retrieve them by their name.

Even when specifying the same tag, the caching will be done according to the SQL query (which is different), but we can invalidate both caches if needed later:

$alice = User::whereName('Alice')
    ->cacheTags(['users'])
    ->first();

$bob = User::whereName('Bob')
    ->cacheTags(['users'])
    ->first();

Book::flushQueryCache(['users']);
PreviousCache TagsNextGlobal Cache Invalidation

Last updated 3 years ago

Was this helpful?

🛑