<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1639164799743833&amp;ev=PageView&amp;noscript=1">
Diagram Views

Using Caching Options to Ensure the Best Website Performance

Cory Starkovich Sr. Web Developer
#Code
Published on May 22, 2015
warren-wong-323107-unsplash-1

A Diagram developer shares the options that can be used with caching website data to ensure that a site runs smoothly and correctly.

At Diagram, our developers spend a lot of time working directly with the underlying code of the websites we create. This level of immersion gives us the experience and expertise we need to build sites that meet our clients’ needs while functioning at optimal performance levels. One way we can ensure that we’re getting the best performance out of a website is to use the correct caching options.

We’ve written previously about the standard caching function that our developers use, but today we’re going to look a little bit deeper into the different options that are available for caching and how developers can use them to handle when and how the data that is cached expires.

Expiration

When data is cached, it is stored in the web server’s memory, reducing the need for the site to query the database, which will result in improved performance. However, depending on how often the data in the database changes, developers will want to configure when the data that is stored in the cache expires. There are two ways to do so:

Absolute Expiration: This will cache data for a pre-determined amount of time. Developers can use a fitting time based on how often the data is expected to change. In this example, the data will expire 1 minute after it was saved in the cache:

Cache.Insert(cacheKey,cachedData,null,DateTime.Now.AddMinutes(1),
  System.Web.Caching.Cache.NoSlidingExpiration);

Sliding Expiration: This option will cache data for as long as it is being accessed. Every time the data is retrieved from the cache, the expiration timer is reset back to the time specified (the following example specifies the time as 1 minute), and the cache will expire when the timer ends. This should be used for data that is accessed frequently.

Cache.Insert(cacheKey, cachedData, null,
  System.Web.Caching.Cache.NoAbsoluteExpiration,TimeSpan.FromMinutes(1));

Priority

Setting a priority on a cached item allows developers to determine how important cached data is when space in memory needs to be cleared. Using the CacheItemPriority Enumeration, a hierarchy can be specified, meaning that if memory becomes limited, the system will clear out lower-priority cached information first. Data can also be set to be NonRemovable, which prevents the system from automatically removing the data.

Dependencies

Setting a dependency allows developers to clear the cache when the dependent source changes. This dependency can be set to monitor changes to a SQL database table, a file, or even another item in the cache. This can ensure that cached data will be cleared when it is outdated, rather than waiting for the specified expiration time.

Callback

Setting a callback on a cached item allows developers to execute extra code if/when the data expires. This callback can be used to alert the system and even recreate the cached data again behind the scenes.

By utilizing these options, developers have even more power over how, when, and how often data is cached, ensuring that the website uses the most up-to-date information while still providing the smoothest possible performance for the site’s users. Do you have any questions about how to implement caching and improve the performance of your website? Do you need help determining the best way to use these options in your code? Please contact us to speak to a Solutions Engineer, or feel free to share any other questions or comments below.