"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Showing posts with label Cache. Show all posts
Showing posts with label Cache. Show all posts

Sunday, January 27, 2013

Exploring more in cache in asp.net-- remove cache-- part 5

I covered a few topic about cache in asp.net except one how to remove a cache :)

HttpContext.Current.Cache.Remove("customers"); // customers my cache name


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

Saturday, January 26, 2013

Exploring more into caching in asp.net-- Data Caching (Part 4)


ASP.NET also supports caching of data as objects. We can store objects in memory and use them across various pages in our application. This feature is implemented using the Cache class. This cache has a lifetime equivalent to that of the application. Objects can be stored as name value pairs in the cache. A string value can be inserted into the cache as follows:


Cache["name"] = "Arun Aravind";

 The stored string value can be retrieved like this:


if (Cache["name"] != null)
            Label1.Text = Cache["name"].ToString();
 
To insert objects into the cache, the Add method or different versions of the Insert method of the Cache class can be used. These methods allow us to use the more powerful features provided by the Cache class.


protected void Page_Load(object sender, EventArgs e)
    {
        ArrayList datestamps;
        if (Cache["datestamps"] == null)
        {
            datestamps = new ArrayList();
            datestamps.Add(DateTime.Now);
            datestamps.Add(DateTime.Now);
            datestamps.Add(DateTime.Now);

            Cache.Add("datestamps", datestamps, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 0, 60), System.Web.Caching.CacheItemPriority.Default, null);
        }
        else
            datestamps = (ArrayList)Cache["datestamps"];

        foreach (DateTime dt in datestamps)
            Response.Write(dt.ToString() + "<br />");
    }


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

Friday, January 25, 2013

Exploring more into caching in asp.net-- Caching page fragments (Part 3)

Sometimes we might want to cache just portions of a page. For example, we might have a header for our page which will have the same content for all users. There might be some text/image in the header which might change everyday. In that case, we will want to cache this header for a duration of a day.

The solution is to put the header contents into a user control and then specify that the user control content should be cached. This technique is called fragment caching.

To specify that a user control should be cached, we use the @OutputCache directive just like we used it for the page.



<%@ OutputCache Duration=10 VaryByParam="None" %>

With the above directive, the user control content will be cached for the time specified by the Duration attribute [10 secs]. Regardless of the querystring parameters and browser type and/or version, the same cached output is served. 



If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

Thursday, January 24, 2013

Exploring more into caching in asp.net (Part 2)

 In order to cache a page's output, we need to specify an @OutputCache directive at the top of the page. The syntax is as shown below:



<%@ OutputCache Duration=5 VaryByParam="None" %>

Wednesday, January 23, 2013

I started loving Cache :) Part 1

Hi *.*,

When it comes to build a high-performance and scalable ASP.Net Web applications Caching is inevitable. It has the ability to store objects whether its data object or pages or controls or even parts of a page. The plan is to keep them in memory while initially requested. You can store them in web-server or in proxy-server or in browser, your choice. Using this feature prevents you recreating information while it already satisfied in previous request. The motive is reusing the objects when they are needed.

Basically ASP.Net provides two types of caching. Output caching and another one is traditional application data caching. There are few good practices to cache your data. When the data is going to be used more than once it’s a candidate for caching and if data is general rather than specific to a given request or user, it’s a great candidate for the cache. But the thing sometimes developers often overlook is that they can cache too much and it causes out of memory exception. Therefore, caching should be bounded. In my experience ASP.NET out-of-memory errors caused by overcaching, especially of large datasets.

So how do you use this? – If your components are running within an ASP.NET application, you simply need to include a reference to System.Web.dll in your application project. When you need access to the Cache, use the HttpRuntime.Cache property (the same object is also accessible through Page.Cache and HttpContext.Cache).

By default Location property sets to "Any" which means this stores the output cache in the client’s browser, on the proxy server (or any other server) that participates in the request, or on the server where the request is processed.

The choices you have are "Server", "Client", "Downstream" or "None".

And the programmatic approach for Output cache is

TimeSpan freshness = new TimeSpan(0,0,0,60);
DateTime now = DateTime.Now;
Response.Cache.SetExpires(now.Add(freshness));
Response.Cache.SetMaxAge(freshness);
Response.Cache.SetCacheability(HttpCacheability.Server);
Response.Cache.SetValidUntilExpires(true);

 Here is an example. Suppose your target is to fill a DataGrid, if the DataSet is already been stored you’ll ignore re-filling it, otherwise you’ll fill the DataSet and will cache it.



DataView Source;
        // Retrieve the DataView object from Cache. If not exist, then add DataView object to the Cache.
        Source = (DataView)Cache["MyDataSet"];
        if (Source == null)
        {
            SqlConnection myConnection = new SqlConnection("Server=ServerName; database=Pubs; user id=UID; password=PWD;");
            SqlDataAdapter myCommand = new SqlDataAdapter("select * from Authors", myConnection);

            DataSet ds = new DataSet();
            myCommand.Fill(ds, "Authors");

            Source = new DataView(ds.Tables["Authors"]);
            Cache["MyDataSet"] = Source;
            CacheMsg.Text = "Dataset created explicitly";
        }
        else
        {
            CacheMsg.Text = "Dataset retrieved from cache";
        }

        // Binding the DataView object with DataGrid.
        DataGrid1.DataSource = Source;
        DataGrid1.DataBind();

 Now u got y i titled this post so...
 

If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)