How to implement caching?

Answers were Sorted based on User's Feedback



How to implement caching?..

Answer / darshan

Basically cache is used to store frequently accessed data
into the memory, for faster retrieval.

Cache object is dictionary, it stores data in key=>value
pair combination. There are two ways to store the data into
the cache.

1. Cache.Add(key, value, dependance, absolute expiration,
sliding expiration, priority, onRemoveCallback);
2. Cache["key"] = value;

Note: If you are not working in web project, then you might
need to create instance of cache class, like following.

Cache che = new Cache();
che.Add(....);
che["key"] = value;
-----------------------------------------------------------------------
Example:
using System;
using System.Web.Caching;

namespace cacheExample
{
public class Example
{
public static void Main()
{
Cache ch = new Cache();
ch["name"] = "Anonymous";
console.WriteLine("Hello your name is: {0}",
ch["name"].ToString();
}
}
}

Is This Answer Correct ?    1 Yes 0 No

How to implement caching?..

Answer / sivasaravanan

Data caching:

DataView Source;
//Declare variable for caching
Source = (DataView)Cache["MyDataset"];
if (Source == null)
{
OleDbConnection objcon = new OleDbConnection
(ConfigurationManager.ConnectionStrings
["strcon"].ConnectionString);
OleDbDataAdapter objcmd = new OleDbDataAdapter
("select * from Customer", objcon);
DataSet objds = new DataSet();
objcmd.Fill(objds, "Customer");
Source = new DataView(objds.Tables["Customer"]);
Cache["MyDataset"] = Source;
Label1.Text = "Dataset created explicitly";


}
else
{
Label1.Text = "Dataset retrived from Cache";
}
GridView1.DataSource = Source;
GridView1.DataBind();
gridbind();

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More ASP.NET Interview Questions

What is the postback property in asp.net?

0 Answers  


Write a code for sending an email from asp.net application.

0 Answers  


How can you get public key information?

1 Answers   Microsoft,


How Clustered Index and Non clustered index stored on SQL server?

2 Answers   TCS,


Can you change a master page dynamically at runtime? How?

0 Answers  






What are the advantages of passport authentication?

0 Answers  


Differentiate the session object and application object?

0 Answers  


• What are HttpHandlers? • What are HttpModules?

2 Answers  


What is the purpose of master page?

0 Answers  


We Only Know The Total Number Of Feet In The Farmyard. Write A Program that will compute the total number of rabbits and chickens in the farmyard. Assume number of feet in the farmyard are 40. how many rabbits and chickens are?

0 Answers  


Where code pages are used?

0 Answers   Blue Star,


What do you understand from custom control?

0 Answers   C DAC, CDAC,


Categories