NET Caching Library Help
Expiration Examples
Examples > Expiration Examples

The .NET Caching Library provides several ways of setting the expiration policy; default expiration with configuration, passing in the same expiration policy for both, or using specific expiration. 

Using Default Expiration with Configuration

The default expiration of the primary cache provider is 20 minutes for cached items.  The default expiration for the secondary cache provider is 24 hours.

The expiration policy can be set using configuration. 

Example Default Expiration

    public static class Cache

    {

        private static SmartCache _smartCache;

        public static FileCacheProvider SecondaryCache { get; set; }

        private static void Setup()

        {

            string cacheDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),

                "KellermanSoftware",

                "NetCachingLibrary",

                "ExpirationExample");

            SecondaryCache = new FileCacheProvider(cacheDirectory);

            SecondaryCache.MaxCacheItems = 400;

            SmartConfig config = new SmartConfig(new MemoryCacheProvider(), SecondaryCache);

            //Set the default expiration

            config.DefaultPrimaryExpirationInMinutes = 20;

            config.DefaultSecondaryExpirationInMinutes = 60 * 24;  //24 hours

 

 

            //Specify UserName and LicenseKey for Licensed Mode

            //config.UserName = "my user name";

            //config.LicenseKey = "my license key";

 

            _smartCache = new SmartCache(config);

        }

        public static SmartCache Instance

        {

            get

            {

                if (_smartCache == null)

                    Setup();

                return _smartCache;

            }

        }

    }

Passing in the same expiration policy for both Example

        public void PassingInSameExpirationPolicy()

        {

            //Build parameters for the report

            Dictionary<string, object> parms = new Dictionary<string, object>();

            parms.Add("Month", DateTime.Now.Month);

            parms.Add("Year", DateTime.Now.Year);

 

            //Get the cache key based on the report parameters

            string cacheKey = Cache.Instance.BuildCacheKey("MonthlySalesReport", parms);

           

            if (!Cache.Instance.Contains(cacheKey))

            {

                //Get a long running report

                List<ReportDetail> result = ReportLogic.MonthlySalesReport(parms);

                //Expire in 30 minutes for both the primary cache and secondary cache

                Cache.Instance.Set(cacheKey, result, DateTimeOffset.Now.AddMinutes(30));

            }

        }

 

Specific Expiration Example

        public void SpecificExpirationPolicy()

        {

            //Build parameters for the report for last month

            Dictionary<string, object> parms = new Dictionary<string, object>();

            parms.Add("Month", DateTime.Now.AddMonths(-1).Month);

            parms.Add("Year", DateTime.Now.AddMonths(-1).Year);

 

            //Get the cache key based on the report parameters

            string cacheKey = Cache.Instance.BuildCacheKey("MonthlySalesReport", parms);

            if (!Cache.Instance.Contains(cacheKey))

            {

                //Get a long running report

                List<ReportDetail> result = ReportLogic.MonthlySalesReport(parms);

                //Expire from the primary cache in 20 minutes and expire from the secondary cache at the end of the month

                SmartParms smartParms = new SmartParms(key: cacheKey,

                    region:"Default",

                    primaryAbsoluteExpiration: DateTimeOffset.Now.AddMinutes(20),

                    secondaryAbsoluteExpiration: Cache.Instance.GetLastDayOfMonth());

                Cache.Instance.Set(smartParms, result);

            }

        }