try...catch...finally...bloggg....: Caching Basics in .NET

try...catch...finally...bloggg....

Wednesday, June 23, 2004

Caching Basics in .NET

Caching in .NET

Proactive Loading - Load at the time of application startup
Reactive Loading - Load when the data is accessed first

Caching addresses Performance, scalability and Availability.

When considering data to be cached think of these two parameters
1. Likelihood of changes.
2. Relevance of changes.

ASP.NET Caching
Cache is related with the Application object. Cache is recreated everytime the application restarts. The main difference between them is the dependencies and the expiration policy support in the Cache Object.

Dependencies provided are
1. File Based Dependency.
2. Key Based Dependency.
3. Time Based Expiration. - Absolute Expiration, Sliding Expiration.

Cache Callback can be used to reinsert the same item or get an update of the removed item or do nothing.

Page output caching, Page fragment caching.
VaryByParam
VaryByHeader
VaryByCustom
VaryByControl

Cache can be accessed outside the context of the request using
HttpRuntime.Cache.

Remoting Singleton Caching
This stores the cache information between different client invokations. you need to make sure that the remote cache object is not garbage collected. This method has poor performance and
scalability.

Memory Mapped File Caching
Memory-mapped file caches are not easy to implement because they require the use of complex Win32® application programming interface (API) calls. The .NET Framework does not support memory-mapped files, so any implementations of a memory-mapped file cache run as unmanaged code and do not benefit from any .NET Framework features, including memory management features, such as garbage collection, and security features, such as code access security

SQL server caching
SQL Server caching is easy to implement by using ADO.NET and the .NET Framework, and it provides a common development model to use with your existing data access components. It provides a robust security model that includes user authentication and authorization and can easily be configured to work across a Web farm using SQL Server replication.
Because the cache service needs to access SQL Server over a network and the data is retrieved using database queries, the data access is relatively slow. Carefully compare the cost of recreating the data versus retrieving it from the database.

Client side caching
1. HiddenFields
2. ViewState
3. Cookies.
4. QueryString
5. Hidden Frames.

Implement thread safety in when using cache using the ReaderWriterLock.

Normalizing cached data means storing the data in the format
in which the clients use it. This avoids repeated formatting and
serialization/deserialization calls.


·