What are the 3 different ways to store data in the Cache object?

 Posted by deccansoft on 10/30/2010 | Category: ASP.NET Interview questions | Views: 2935

Use assignment. Assigning a value to an unused key in the Cache object automatically creates that key and assigns the value to that key. Assigning a value to a key that already exists replaces the cached value with the assigned value.
Use the Insert method. The Insert method uses parameters rather than assignment to create or change cached data. Insert optionally accepts parameters to establish dependencies and set expiration policy.
Use the Add method. The Add method is similar to Insert; however, it requires all parameters and returns an object reference to the cached data. For example, the following Cache statements all add the same item to the cache: using System.Web.Caching; private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { Cache["NewItem"] = "Some string data"; Cache.Add("NewItem", "Some string data", null, Cache.NoAbsoluteExpiration, System.TimeSpan.FromMinutes(1), CacheItemPriority.Default, null); Cache.Insert("NewItem", "Some string data"); } }

Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response