in reply to Ways of caching

You're doing a here a form of caching: Pre-generating (long) a document/html page/... only when needed (data changes) and providing it (fast) each time requested, instead of making generation on each requests.

Is there a better way to do it?

The principle is good, then the real means depends on you code...

____
HTH, Dominique
My two favorites:
If the only tool you have is a hammer, you will see every problem as a nail. --Abraham Maslow
Bien faire, et le faire savoir...