I have a search page that allows the users to filter other users by various categories (age, gender, country, region, ethnicity, etc). The first time the search function is called it gets all the possible values for each category from the database. Just wondering though...
Is it better to POST this data back each time the page is re-loaded (in a serialized h开发者_如何学Cidden form field) or should I retrieve this information from the database every time the page is loaded.
If i understand the situation correctly, the serialized POST option would increase the sites bandwidth consumption as the data is traveling from the users machine, to the server and back to the users machine again. On the other hand, querying the database everytime would increase the load on the server/database, but would only use half the bandwidth.
If thats the case, what option should I go for... I know that it probably doesn't make much difference for the amount of traffic i'll be getting, but I want to learn the best methods of doing this sort of stuff :P
Generally, I would always keep things under your own control. Which means keeping it between your server and your database.
You have no control over the speed of a user's internet connection, or network latency etc.
Also, if you want to keep things between requests, have you thought about using sessions or cache? A cache, such as APC would allow you to store the data on the server for the next request, so it needs neither Database or bandwidth.
I think, "POST back" approach is generally not good:
This may lead to security issues
Usually connection between database and web-server has higher bandwidth, then connection between client and web-server
I'd recommend either to use data caching on web-server side (see other answers for links), or to implement client-side data filtering / sorting - there must be ready-to-use javascript libs for that.
Facebook and many other companies employ memcache to store the search results and query this cache so as to minimize requests to the database.
You can store the search results of your first search in memcache and since this is the superset of the data subsequently filtered through using the various parameters, all subsequent requests will be made against the cache as opposed to the database.
By serializing your result and sending them back via POST you have to "print" them into your form somehow, which means it can be tempered with, no it's definitely not a viable solution.
Caching your results in memcache, APC, etc. could be a great alternative. If you don't want to or cannot one of these solutions, there's already the database engine cache that will take care of most of the overhead for you.
One last thing, a search page should use a GET method according to HTTP/REST specifications: POST must be used for inserting, while GET must be used for retrieving.
精彩评论