Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
Enable ASP.NET polling using “web.config” file
Enable ASP.NET polling using “web.config” file
✍: Guest
Now that all our database side is configured in order to get the SQL Cache working in the
ASP.NET side we need to do some configuration in the web.config file.
We need to set two attributes in the gweb.configh file:-
ã Set gEnabledh attribute to true to set the caching on.
ã Set the poll time attribute to the number of milliseconds between each poll
Below is the snapshot of the web.config file.
Finally use the Cache dependency object in your ASP.NET code
Now comes the final step to use our cache dependency with programmatic data caching,
a data source control, and output caching.
For programmatic data caching, we need to create a new SqlCacheDependency and supply
that to the Cache.Insert() method. In the SqlCacheDependency constructor, you supply
two strings. The first is the name of the database you defined in the element in the section
of the web.config file e.g: Northwind. The second is the name of the linked table e.g:
Products.
private static void CacheProductsList(Listproducts) {SqlCacheDependency sqlDependency = new SqlCacheDependency("Northwind", "Products"); HttpContext.Current.Cache.Insert("ProductsList", products, sqlDependency, DateTime.Now.AddDays(1), Cache.NoSlidingExpiration);} private static List GetCachedProductList() {return HttpContext.Current.Cache["ProductsList"] as List ;}
ClsProductItem is business class, and here we are trying to cache a list of ClsProductItem
instead of DataSet or DataTable.
The following method is used by an ObjectDataSource Control to retrieve List of Products
public static List<ClsProductItem> GetProductsList(int catId, string sortBy) { //Try to Get Products List from the Cache List<ClsProductItem> products = GetCachedProductList(); if (products == null) { //Products List not in the cache, so query the Database layer ClsProductsDB db = new ClsProductsDB(_connectionString); DbDataReader reader = null; products = new List<ClsProductItem>(80); if (catId > 0) { //Return Product List from the Data Layer reader = db.GetProductsList(catId); } else { //Return Product List from the Data Layer reader = db.GetProductsList(); } //Create List of Products -List if ClsProductItemproducts = BuildProductsList(reader); reader.Close(); //Add entry to products list in the Cache CacheProductsList(products); } products.Sort(new ClsProductItemComparer(sortBy)); if (sortBy.Contains("DESC")) products.Reverse(); return products; }
To perform the same trick with output caching, you simply need to set the SqlDependency
property with the database dependency name and the table name, separated by a colon:
<%@ OutputCache Duration="600" SqlDependency="Northwind:Products"
VaryByParam="none" %>
The same technique works with the SqlDataSource and ObjectDataSource controls:
<asp:SqlDataSource EnableCaching="True"
SqlCacheDependency="Northwind:Products" ... />
Note :- ObjectDataSource doesn't support built in caching for Custom types such as the one
in our example. It only supports this feature for DataSets and DataTables.
Just to make a sample check run the SQL Server profiler and see that does the SQL
actually hit the database after the first run.
2007-10-23, 7564👍, 0💬
Popular Posts:
What is application domain? Explain. An application domain is the CLR equivalent of an operation sys...
What’s the difference between Unit testing, Assembly testing and Regression testing? Unit testing is...
Assuming that the structure of a table shows two columns like this: --------+------------+-- ----+---...
What Is URI? URI (Uniform Resource Identifier) is a superset of URL. URI provides a simple and exten...
What are unadjusted function points and how is it calculated? Unadjusted function points = ILF + EIF...