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

screwtape_mk's avatar

Why Keys when accessing a cache

I am trying to learn the basics of caching a database query. There is plenty of examples on how to do so. Except i want to also understand the concept/theory of caching in laravel. None of the material ive seen actually bothers to explain why you need a cache key. SO why must u have a key to access a cache?

0 likes
5 replies
Snapey's avatar
Snapey
Best Answer
Level 122

the key is a label

just like putting the data (the result of a query) into a variable. When you want to access the data you use the variable.

In the cache it puts all the data in a flat space, with the data held under a specific label (key)

1 like
Jaytee's avatar

Caching is generally a key/value pair. The key is unique, and by searching that key, you then get the value.

Kind of like a database, in order to get a user by their id, you need to search them up by their unique id right? Same goes for caching. In order to get the cached value, you need to search it up by it's key.

1 like
screwtape_mk's avatar

Thanks Guys- for a beginner this helps in grasping the concepts - copying a solution as is without understanding concepts isnt the greatest idea

Please or to participate in this conversation.