2

I am trying to implement caching in my program. I have a web application that provides user access to some learning courses.When the user logs on I authenticate him from our database. Now, if access a different course from the list I don't want to go back to the database. Instead I want to store the value in cache for lets say 20 mins. The program should 1) Authenticate the user, and let him access the course. If he changes the course the program should look in cache. if the user id exists let him access the course without going back to server. 2) Provide a mechanism, that if after 20 minutes the user is still on the system, automatically update the time.so, that the user is not logged off. I tried something like this

Cache ch = new Cache();
ch.Add("key","value);

but I get null refernece exception in 2nd statement. I have never worked with caching before so will appreciate any pointers.

Abhi.Net
  • 722
  • 3
  • 11
  • 37
  • possible duplicate of [ASP.Net Caching](http://stackoverflow.com/questions/1130762/asp-net-caching) –  May 31 '12 at 05:41

4 Answers4

2

Well this is how I use it:

/// <summary>
/// Get Data
/// </summary>
/// <returns>data</returns>
public static ElementOut GetData()
{
    //function return value
    ElementOut functionReturnValue = new ElementOut();

    try
    {
        string key = "data";
        if (HttpRuntime.Cache[key] == null)
        {
            //fetch data
            var data = CreateBLLAdapter().GetData();

            //validate data
            if (data == null) throw new Exception("data is empty!");

            //set cache to 12 hours
            DateTime timeToExpire = DateTime.Now.AddHours(12);

            //set cache
            HttpRuntime.Cache.Insert(key, data, null, timeToExpire, Cache.NoSlidingExpiration);
        }

        //set return from cache
        functionReturnValue = (ElementOut)HttpRuntime.Cache[key];

    }
    catch (Exception ex)
    {
        //Log.WriteServerErrorLog(ex);
    }

    return functionReturnValue;
}
Rumplin
  • 2,703
  • 21
  • 45
1

Use Cache of the current HTTP context:

HttpContext.Current.Cache

Description: When a new HTTP request comes, application processes that request in a pipeline commonly known as Request Process Pipeline, that is, it does some actions on the request one after the other, before giving the request to your handler to process it.

One of the actions that ASP.NET performs, is to load session, and cache data, based on some parameters.

HttpContext.Current.Cache is the loaded cache and you can use it. Then when you store something in that cache, ASP.NET again takes the responsibility of storing that cache and saving it on the next HTTP request.

You can also get access to that global object using Cache property of your pages.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
0

If You develop in ASP.NET, You can simply use ASP.NET session store, which stores values for each user, as long as his session lasts (which is by default 20 minutes from his last action)

YavgenyP
  • 2,113
  • 14
  • 11
0

You may try output cache.

Please see the link below for more details. http://msdn.microsoft.com/en-us/library/ms178597.aspx

Pinoy2015
  • 1,429
  • 15
  • 29