Пример #1
0
        public override bool TryGet <T>(string key, out T entry)
        {
            Object  cacheEntry;
            Boolean exist = TryGetObject(key, out cacheEntry);

            if (!exist)
            {
                //不存在
                entry = default(T);
                return(false);
            }
            if (cacheEntry == null)
            {
                //存在但为 null
                entry = (T)((Object)null);
                return(true);
            }
            if (cacheEntry is T)
            {
                //存在,直接返回
                entry = (T)cacheEntry;
                return(true);
            }

            //check json type
            entry = NewtonsoftJsonUtil.EnsureObjectType <T>(cacheEntry);
            return(true);

            //if (!(cacheEntry is ExpirationWraper<T>)) {
            //    //类型不为 T 也不为 ExpirationWraper<T>,抛出异常
            //    throw new InvalidOperationException(String.Format("缓存项`[{0}]`类型错误, {1} or {2} ?",
            //        key, cacheEntry.GetType().FullName, typeof(T).FullName));
            //}

            //var cacheWraper = (ExpirationWraper<T>)cacheEntry;
            ////表示滑动过期缓存项
            //if (cacheWraper.SlidingExpiration == Cache.NoSlidingExpiration) {
            //    //绝对时间过期,返回
            //    entry = cacheWraper.Value;
            //    return true;
            //}

            //var diffSpan = DateTime.Now.Subtract(cacheWraper.SettingTime);
            ////当前时间-设置时间>滑动时间, 已经过期
            //if (diffSpan > cacheWraper.SlidingExpiration) {
            //    Expire(key);
            //    entry = default(T);
            //    return false;
            //}

            ////当前时间-设置时间> 滑动时间/2, 更新缓存
            //if (diffSpan.Add(diffSpan) > cacheWraper.SlidingExpiration) {
            //    entry = cacheWraper.Value;
            //    Overwrite(key, cacheWraper.Value, cacheWraper.SlidingExpiration);
            //}
            //entry = cacheWraper.Value;
            //return true;
        }
        public override bool TryGet <T>(string key, out T entry)
        {
            Object  cacheEntry;
            Boolean exist = TryGetObject(key, out cacheEntry);

            if (!exist)
            {
                //不存在
                entry = default(T);
                return(false);
            }
            if (cacheEntry == null)
            {
                //存在但为 null
                entry = (T)((Object)null);
                return(true);
            }
            if (cacheEntry is T)
            {
                //存在,直接返回
                entry = (T)cacheEntry;
                return(true);
            }

            //使用与不使用 NewtonsoftJsonTranscoder 的情况下都支持
            SlidingCacheWrapper <T> slidingCache;

            if (SlidingCacheWrapper <T> .IsSlidingCache(cacheEntry, out slidingCache))
            {
                //尝试以 SlidingCacheWrapper<T> 处理
                var diffSpan = DateTime.Now.Subtract(slidingCache.SettingTime);
                //当前时间-设置时间>滑动时间, 已经过期
                if (diffSpan > slidingCache.SlidingExpiration)
                {
                    Expire(key);
                    entry = default(T);
                    return(false);
                }

                //当前时间-设置时间> 滑动时间/2, 更新缓存
                if (diffSpan.Add(diffSpan) > slidingCache.SlidingExpiration)
                {
                    Overwrite(key, slidingCache.Value, slidingCache.SlidingExpiration);
                }
                entry = slidingCache.Value;
            }
            else
            {
                //尝试以普通JSON处理
                entry = NewtonsoftJsonUtil.EnsureObjectType <T>(cacheEntry);
            }
            return(true);
        }