Let's imagine for a moment you are the creator of a popular CMS, written in PHP. It's used by many people, for many websites. Now let's talk a bit about caching techniques. There is one function (getItemByHandle
) in a service class to get a specific item from a specific table from the database.
In respect of the overall performance, would you rather
- write a database query to fetch this specific item and cache it per handle,
- or, would you rather fetch all items, cache them and then filter the desired item using a foreach-loop in PHP?
Both techniques would cache it only for the current request. The database output is only stored in a PHP variable. So no further caching techniques like Key-Value-Store or so.
The first technique would only cache already fetched items. Each getItemByHandle(...)
call with a different handle would cause a new database hit.
However the second technique would only hit the database once (per request), but it also means that there is more traffic from the database and PHP has more workload. So if there would be thousands of entries in the table it would fetch all for each request, but would not hit the database on further getItemByHandle(...)
calls within the same request.
I'm asking because maybe there are also other aspects to consider which are not described here.