Esempio n. 1
0
        private static CacheTimeRate WriteCacheByCacheTimeSetting(object resultValue, string cacheKey, double cacheSeconds, string cacheTimeSetting)
        {
            CacheTimeRate cacheTimeRate = null;

            try
            {
                if (cacheSeconds <= 0)
                {
                    return(null);
                }
                cacheTimeRate = CacheTimeSettings.GetCacheTimeRate();
                if (cacheTimeRate == null || (cacheTimeRate.AppCacheTime == 0 && cacheTimeRate.RedisCacheTime == 0))
                {
                    return(null);
                }

                // 对不需要封装的接口,无需封装
                if (!resultValue.GetType().ToString().StartsWith("Modobay.Api.ResultDto"))
                {
                    resultValue = ResultBuilder.AsSuccess(resultValue);
                }

                // 本地缓存
                if (cacheTimeRate.AppCacheTime > 0 || IsStatic(cacheTimeSetting))
                {
                    var seconds = cacheTimeRate.AppCacheTime * cacheSeconds;
                    if (seconds < 60)
                    {
                        seconds = 60;
                    }
                    if (seconds > 600)
                    {
                        seconds = 600;
                    }
                    AppCache.Set(cacheKey, resultValue, TimeSpan.FromSeconds(seconds));
                    //AppCache.Set(cacheKey, resultValue, TimeSpan.FromSeconds(cacheTimeRate.AppCacheTime * cacheSeconds));
                }

                // 分布式缓存
                if (cacheTimeRate.RedisCacheTime > 0 && !IsStatic(cacheTimeSetting)) //if (cacheTimeRate.RedisCacheTime > 0 && !RedisCache.KeyExists(cacheKey))
                {
                    RedisCache.Set2(cacheKey, resultValue, TimeSpan.FromSeconds(cacheTimeRate.RedisCacheTime * cacheSeconds));
                }

                // 静态模式更新文件?
            }
            catch (Exception ex) { Lib.Log.WriteExceptionLog($"WriteCache Exception:{cacheKey}  {ex.Message}  <br> StackTrace:{ex.StackTrace}"); }
            return(cacheTimeRate);
        }
Esempio n. 2
0
        internal static ResultDto <dynamic> ReadCache(ActionExecutingContext context, IAppContext appContext)
        {
            try
            {
                if (!IsEnableApiCache(appContext.AppID))
                {
                    return(null);
                }
                var cacheTimeRate = CacheTimeSettings.GetCacheTimeRate();
                if (cacheTimeRate == null || (cacheTimeRate.AppCacheTime == 0 && cacheTimeRate.RedisCacheTime == 0))
                {
                    return(null);
                }
                var actionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;
                var cacheSeconds     = CalcCacheSeconds(actionDescriptor, out var cacheTimeSetting);
                if (cacheSeconds <= 0)
                {
                    return(null);
                }
                ResultDto <dynamic> cacheResult = null;
                var cacheKey = GetApiCacheKey(actionDescriptor, context.HttpContext);

                try
                {
                    if (cacheTimeRate.AppCacheTime > 0)
                    {
                        AppCache.TryGetValue <ResultDto <dynamic> >(cacheKey, out cacheResult);
                    }
                    if (cacheResult == null && cacheTimeRate.RedisCacheTime > 0)
                    {
                        cacheResult = RedisCache.Get2 <ResultDto <dynamic> >(cacheKey);

                        if (cacheResult != null && cacheTimeRate.AppCacheTime > 0)
                        {
                            // 恢复本地缓存
                            var seconds = cacheTimeRate.AppCacheTime * cacheSeconds;
                            if (seconds < 60)
                            {
                                seconds = 60;
                            }
                            if (seconds > 600)
                            {
                                seconds = 300;
                            }
                            AppCache.Set(cacheKey, cacheResult, TimeSpan.FromSeconds(seconds));
                            //AppCache.Set(cacheKey, cacheResult, TimeSpan.FromSeconds(cacheTimeRate.AppCacheTime * cacheSeconds));
                        }
                    }

                    // 存在缓存则检查缓存是否需要更新;如果cacheResult为null,则接口调用完成后由WriteCache写入缓存
                    if (cacheResult != null)
                    {
                        var isNeedUpdate = CheckUpdateAndFlag(cacheKey, cacheTimeSetting);
                        if (isNeedUpdate)
                        {
                            Task.Run(async() =>
                            {
                                var newAppContext = AppManager.CopyAppContext(appContext);
                                await Task.Delay(1);
                                var resultValue = InvokeByActionContext(context, newAppContext);
                                if (resultValue != null)
                                {
                                    WriteCacheByCacheTimeSetting(resultValue, cacheKey, cacheSeconds, cacheTimeSetting);
                                }
                            });
                        }
                    }

                    return(cacheResult);
                }
                catch (Exception ex)
                {
                    Lib.Log.WriteExceptionLog($"ReadCache-{cacheKey} Exception:{cacheKey}  {ex.Message}  <br> StackTrace:{ex.StackTrace}");
                    return(null);
                }
            }
            catch (Exception exx)
            {
                return(null);
            }
        }