I have a page which performs following operations:
- Executes a query in a DB (More precisely, select u.user_uid, ut.user_metatada from users u, user_tag ut where u.uid=ut.uid. One user can have one or more metadatas)
- Generates an array of data based on result of the query
- Performs some complicated operation on these arrays (More about this operation read in this post.). Returns final result as an array. This array is one-dimensional array containing user_uids.
- Executes another query and gets user_fullname, user_photo of all elements of user_uid
- Prints result of query in a page.
When user reloads the page these 5 operations will be performed from the beginning. In order to avoid execution of all these 5 operations every time user reloads the page we can save final result (operation number 5) in a session. So we can say following:
If session is not set
Perform those 5 operations
else
Print session values
This will reduce CPU load but it will take some (I don’t know how much) memory instead to save information in a sessions. But somebody argue that it is better to load CPU rather than a memory.
Question: What do you recommend me to do? To use sessions or not (in my case)? Are there another better solutions also?
Use APC or Memcached. They are in memory key-value caches designed for exactly this purpose. You can store arrays in them if you want, and set expiration time for invalidation. Facebook uses them to reduce load, so they can’t be bad… 🙂