Lab 07 - Cache Support

Duration: ~10mins

Here a classic business method without any Cache Support. It simply loads all the countries from the database.

// Business | CountryBusiness.custom.cs

namespace Com.Example.Labs.Business
{
    using System;

    using Com.Example.Labs.Core;
    using Com.Example.Labs.Crud;
    using Com.Example.Labs.Models;

    [BusinessClass]
    public class CountryBusiness : BusinessBase
    {
        #region [ Custom public methods ]

        [BusinessMethod]
        public TCollection<Country> GetAllCountries(IUserContext userContext)
        {
            TCollection<Country> countries = null;

            using (var et = new ExecutionTracerService())
            using (var db = new CountryCrud(userContext))
            {
                SearchOptions options = new SearchOptions();

                // Search without SearchOptions.Filters retrieves all the records.
                countries = db.Search(ref options); 
            }

            return countries;
        }

        #endregion
    }
}

The same business method with Cache Support.

// Business | CountryBusiness.custom.cs

namespace Com.Example.Labs.Business
{
    using System;

    using Com.Example.Labs.Core;
    using Com.Example.Labs.Crud;
    using Com.Example.Labs.Models;

    [BusinessClass]
    public class CountryBusiness : BusinessBase
    {
        #region [ Member ]

        private static readonly object _getAllCountriesCacheLocker = new object();

        #endregion

        #region [ Custom public methods ]

        [BusinessMethod]
        public TCollection<Country> GetAllCountries(IUserContext userContext)
        {
            const string cacheKey = "GetAllCountries";

            // Retrieve the data from the default cache.
            var countries = CacheServiceHelper.Current.Get<TCollection<Country>>(cacheKey);
            if (countries == null)
            {
                lock (_getAllCountriesCacheLocker)
                {
                    countries = CacheServiceHelper.Current.Get<TCollection<Country>>(cacheKey);
                    if (countries == null)
                    {
                        // No data found. Request from the database...

                        using (var et = new ExecutionTracerService())
                        using (var db = new CountryCrud(userContext))
                        {
                            SearchOptions options = new SearchOptions();

                            countries = db.Search(ref options);
                        }

                        // Add the data to the default cache for 2 hours.
                        CacheServiceHelper.Current.Add(new CacheItem
                        {
                            Key = cacheKey,
                            Data = countries,
                            Expiration = DateTime.Now.AddHours(2)
                        });
                    }
                }
            }

            return countries;
        }

        #endregion
    }
}

It is possible to change the default ICacheService implementation (System.LocalMemoryCacheService) edit the implementation (with the new one) attribute in the Configuration file.

<serviceLocatorConfiguration>
  <instances>
    <instance
      interface="System.ILoggerService, System.Extensions"
      implementation="System.LoggerService, System.Extensions"
      instancingMode="Singleton"/>
    <instance
      interface="System.ICacheService, System.Extensions"
      implementation="System.LocalMemoryCacheService, System.Extensions"
      instancingMode="Singleton"/>
    <instance
      interface="System.ISmtpService, System.Extensions"
      implementation="System.SmtpService, System.Extensions"
      instancingMode="Singleton"/>
  </instances>
</serviceLocatorConfiguration>