Skip to content

Glossary

Laravel cache: keeping computed results instead of redoing them

A store for results that are expensive to work out and rarely change.

Some results are expensive. A report across two years of order data, a price list assembled from several sources, a permission check with many branches. When the result rarely changes, working it out again on every call is waste.

A cache keeps it. Next time nothing is computed, it is looked up. Laravel offers one interface for this whether the store sits in the file system, in the database or in Redis. For anything that has to be fast, Redis is the usual foundation.

The hard question is the second one

Keeping something is easy. Knowing when it no longer holds is the actual work. A price changes, a customer moves to different terms, an article is deactivated. Every time, the store has to find out.

So we use caching deliberately rather than everywhere. Measure first where the time actually goes, then keep results exactly there, and for each place decide what makes the entry invalid. A cache nobody invalidates is a fault waiting on a timer.

FAQ

Häufige Fragen

Why do we sometimes see stale data?
Because a cache is holding a result that has since changed and nobody told it. So every cache needs a rule for what makes it invalid. Without one you get exactly this effect, and it is hard to find, because everything works and only the numbers are wrong.

Related

Terms that belong with this one

A project, or a question?

Write and tell us what it is about. You get an assessment from somebody who works with Laravel themselves, not from a sales desk.