Example #1
0
        public CacheValueAndsource getCacheMethodOne()
        {
            string cacheValue;
            string sourceOfData     = "Cache";
            CacheValueAndsource cvs = new CacheValueAndsource();

            cvs.EmployeeName = null;
            cvs.Source       = null;
            // Look for cache key.
            if (!_memoryCache.TryGetValue <string>("_EmployeeName", out cacheValue))
            {
                // Key not in cache, so get data.
                EmployeeViewModel model = new EmployeeViewModel();
                cacheValue   = model.emp.EmployeeName;
                sourceOfData = "Not Cache";

                //Cache expiration
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        .SetSlidingExpiration(TimeSpan.FromMinutes(1));

                // Save values in cache for a given key.
                _memoryCache.Set <string>("_EmployeeName", cacheValue, cacheEntryOptions);
            }
            cvs.EmployeeName = cacheValue.ToString();
            cvs.Source       = sourceOfData.ToString();
            return(cvs);
        }
        public IActionResult Index()
        {
            CacheValueAndsource vv = CacheFirstWay();

            ViewData["name"]   = vv.EmployeeName;
            ViewData["source"] = vv.Source;
            return(View());
        }