public CachedObject <TResult> GetItem <TResult>(ProxyRequest <T, TResult> proxyRequest, CachePolicy cachePolicy) { var key = proxyRequest.CreateHash(); object value; if (_client.TryGet(key, out value)) { var cachedObject = value as MemcachedObject <TResult>; if (cachedObject == null) { return(new CachedObject <TResult>(CachedObjectState.None, null)); } if (cachedObject.IsException && cachedObject.Created.AddSeconds(cachePolicy.ExceptionCacheDuration) > DateTime.Now) { return(new CachedObject <TResult>(CachedObjectState.Exception, cachedObject.Exception)); } else if (cachedObject.IsException) { return(new CachedObject <TResult>(CachedObjectState.None, null)); } var fresh = cachedObject.Created.AddSeconds(cachePolicy.CacheDuration) > DateTime.Now; var state = fresh ? CachedObjectState.Fresh : CachedObjectState.Stale; return(new CachedObject <TResult>(state, cachedObject.Object)); } return(new CachedObject <TResult>(CachedObjectState.None, null)); }
public bool TryGet <T>(string key, out T value) { value = default(T); object cached = new object(); if (cache.TryGet(key, out cached)) { value = (T)cached; return(true); } return(false); }
public bool TryGet(string key, out object value) { return(_memcachedClient.TryGet(key, out value)); }
/// <summary> /// Exists the specified cacheKey. /// </summary> /// <returns>The exists.</returns> /// <param name="cacheKey">Cache key.</param> public bool Exists(string cacheKey) { ArgumentCheck.NotNullOrWhiteSpace(cacheKey, nameof(cacheKey)); return(_memcachedClient.TryGet(this.HandleCacheKey(cacheKey), out object obj)); }
/// <summary> /// 是否存在指定缓存 /// </summary> /// <param name="cacheKey">缓存键</param> /// <returns></returns> public bool Exists(string cacheKey) { Check.NotNullOrEmpty(cacheKey, nameof(cacheKey)); return(_memcachedClient.TryGet(this.HandleCacheKey(cacheKey), out var obj)); }