Caching is an essential part of modern web development. Developers must always find ways to optimize performance for web applications as this will improve application's performance. The following blog post will demonstrate how to cache items coming from 3-rd party services.

Real-world scenario:

Imagine that your agency needs to pull specific data from a 3rd party system as SalesForce and display it as a list on one of the website pages. Each time the page is requested, service call will be made to the SalesForce service to pull the data.

To improve the performance of the site, we need to cache the items retrieved by the SalesForce API request. Content editors with backend permissions will be able to go to the Basic settings and either clear the cache or modify the cache substitution time.

Solution

  • Create custom basic settings section

In this section you can configure the cache expiration time. For more information, see one of my previous blog posts "Create custom Sitefinity basic settings section"

  • Substitute cache via Sitefinity's CacheManager

1) Create an instance of the CacheManager:

private static ICacheManager CacheManager
    {
        get
        {
            return SystemManager.GetCacheManager(CacheManagerInstance.Global);
        }
    }

2) Substitute cache

 internal static IList<EventModel> GetEventsFromSalesforce()
    {
       //call SalesForce service and get JSON data
     eventList = parsedJson.Data;
     var cacheExpirationTime = Config.Get<EventsCacheConfig>().CacheExpirationTime;
                    CacheManager.Add(
                        cacheKeywordEvents,
                        eventList,
                        CacheItemPriority.Normal,
                        null,
                        new SlidingTime(TimeSpan.FromMinutes(cacheExpirationTime)));                     
                return eventList;
     }

The caching of events is achieved via the Caching Application Block by Microsoft Enterprise Library. The cache can be invalidated via setting a time duration. This is called a time-based expiration. Content editors can modify the expiration time by the basic settings section in Sitefinity. The implementation is made so that on initial load the events are substituted in the cache. The cache will be invalidated only if there are no requests during the expiration time. This is achieved by creating a CacheManager instance and new SlidingTime object. Using the Sliding expiration allows caching of items that are frequently used in the application. This approach is better for the performance of the application as it will not use unnecessary memory.
An alternative would be to use AbsoluteTime however this can be used only in cases when cached data is not changed frequently.

3) Load items list from the cache

On the first page load, the events list will be retrieved from SalesForce via GetEventsFromSalesForce method where the cache will be substituted. For every other request, events list will be loaded from the cache:

    internal static IList<EventModel> GetEventsList()
    {
        IList<EventModel> eventList = new List<EventModel>();

        if (((IList<EventModel>)CacheManager[cacheKeywordEvents]) != null)
        {
            eventList = (List<EventModel>)CacheManager[cacheKeywordEvents];
        }
        else
        {
            eventList = GetEventsFromSalesforce();
        }

        return eventList;
    }

    private const string cacheKeywordEvents = "eventListCached";

The items are stored in a special collection with custom key - cacheKeywordEvents.


Hope you enjoyed :)

Veronica Milcheva

About Veronica Milcheva

I am a passionate Sitefinity blogger, developer and consultant. In my spare time I enjoy running and listening to music. My personal quote: There's no tough problem, just not enough coffee :)

View Comments

comments powered by Disqus