views:

37

answers:

3

I am developing a website for a client (ASP.NET, T-SQL). It is a data-entry website allowing many of their users to login and manipulate records in the same database.

There are instructions (basically a list of string) throughout the form, telling the users what to do for each section; these instructions are themselves present in the database.

On each login, I store these instructions in the Session[] object per authenticated user. The instructions are identical for everyone.

I've looked at a solution which suggested storing a common session identifier in the database and then querying it to re-use that particular session but this seems very hacky. What is a best-practices solution to accomplish this? Is there a 'common' object available to all users?

+1  A: 

Sounds to me like you're looking for the Application Cache. Like the Session, it is an in-memory cache of data. Unlike the session, it is shared among all users; each user doesn't get their own individual copy of the data. Also, when you add data elements to the cache, you can specify criteria which will automatically invalidate that data, and cause it to be reloaded/refreshed (useful when your seldom-changing data actually does change :).

Here's some articles which should give you everything you need to know about using the Application cache (and some other caching options within ASP.NET as well):

mikemanne
Thank you for your answer, but I could only accept one :]
Alex
+3  A: 

Firstly, does it matter at this point? Yes, it's bad practice and inefficent, but if you're storing 20Kb of strings in memory and have a maximum of 100 users, that's 2,000Kb of data. Hardly a lot of memory "wasted". Even at 200Kb of strings, that's 20,000Kb of data. Again, not a lot. Is it worth your time, and the client waiting for you to solve it, right now?

If you decide it is then you could:

  1. Store the strings in the Application object or a static class so that they're retrieved once and used many times.
  2. Retrieve the strings on every page view. This may not be as performance damaging as it seems.
  3. Use something like the Cache class in System.Web.Caching.
  4. Make use of Output Caching.
  5. Make use of Windows Server AppFabric "Velocity" memory cache.
Rob
+1  A: 

I would suggest using the application-level Cache object. It is available everywhere as part of HttpContext. You can populate it on App_Start.

You can put any kind of object into Cache, though obviously, the smaller the better.

Here are some examples of how to populate it using C#:

1) Add items to the cache as you would add items to a dictionary by specifying the item's key & value. Example: add the current Value property of a text box to the cache.

Cache["txt1"] = txtName.value;

or

Cache["result"] = dataset;

2) The Insert method is overloaded, allowing you to define values for the parameters of the version you're using. Example: add only an item key & value:

Cache.Insert("MyData1", connectionString);

3) The Add method has the same signature as the Insert method, but it returns an object representing the item you added.

Cache.Add("MyData1", connectionString);

To retrieve the from cache:

stringName = Cache["MyData"];

If the cached data is not a string, you may need to cast it to the proper data type.

result = (DataSet)Cache["result"];

One of the benefits of using the Cache object as opposed to the Application object is that the CLR will dump contents of Cache if the system is in danger of running out of memory.

DOK
Thank you for your answer, but I could only accept one :]
Alex