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));
        }
예제 #2
0
        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);
        }
예제 #3
0
 public bool TryGet(string key, out object value)
 {
     return(_memcachedClient.TryGet(key, out value));
 }
예제 #4
0
        /// <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));
        }
예제 #5
0
        /// <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));
        }