예제 #1
0
        private static async Task<RType> Get<RType>(string cacheKey, Func<Task<RType>> createFunc
            , TimeSpan slidingExpiration, DateTime? absoluteExpiration,
            DateTime? hitProtectExpiration, string moduleName)
        {
            var obj = GetCache(moduleName).Get<HitProtectCahce<RType>>(cacheKey);
            if (!obj.Equals(null))
            {
                if (obj.IsNull)
                    return default(RType);

                return obj.Data;
            }

            var data = await createFunc.Invoke();
            if (data==null)
            {
                if (hitProtectExpiration.HasValue)
                {
                    var safeData = new HitProtectCahce<RType>();
                    Set(cacheKey, safeData, hitProtectExpiration.Value);
                }
            }
            else
            {
                var safeData = new HitProtectCahce<RType>(data);

                if (absoluteExpiration.HasValue)
                    Set(cacheKey, safeData, absoluteExpiration.Value);
                else
                    Set(cacheKey, safeData, slidingExpiration);
            }
            return data;
        }
예제 #2
0
        private static async Task <ResultMo <RType> > Get <RType>(string cacheKey, Func <Task <ResultMo <RType> > > createFunc
                                                                  , TimeSpan slidingExpiration, DateTime?absoluteExpiration,
                                                                  DateTime?hitProtectExpiration, string moduleName)
        {
            var obj = GetCache(moduleName).Get <HitProtectCahce <RType> >(cacheKey);

            if (!obj.Equals(null))
            {
                if (obj.IsNull)
                {
                    return(new ResultMo <RType>(ResultTypes.ObjectNull, "未发现对应数据!"));
                }

                return(new ResultMo <RType>(obj.Data));
            }

            var metaRes = await createFunc.Invoke();

            if (!metaRes.IsSuccess() || metaRes.data == null)
            {
                if (hitProtectExpiration.HasValue)
                {
                    var safeData = new HitProtectCahce <RType>();
                    Set(cacheKey, safeData, hitProtectExpiration.Value);
                }
            }
            else
            {
                var safeData = new HitProtectCahce <RType>(metaRes.data);

                if (absoluteExpiration.HasValue)
                {
                    Set(cacheKey, safeData, absoluteExpiration.Value);
                }
                else
                {
                    Set(cacheKey, safeData, slidingExpiration);
                }
            }
            return(metaRes);
        }