ah cheers for that...
Only problem is some sections of the query may be slightly different, i.e containing different IDs or different links... it will ultimatly bring back the same sorta stuff though...
As i dont think im explaining this very well i will try to think of an example of what would happen and why i want to cache it...
A game would be a perfect example...
In your game you have a character and items. When you log in with your character you are going to need details on your character and your items. Now either you do a query each time the page is loaded to retrieve all this information, or you cache it in the session on the first page load. This way when you display your inventory you would be able to just look at the cached information rather than having to query the database, however imagine that when you click on the item you are going to need to know more than just the name/image/amount of the item you have.
Now at this point you can either query the database using AJAX when the user presses on it to retrieve its description, any bonuses or requirements it may have or you could have it pre-cached within the session when you retrieve your character. However at this point you now have your session containing your core character details as well as his inventory item list, and text data relating to each item within the inventory...
This way you only cache data that you will be requiring on most pages to display and you will be minimizing the amount of DB calls through AJAX as it would be able to retrieve most of the data from the session requiring alot less joins and lookups, and ultimatly less file including on the AJAX files because you only need to know the item type thats in the session, you dont have to include any DB related classes or anything else that could slow it down.
Now this all sounds great but then there are 100 characters with the same item, and all of them are in there inventories clicking away to view more information on the items they have. If everyone clicked on one item using this caching method will save 100 queries and speed up the php execution time as its only including a minimal amount of includes in the file.
However each persons session will now be bloated as they are no longer going to be storing purely character - inventory information such as id, name, image, amount. They will also be storing lots of generic string data and other information which they will only need when they click on it.
This extra 10-20kb on the session per person will minimize LOADS of database queries, as if each person of the 100 clicked on each of their 10 unique items once thats 1000 queries that have been saved at the cost of 10-20kb on the webserver.
This is where my question would come in, would it be even better to cache the generic data that bloats the session within a text file on the server, so each time they click on the item it will still be saving the queries, however it will instead be having to do a file_exists/file_get_contents/unserialise call every time the item is clicked. Although this is taking 20kb per person off the session sizes... so given the example above this would be 2Mb saved off the session size with the 100 people.
So basically which would you guys say is the best implementation:
A) Querying the database each time the user clicks on the item creating 1000 queries over the space of a few seconds.
😎 Caching all the data that would be needed per click, so it saves 1000 queries but uses up an extra 2Mb in session variables to compensate.
C) Caching the generic data in local files, which will save 1000 queries and will save the session 2Mb, however will use up about 200kb worth of local files to store the unique items and will then do 1000 file actions to get the data.
Please bear in mind that if you scaled each of these up to 200 people will 20 items you are then going to be dealing with alot larger load, and who knows how many times people will click on these things.
Making use of that query caching in MySQL would help in this example but it would still make use of 1000 queries to the server, just means it doesnt have to do as much work on each query.
I hope that is a decent example, i know its quite lengthy but i dont think its as simple as a yes or no answer and needs the explanation to get the decent answers...