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

Web Developer Tip: A Better Way to Handle Caching

Brad McDavid
#Episerver, #Ektron, #Code
Published on June 11, 2014
warren-wong-323107-unsplash-1

Caching data is a useful technique to improve website performance, but it can be prone to error. Learn how we've simplified and improved the process.

At Diagram, we work on programming websites every day, so certain functions and techniques become ingrained. We might create code that performs certain operations so often that we don’t consider whether there might be a better way to handle them. That’s why it’s a good idea to take a step back and rethink the way we perform certain processes and see if we can streamline them and make them work better. A great example of this is caching.

What is Caching?

A website will regularly need to access its database in order to display the data on its pages, but if it is constantly retrieving the data from the database server to the web server, the site’s performance will suffer. Caching stores the necessary data in the web server’s memory, eliminating the need for constant communication with the database server and improving performance.

How is Caching Normally Handled?

When a site needs to get data from the database (for example, on a page which displays the most recent news items), a typical caching process will perform a check to see if the data currently exists in the web server’s memory. If the data does not exist on the web server, the site will pull the data from the database server and insert it into the web server’s memory. Typically, a developer will need to write a snippet of code for each page that needs to perform this check and copy data from the database, similar to the following example:

string cacheKey = "MyCachedData";
string data = System.Web.HttpRuntime.Cache[cacheKey] as string;

if(string.IsNullOrEmpty(data))
{
data = "Some data that typically comes from the data layer";

if (data != null)
System.Web.HttpRuntime.Cache.Insert(
cacheKey,
data,
null,
DateTime.UtcNow.AddSeconds(300),
System.Web.Caching.Cache.NoSlidingExpiration
);
}

Recreating this process of checking the web server memory and inserting data into the memory for each page that needs to access the database is not only an unnecessary replication of labor, it can be prone to error.

Is There a Better Way to Handle Caching?

We’ve found a way to simplify the process of caching by creating a standard caching function that can be called every time a page needs to copy data from the database. This function provides a standard method of reading and writing to server memory, allowing developers to focus on the information that needs to be stored. Using this method, developers simply need to define what data needs to be accessed, and the caching function takes care of reading and writing to the server’s memory. Here is an example of code that calls this function:

public string NewCache()
{
return Cache.CacheItem<string>("MyCachedData", CachingFunction, 300);
}

/// <summary>
/// This function builds the string to be cached.
/// </summary>
/// <returns></returns>
public string CachingFunction()
{
return "Some data that comes from the database.";
}

With this standard in place, developers are able to simplify the process of caching data, and the possibility of code errors when accessing the database is reduced. This also allows for global changes to be made to the process of accessing the database and reading and writing to server memory, rather than having to update each snippet of code that uses caching. It also allows for helpful development functionality such as creating a flag which can turn caching on or off on a site-wide basis, which is helpful when building a site, since developers will always want to work with the latest data.

Utilizing this caching method is a great way of reducing developers’ efforts and improving their productivity, and it also provides a standard for accessing the database and reading and writing to server memory across the entire site. This is a great example of what we can do to improve our development process when we take a step back and look at how we can standardize certain functions that we perform regularly. If you would like to utilize this method of caching, you can download a sample Visual Studio project to get started. Do you have any questions about how you can implement this caching function? Do you have any tips of your own on caching or other programming functions that can be standardized? Please feel free to leave a comment below, or contact us to speak to a developer.