PowerPages – High Performance Guidelines: Caching

How Can We Help?

PowerPages – High Performance Guidelines: Caching

< Back

Since Power Pages provide fine-grained control over PostBacks, data binding and caching, they can be leveraged to implement high performance pages.
The following article provides some guidelines and recommendations on how to achieve this.

This article addresses the topic of

  • Caching

See the links below for articles on other high performance topics:

Caching

Caching of data can greatly improve the performance of an application if you are dealing with data sets that are large, slow, or infrequently changing but frequently used. That said, it can, however, also be challenging managing the cache and ensuring it is updated as expected. Caching also places additional resource requirements on the server as it will require more storage space (both memory and disk storage).

Cache duration

The cache duration is the time that a cached item will be stored before it is invalidated and set to be updated. Choosing a cache duration is specific to the particular problem you are trying to solve and could range from minutes to days, or weeks, depending on how often the underlying data source changes.

Cache invalidation

Cache invalidation is the process of deeming a cache entry as no longer relevant and fresh set of data should be retrieved. Cache entries are usually invalidated once they expire at the end of their configured duration, but there are also other ways that cache entries can be invalidated. Cache dependencies provide a way to make the validity of a cache entry depend on another value or data set. If the dependency changes, the cache item is invalidated.

Caching in ASP.Net Web Forms

Application Cache

ASP.Net Web Forms provides an application cache which is accessible via the Page.Cache object. This is a global application cache and so will survive across page requests and user sessions.

Each item added to the cache requires a unique key associated with it. This is the key you will need to use to retrieve the cached item later.

[C#] var key = "MyUniqueCacheKey"; var data = GetSomeData(); Cache.Insert(key, data);

 

Remember to choose a key that is unique enough to solve the problem at hand. For example, if you need to cache a particular user’s data, the key should include some identifier unique to that user (e.g. a unique User Id).

 

The Cache object supports various options for setting the duration the item should be stored as well as cache dependencies that help with cache invalidation.

Session State

It is possible to store values in the Session Sate (Page.Session) object, however, it is not recommended to be used as a cache store since:

  • Session State object does not support the expiration and dependency features that the Application Cache (Page.Cache) provides.
  • Extensive use of Session State to cache data can place a high load on server resources. The .Net garbage collector (GC) can clean up items from the Application Cache in order to free up memory, whereas items in Session State will remain in memory until the Session ends and is disposed.
  • Items in Session State cannot be reused across different user sessions, providing limited improvement in performance

SqlDataSource Cache

The SqlDataSource control provides support for caching and can be configured as follows:

[C#] mySqlDataSource.EnableCaching = true; mySqlDataSource.CacheDuration = 900; //15 minutes x 60 sec/min = 900 sec mySqlDataSource.CacheKeyDependency = "SomeUniqueIdentifier";

This will then automatically cache the result of the Select statement, and store it for the configured duration.

The SqlDataSource control also allows you to define a CacheKeyDependency value which identifies all cache entries created by the control. You can then invalidate all these entries by invalidating the CacheKeyDependency key.

[C#] var key = "SomeUniqueIdentifier"; ///Updates the CacheKeyDependency item in the Page.Cache object, invalidating any cache entries dependent on the CacheKeyDependency item Cache.Insert(key, DateTime.UtcNow.Ticks);

Output Caching

Another option is to cache the result of some expensive or long-running process.

ASP.Net Web Forms OutputCache

ASP.Net Web Forms provides built-in output cache support, which will cache the final output of a page. It can be enabled in a page by placing an <%@ OutputCache %> directive below the <%@ Page %> directive in the markup (.aspx).

[XHTML] <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ OutputCache duration="10" varybyparam="p" %>

The OutputCache supports options for varying the cache by query parameter, control, header, or a custom value in order to provide a more flexible caching solution.

Output cache is recommended for pages when relatively static content needs to be served in high volume. Interactive pages where users may see various different result sets is not likely to benefit from page output caching much.

 

Custom Output Caching

Expensive data processing routines can also be cached and stored so that their result can be retrieved without having to run the expensive routine every time. For example, an long running database query can be stored in another table in the database and then the static result can be retrieved when it is needed.

This approach will require that you manage the cache yourself by storing a timestamp of when the cached was created and also evaluating when it should expire. Alternatively, you could periodically run the processing (e.g. during out of office times) and then only ever retrieve the latest cached result, simplifying the management of the cache.

 

Comments are closed.

This is the legacy version of the XMPro Documentation site. For the latest XMPro documentation, please visit documentation.xmpro.com

X