views:

57

answers:

4

I have a page which performs following operations:

  1. 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)
  2. Generates an array of data based on result of the query
  3. 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.
  4. Executes another query and gets user_fullname, user_photo of all elements of user_uid
  5. 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?

A: 

In your case: what do you need more, CPU or memory?

If the result of these 5 operations doesn't change often, by all means, cache the result (either in sessions or on disk).

Piskvor
+2  A: 

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... :)

galambalazs
A: 

You could also use something like memcached

And if it's better trying to decrease cpu load, or memory, it depends.

Try to observe your cpu and memory consumption, which is hitting it's limits first, that one is the on you should try to decrease.

jigfox
A: 

You could also store your result in a new DB-Table. Or in a session.

The memory used, will depend on how many users visit your site. Each user will generate a new entry in the session_save_path directory (that is where session information is stored).

Also note, that after some time PHP will delete old entries from this directory, meaning the value will have to be regenerated the next time the user visits your page. But which also means, that memory space is saved.

JochenJung