Site sponsored by: Idera Try Idera’s new SQL admin toolset
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Capture DDL Changes using Change Data Capture with SQL Server 2008 ...
Business Intelligence in Collaborative Planning, Forecasting and Replenishment
Inside SQL Server Cluster Setup and Troubleshooting Techniques - Part I ...
Configure and Manage Policy Based Management in SQL Server 2008 ...

More     
 
Latest FAQ's

Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running
How to Display Server Name or IP Address in a Reporting ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> asp.net / ado.net >> Caching in ASP.NET (Part II) ...

Caching in ASP.NET (Part II)

By : Joydip Kanjilal
Feb 28, 2007

Page 2 / 3


Cache Dependency and Cache Expiration Strategies

Cache dependency implies a logical dependency between the data in the Cache and physical data storage. Whenever the data in the physical storage changes, the dependency is broken, the cache is invalidated and the cached item is removed. This physical storage can be an XML file, a database, a flat file, etc.

The following is the complete syntax of using the CacheDependency class.

CacheDependency cacheDependency = new CacheDependency(fileObj, dateTimeObj);
Cache.Insert(key, value, cacheDependency);

As an example, you can set Cache dependency to an external Xml file as shown below:--

Cache.Insert("Country", objDataSet, new CacheDependency(Server.MapPath("Country.xml")));

You can also set a Cache dependency to a file in a remote system as shown below:--

CacheDependency objCacheDependency = new
CacheDependency(@"\\192.168.0.9\joydipk\Test.txt");

Similarly, you can also set Cache dependency on an entire folder. When the contents of the folder changes it will call an event handler for which you need to write the necessary code. In the earlier version of ASP.NET, the CacheDependency class was sealed. Hence you could not subclass this class to create your own Custom Cache dependencies. Things have now changed with ASP.NET 2.0 and the class is no longer sealed so you can create Custom Cache Dependencies by extending this class.

Cache Expiration relates to the removal of data in the cache after the durability of the cached item in the cache has expired. Cache expirations strategies can be Time Based, File Based or Key Based. The following sections discuss each of these strategies in detail with code examples.
 

Time Based Expiration

This is implemented by specifying a specific duration for which a cached item should remain in the cache. When the time elapses, the item is removed from the cache and subsequent requests to retrieve the item returns a null. This is used to specify a specific period of time for which the page would remain in the cache. The following statement specifies such expiration for a page in the code behind of a file using C# using the Cache API.

Response.Cache.SetExpires(DateTime.Now.AddSeconds(45));

This can also be accomplished by specifying the following in the page output cache directive as shown here.

<%@OutputCache Duration="45"
VaryByParam="None" %>

Time based expiration strategies can in turn be of the following two types:

        Absolute
        Sliding

In Absolute Expiration, the cache expires at a fixed time/date. For Sliding Expiration the cache duration is increased by the specified sliding expiration value each time the page is requested. Thus a heavily requested page will remain cached, whereas a less requested page will not be cached. Sliding Expiration is therefore useful in ensuring that the valuable resources allocated to the cache are allocated to the most heavily requested pages/items.

 The following is an example of Absolute Expiration:

Cache.Insert("Country", ds, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration);

The cache is set to expire exactly two minutes after the user has retrieved the data.

The following is an example of Sliding Expiration:

Cache.Insert("Country", ds, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1));

Note that you cannot use both Absolute and Sliding expirations at the same time.


<< Prev Page     Next Page>>    








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved