Code vs Database - Sequence of Steps, Call Database or Check Session Object?
I have several web sequential pages which will modify a record and its child records in th开发者_如何转开发e database, called a "project". Such a project is currently passed between pages using its database ID in the URL parameters.
A project has some information specific to itself, and also consists of one or more Tasks, which each have information specific to itself.
Is it faster (alternatively, more maintainable or more easily understood) to hit the database each time I need to query the same project (and its tasks), or should I query the database once (either once for each page or once for all pages and save to Session) and check the saved object rather than the database?
Note that I am asking about checking data, not saving it (I will still hit the database each time I need to save something, obviously).
Put the database ID in the session and get the rest from the database. Is the database and the web server on the same hardware, if so that database will cache those results anyway, and it should return that data fairly fast?
I assume your taking about a simple site, if your talking about twitter/facebook scale, you've got a lot more to worry about then where to store your data.
Two ideas:
- As @ronaldwidha said, use more Ajax. This will allow you to hit that database for your data, while not looking like you are :)
- Test it and try it. Try it out, hit the database for project details 1000 times, and get your stopwatch out.
Often times questions like this depend on so many variables it's hard to give a straight answer. If you've got a lot of data, a project with 10,000 tasks, well that's going to be a bad idea to store in the session. If you're just asking about storing database ID and a project name, well then I'd say store it in the session.
It seems what you are describing is a good old caching mecahnism.
Personal "Cache"
You have pointed out that Session could be one of that 'caching' location - which implies the project information is only specific to the current user.
Global Cache
If it was relevant to other users (no complicated requirement around permissions on who can see which tasks within a project), you could potentially store it in a global caching mechanism where read is much faster than reading from db. It will be even more useful when the number of projects are fairly low - which will substantially reduce the number of cache miss hits.
Populate the cache if the project/task wasn't found on the first call.
Don't forget to invalidate the cache with a suitable trigger from the database or other expiration rules.
Ajax
If alot of the information are only valid as long as the wizard step, why not just skip postbacks altogether? Keep the information that you want to continuously show throughout the wizard on screen, and only update part of the page that you want to update by the use of javascript visible=false, true, or get the new bits of content using ajax.
精彩评论