/// <summary> /// Gets the specified cacheKey, dataRetriever and expiration async. /// </summary> /// <returns>The async.</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 async Task <CacheValue <T> > GetAsync <T>(string cacheKey, Func <Task <T> > dataRetriever, TimeSpan expiration) where T : class { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); var result = await _memcachedClient.GetValueAsync <T>(cacheKey); if (result != null) { return(new CacheValue <T>(result, true)); } var item = await dataRetriever?.Invoke(); if (item != null) { await SetAsync(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } else { return(CacheValue <T> .NoValue); } }
/// <summary> /// Gets the specified cacheKey, dataRetriever and expiration async. /// </summary> /// <returns>The async.</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 async Task <CacheValue <T> > GetAsync <T>(string cacheKey, Func <Task <T> > dataRetriever, TimeSpan expiration) where T : class { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); ArgumentCheck.NotNegativeOrZero(expiration, nameof(expiration)); var result = await _memcachedClient.GetValueAsync <T>(this.HandleCacheKey(cacheKey)); if (result != null) { if (_options.EnableLogging) { _logger?.LogInformation($"Cache Hit : cachekey = {cacheKey}"); } return(new CacheValue <T>(result, true)); } var item = await dataRetriever?.Invoke(); if (item != null) { await this.SetAsync(cacheKey, item, expiration); return(new CacheValue <T>(item, true)); } else { if (_options.EnableLogging) { _logger?.LogInformation($"Cache Missed : cachekey = {cacheKey}"); } return(CacheValue <T> .NoValue); } }
static async Task TrySingle() { await _memcachedClient.SetAsync(_cacheKey, new Foo(), 36000); var test = await _memcachedClient.GetValueAsync <Foo>(_cacheKey); Console.WriteLine("Single Run: {0}", test.DateTime); }
public async Task <IActionResult> Get() { ViewData["Message"] = "Called Memcached Get API."; var data = await _memcachedClient.GetValueAsync <String>(CacheKey); ViewData["GetData"] = data; for (var i = 0; i < 30; i++) { var key = "memcached-key-" + i; int testCacheSeconds = 120; var result = await _memcachedClient.GetAsync <string>(key); if (result.Value != null) { Console.WriteLine("Hit!!!! : " + result.Value); } else { Console.WriteLine("No Hit....."); } } return(View()); }
/// <summary> /// 获取缓存 /// </summary> /// <typeparam name="T">数据类型</typeparam> /// <param name="cacheKey">缓存类型</param> /// <returns></returns> public async Task <CacheValue <T> > GetAsync <T>(string cacheKey) { Check.NotNullOrEmpty(cacheKey, nameof(cacheKey)); var result = await _memcachedClient.GetValueAsync <T>(this.HandleCacheKey(cacheKey)); if (result != null) { WriteLog($"缓存击中 : cacheKey = {cacheKey}"); CacheStatsInfo.OnHit(); return(new CacheValue <T>(result, true)); } CacheStatsInfo.OnMiss(); WriteLog($"缓存穿透 : cacheKey = {cacheKey}"); return(CacheValue <T> .NoValue); }
public virtual async Task <T> GetAsync <T>(string key) { var data = await memcachedClient.GetValueAsync <T>(key); if (data == null) { return(default(T)); } else { return(data); } }
public async Task <IActionResult> Get(string key, [FromQuery] string format = "") { try { string result = await _memcachedClient.GetValueAsync <string>(key); if (!string.IsNullOrEmpty(result)) { switch (format.ToLower()) { case "json": return(Ok(JsonConvert.DeserializeObject(result))); case "xml": result = WebUtility.HtmlDecode(result); var xDoc = XDocument.Parse(result); return(new ContentResult() { Content = xDoc.Root.ToString(), ContentType = "application/xml", StatusCode = StatusCodes.Status200OK }); case "text": return(new ContentResult() { Content = result, ContentType = "plain/text", StatusCode = StatusCodes.Status200OK }); default: return(Ok(result)); } } } catch (JsonReaderException ex) { return(this.JsonFormatMismathErrorResult(key, ex, _logger)); } catch (XmlException ex) { return(this.XmlFormatMismathErrorResult(key, ex, _logger)); } catch (Exception ex) { return(this.ApiErrorResult(ex, _logger)); } return(NotFound()); }
public Task <T1> GetValueAsync <T1>(string key) { return(_memcachedClient.GetValueAsync <T1>(key)); }
/// <summary> /// 获取,用户信息 /// </summary> /// <param name="userid"></param> /// <returns></returns> public async Task <UserShowModel> GetUserInfo(long userid) { return(await _cache.GetValueAsync <UserShowModel>(GetUserInfoKey(userid))); }
public async Task <TEntity> GetAsync <TEntity>(string key) { var value = await _memoryCache.GetValueAsync <TEntity>(key); return(value); }