/// <summary> /// Get the specified cacheKey. /// </summary> /// <returns>The get.</returns> /// <param name="cacheKey">Cache key.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public override CacheValue <T> BaseGet <T>(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); var result = ConvertFromStoredValue <T>(_memcachedClient.Get(this.HandleCacheKey(cacheKey))); if (result.HasValue) { OnCacheHit(cacheKey); } else { OnCacheMiss(cacheKey); } return(result); }
/// <summary> /// Get the specified cacheKey, dataRetriever and expiration. /// </summary> /// <returns>The get.</returns> /// <param name="cacheKey">Cache key.</param> /// <param name="dataRetriever">Data retriever.</param> /// <param name="expiration">Expiration.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public override CacheValue <T> BaseGet <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); if (_memcachedClient.Get(this.HandleCacheKey(cacheKey)) is T result) { CacheStats.OnHit(); if (_options.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } return(new CacheValue <T>(result, true)); } CacheStats.OnMiss(); if (_options.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } var flag = _memcachedClient.Store(Enyim.Caching.Memcached.StoreMode.Add, this.HandleCacheKey($"{cacheKey}_Lock"), 1, TimeSpan.FromMilliseconds(_options.LockMs)); if (!flag) { System.Threading.Thread.Sleep(_options.SleepMs); return(Get(cacheKey, dataRetriever, expiration)); } var item = dataRetriever(); if (item != null) { this.Set(cacheKey, item, expiration); _memcachedClient.Remove(this.HandleCacheKey($"{cacheKey}_Lock")); return(new CacheValue <T>(item, true)); } else { _memcachedClient.Remove(this.HandleCacheKey($"{cacheKey}_Lock")); return(CacheValue <T> .NoValue); } }
/// <summary> /// Get the specified cacheKey, dataRetriever and expiration. /// </summary> /// <returns>The get.</returns> /// <param name="cacheKey">Cache key.</param> /// <param name="dataRetriever">Data retriever.</param> /// <param name="expiration">Expiration.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public CacheValue <T> Get <T>(string cacheKey, Func <T> dataRetriever, TimeSpan expiration) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); if (_memcachedClient.Get(this.HandleCacheKey(cacheKey)) is T result) { CacheStats.OnHit(); if (_options.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } return(new CacheValue <T>(result, true)); } CacheStats.OnMiss(); if (_options.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } var item = dataRetriever(); if (item != null) { this.Set(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } else { return(CacheValue <T> .NoValue); } }