Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kai-init's avatar

I built a automatic normalized caching layer for Eloquent — massive Redis memory savings

Hey everyone 👋

Over the past few months, I’ve been working on a small open-source package called laravel-normcache.

It actually started from a problem I kept running into across a few different projects. I found myself repeatedly building the same kind of caching layer to avoid redundant Eloquent data sitting everywhere in Redis, so eventually I thought: why not turn it into a reusable package and learn more deeply how Laravel caching works under the hood?

I’ll be honest — this is still an early project, and definitely not perfect 😅 A lot of the ideas are inspired by patterns used in things like Redux/Apollo normalized stores, and I’ve been experimenting and refining things as I use it across real projects.

The main idea behind laravel-normcache is normalizing cached Eloquent results instead of storing entire query collections repeatedly. It stores individual models and query ID maps separately, so when one model changes, every cached query containing that model automatically reflects the update.

Some things it currently does:

  • Automatic caching + invalidation with just a single trait
  • Avoids storing duplicate model data across query caches
  • Instant consistency across cached query results
  • O(1) invalidation using versioned cache keys
  • Uses dramatically less Redis memory compared to traditional query caching approaches

There’s still plenty I want to improve and optimize over time, but I felt it was finally in a good enough place to share with the community.

Here’s a quick benchmark comparing direct DB queries vs laravel-model-cache for reference:

benchmark

I’d genuinely appreciate any feedback, criticism, ideas, or contributions from people more experienced than me. I’m mainly here to learn and improve as an open-source developer.

Repository: https://github.com/kai-init/laravel-normcache

Thanks for reading 🙏

0 likes
0 replies

Please or to participate in this conversation.