ASP.NET Caching Basics

ASP.NET Caching Basics


1st method  on top of aspx page

< %@ OutputCache Duration="10" VaryByParam = "None"  %>  cache the page . Use substitution control to work with it for performing caching with other web control.

2nd method

Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick




If Cache("vin") Is Nothing Then


Cache.Insert("vin", "Vinod Kotiya " & Now.ToString, Nothing, DateTime.Now.AddSeconds(10.0), TimeSpan.Zero)


Response.Write("creating cache")


Else


Response.Write(Cache("vin").ToString)


End If


End Sub



The Add and Insert methods have the same signature, but there are subtle differences between them. First, calling the Add method returns an object that represents the cached item, while calling Insert does not. Second, their behavior is different if you call these methods and add an item to the Cache that is already stored there. The Insert method replaces the item, while the Add method fails.





Absolute Expiration: This example will cache the time sensitive data for one minute, at which point the cache will expire. Note that absolute expiration and sliding expiration (below) cannot be used together.

Sliding Expiration: This example will cache some frequently used data. The data will remain in the cache until one minute passes without anything referencing it. Note that sliding expiration and absolute expiration cannot be used together.

### Caching sqldatasource

EnableCaching="True" CacheDuration="300">< / asp:SqlDataSource >

Comments