views:

58

answers:

5

Let me start by explaining my scenario:

I am developing a quiz engine in C# Asp.net. There is an option to display one question at a time. Since there will be a lot of users accessing the quiz, I do not want to make database calls for each question. Ideally I want to load all the questions on page load, and somehow display one question at a time.

Operations to be done for each question:

  1. The question controls will be generated dynamically depending on question type.

  2. After each question I need to save the user response.

What is the ideal way to do this by conserving database calls? I would really like some directions. Thanks in advance.

+1  A: 

I the set of questions is same for all users then you may consider using application wide caching of questions and responses. For this you may get all the questions in one go or fetch questions first time and then cache it.

For per user stuff like saving responses to questions, you can use session object. As you have the questions and their related options in application-wide cache, you can only save question key and selected option in session object.

TheVillageIdiot
yes, the questions are same for all users, except there will be randomization of questions. Thanks, I have never used caching. Need to check it out.
beginner
A doubt, when I said questions are same, it depends on the quiz. Questions obviuosly will be different for different quiz. And users will be concurrently selecting different quiz. Is caching a good idea still?
beginner
A: 

You could store the questions for a user in a session variable, but you should consider how that affects memory usage. By storing a lot of data for each user, the memory available will limit the number of users that your application can handle.

By getting each question from the database, each page request will take slightly longer to handle, but you will shift the bottle neck from how many users you can handle to how many pages per second you can handle. This is also a softer limit, i.e. the server will just take longer to respond when you are at capacity, instead of starting to throw away user sessions because the memory is running out.

Guffa
What's the downvote for? If you don't explain what you think is wrong, it's rather pointless...
Guffa
A: 

should be relatively easy to achieve using JavaScript. You simply grab all question store them in a local hashtable (simple javascript object) and display then however you like.

There is a jQuery Quiz plug in which you might use/reuse or just look and learn some tips and tricks (see this page as well)

Tzury Bar Yochay
+2  A: 

Unless you are expecting transaction rates in the high hundreds per second I would not bother.

Premature optimisation will cause real problems and only solve imaginary ones.

Modern DBMSes have very efficient caching mechanisims of thier own, so why re-invent the wheel? Keep your app simple and to the point, lets the DBMS handle caching and solve any performance problems when you actually have them.

James Anderson
This is what I would have answered - you're getting ahead of yourself as the frequency of queries (unless the questions are very trivial) per user will be fairly low. Once you have the system working ASP.NET and SQL Server between them offer various opportunities for cacheing
Murph
A: 

Ideally I want to load all the questions on page load, and somehow display one question at a time.

That implies caching of some kind. Don't use session state, because you can't share it from one user to another.

You probably want to use the Cache object. For example, something like (from your code behind):

DataSet quiz;
string quizName = "quiz#1";
lock (quizLock)
{
    quiz = (DataSet)this.Cache[quizName];
    if (quiz == null)
    {
        quiz = DAL.ReadQuiz(quizName);
        this.Cache.Insert(quizName, quiz);
    }
}
RickNZ
Thanks. Is using an Object Datasourse and paging(maybe 1 row at a time) a good idea? I could iterate through the rows and create dynamic controls and also have events when I click Next question to save the answers.
beginner
That sounds like it might be overkill. You could just cache a DataTable (or DataSet), and then use a DataView to select the row you want to display, and databind that DataView to your display control.
RickNZ