-
-
Create an ASP.NET Application inside Visual Studio.NET by the name DataCachingUsingNCache.
-
Add a new Web Form named DataCacheSample.aspx to your project and include the following namespaces.
using Alachisoft.NCache.Web.Caching;
using Alachisoft.NCache.Runtime;
using Alachisoft.NCache.Runtime.Dependencies;
-
Add a nested class Product inside the code behind.
[Serializable]
class Product
{
.....
}
-
Declare a private variable "_cache" in the code behind of the DataCacheSample as.
private Cache _cache;
-
Add the following code in Page_Load event of the DataCacheSample as.
protected void Page_Load( object sender, EventArgs e)
{
if (_cache == null ) { _cache = NCache.InitializeCache( "mycache" ); }
}
-
On the Design view tab for the Web Form, add a Web Form button to the DataCacheSample page. Change the ID property of this button to FetchFromDB, and then change the Text property to Fetch From Database.
-
Add a second Web Form button. Change the ID property to FetchFromCache, and then change the Text property to Fetch From Cache.
-
Add a Web Form label. Change the ID property to Status, and then clear the Text property.
-
On the Design view tab, double-click the FetchFromDB button to open the FetchFromDB_Click event in the code-behind page.
-
Add the following code to the FetchFromDB_Click event:
protected void FetchFromDB_Click(object sender, System.EventArgs e)
{
string connectionString = "Data Source=local host; initial catalog=northwind;User ID = sa; password =;";
string QueryString = "SELECT ProductID, ProductName, SupplierID, CategoryID, UnitPrice FROM dbo.Products where productid < 11";
DataSet ds = new DataSet ();
//Create the connection and pass in the ConnectionString
SqlConnection mySqlConn = new SqlConnection(connectionString);
SqlCommand sqlCmd = new SqlCommand(QueryString, mySqlConn);
mySqlConn.Open();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();
List <Product> products = new List <Product>();
while (sqlReader.Read())
{
Product product = new Product();
product.ProductID = sqlReader.GetInt32(0);
product.ProductName = sqlReader.GetString(1);
product.SupplierID = sqlReader.GetInt32(2);
product.CategoryID = sqlReader.GetInt32(3);
product.UnitPrice = sqlReader.GetDecimal(4);
DBCacheDependency dependency = DBDependencyFactory.CreateSqlCacheDependency(connectionString,"Primary Key:dbo.Products");
_cache.Insert(product.ProductID+":dbo.Products",product,dependency,
Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,Alachisoft.NCache.Runtime.CacheItemPriority.Default);
products.Add(product);
}
//Set the DataGrid's DataSource to the "products" list
DataGrid1.DataSource = products;
mySqlConn.Close();
//Display the status
Status.Text = products.Count+ " products fetched from database.";
DataGrid1.DataBind();
}
-
On the Design view tab for the DataCacheSample.aspx page, double-click the FetchFromCache button to open the FetchFromCache_Click event in the code-behind page.
-
Add the following code to the FetchFromCache_Click event:
protected void FetchFromCache_Click(object sender, EventArgs e)
{
List<Product> products = new List<Product>();
for (int i = 0; i < 11; i++)
{
object cachedProduct = _cache.Get(i + ":dbo.Products");
if(cachedProduct == null){ continue; }
Product product = cachedProduct as Product;
products.Add(product);
}
DataGrid1.DataSource = products;
DataGrid1.DataBind();
Status.Text = products.Count + " products fetched from cache.";
}
-
On the Build menu in the Visual Studio .NET IDE, click Build to build the project.
-
To run the code, right-click the DataCacheSample.aspx page in Solution Explorer, and then click View in Browser.
-
Click the FetchFromDB button; the Status label should display "10 products fetched from database." and the DataGrid control is populated. All products are stored in cache.
-
Click the FetchFromCache button; the Status label should display "10 products fetched from cache." and the DataGrid control is populated.
-
Modify one of the displayed records in the Northwind database by using SQL Query Analyzer or another tool. After you modify the record, cached data for that row is invalidated.
-
Click the FetchFromCache button again. The Status label will display the message "9 products fetched from cache." and 9 products are populated. The modified record in database was removed from cache.