NET Caching Library Help
Getting Started C#

 

Example of loading a monthly sales report from the cache. It demonstrates attaching a memory cache with a backing file storage provider and loading and saving to the cache. 

  

 

    public class Example

    {

       

        public void ExampleUsage()

        {

            //Create a Primary Memory Cache with a backing File Cache

            string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "KellermanSoftware", "NetCachingLibrary", "Example");

            SmartConfig config = new SmartConfig(new MemoryCacheProvider(), new FileCacheProvider(directory));

 

            //Specify UserName and LicenseKey for Licensed Mode

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

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

 

            //Instantiate the cache

            SmartCache cache = new SmartCache(config);

 

            //Build parameters for a long running 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.BuildCacheKey("MonthlySalesReport", parms);

 

            //Attempt to load the report from the cache

            List<ReportDetail> report = cache.Get<List<ReportDetail>>(cacheKey);

 

            //Not found in the cache

            if (report == null)

            {

                //Load the report from the database

                report = ReportLogic.MonthlySalesReport(parms);

                //Save to the cache in the Default region using the default expiration for the primary and secondary caches

                cache.Set<List<ReportDetail>>(cacheKey, report);

            }

        }

    }

    public static class ReportLogic

    {

        //Example long running report

        public static List<ReportDetail> MonthlySalesReport(Dictionary<string, object> parms)

        {

            System.Threading.Thread.Sleep(1000);

            List<ReportDetail> list = new List<ReportDetail>();

            list.Add(new ReportDetail("Midwest", 233457.47m));

            list.Add(new ReportDetail("Southern", 234789.88m));

            return list;

        }

    }