private System.Collections.Generic.IDictionary <string, TPrimaryKey> GetPageList() { System.Collections.Generic.IDictionary <string, TPrimaryKey> pages; if (!_cache.TryGetValue(_cacheKey, out pages)) { // Only allow one thread to poplate the data lock (_lock) { if (!_cache.TryGetValue(_cacheKey, out pages)) { pages = _dataProvider.GetPageToIdMap(); _cache.Set(_cacheKey, pages, new Microsoft.Extensions.Caching.Memory.MemoryCacheEntryOptions() { Priority = Microsoft.Extensions.Caching.Memory.CacheItemPriority.NeverRemove, AbsoluteExpirationRelativeToNow = System.TimeSpan.FromSeconds(this.CacheTimeoutInSeconds) } ); } } } return(pages); }
/// <summary> /// Redirects this instance. /// </summary> /// <returns>IActionResult.</returns> public IActionResult redirect() { var gg = HttpContext.Request.Path.ToString().Replace(@"/home/redirect/", String.Empty); var rtn = new PageURL(); var result = _localCache.TryGetValue(gg.ToString(), out rtn); return(View(rtn)); }
public void setCatch(string key, object value) { if (!_memoryCache.TryGetValue(key, out var cacheEntry)) { // Key not in cache, so get data. cacheEntry = DateTime.Now; // Set cache options. var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetSlidingExpiration(TimeSpan.FromSeconds(30000000)); // Save data in cache. _memoryCache.Set(key, value, cacheEntryOptions); } }
public bool TryGetValue <T>(string key, out T value) { value = default(T); object result = null; if (cache.TryGetValue(key, out result) && result is T) { value = (T)result; return(true); } return(false); }
/// <summary> /// Stores the specified local cache. /// </summary> /// <param name="_localCache">The local cache.</param> /// <param name="storestyle">The storestyle.</param> /// <param name="alldatabase">The alldatabase.</param> /// <param name="database">The database.</param> public static void Store(IMemoryCache _localCache, RestoreStoreStyleEnum storestyle, IAllPageURLRepository alldatabase, IPageURLRepository database) { bool blanklist = true; var _allKeys = new AllPageUrls(); _localCache.TryGetValue("AllKeys", out _allKeys); if (storestyle == RestoreStoreStyleEnum.File) { using FileStream fs = File.Create("storevalues.json"); var _output = JsonConvert.SerializeObject(_allKeys.AllUrls); byte[] bytes = Encoding.UTF8.GetBytes(_output); fs.Write(bytes, 0, bytes.Length); } if (storestyle == RestoreStoreStyleEnum.Db) { var allDbkey = alldatabase.GetAllPageURL(); var allDbkeys = database.GetAllPageURL(); Guid originalId = Guid.NewGuid(); if (allDbkey.Result != null) { originalId = allDbkey.Result.Id; blanklist = false; } var Allkeys = new AllPageUrls() { Id = originalId, AllUrls = new List <PageURL>() }; Allkeys.AllUrls.AddRange(_allKeys.AllUrls); if (blanklist) { alldatabase.SetAllUrls(Allkeys); } else { alldatabase.Update(Allkeys); } } }
public async Task <IActionResult> GetWeatherForecast([FromQuery] SearchCriteria searchCriteria) { try { WeatherViewModel weatherViewModel; if (searchCriteria == null || ((searchCriteria.isZipCode && string.IsNullOrEmpty(searchCriteria.ZipCode)) || (!searchCriteria.isZipCode && string.IsNullOrEmpty(searchCriteria.City)))) { return(BadRequest("Enter valid details")); } string searchText = string.Empty; //check for zipcode or city to filter from memory cache if (searchCriteria.isZipCode) { searchText = searchCriteria.ZipCode; } else { searchText = searchCriteria.City; } //set into cache bool isExist = memoryCache.TryGetValue(searchText, out weatherViewModel); if (!isExist) { weatherViewModel = await weatherService.GetWeatherForecast(searchCriteria); var cacheEntryOptions = new MemoryCacheEntryOptions() .SetSlidingExpiration(TimeSpan.FromMinutes(60)); memoryCache.Set(searchText, weatherViewModel, cacheEntryOptions); } else { weatherViewModel = memoryCache.Get <WeatherViewModel>(searchText); } return(Ok(weatherViewModel)); } catch (Exception ex) { return(exceptionHelper.GetResponseStatus(ex)); //Log error details } }
public bool TryGetValue <T>(string key, out T cachedValue) where T : class => _memoryCache.TryGetValue(key, out cachedValue);