Пример #1
0
        internal static int CalcCacheSeconds(ControllerActionDescriptor controllerActionDescriptor, out string cacheTimeSetting)
        {
            object[] responseCache;
            if (controllerActionDescriptor.ControllerTypeInfo.Name.EndsWith("Proxy"))
            {
                // 代理类,从源类型中查找相关特性
                var sourceControllerType = AppManager.ProxyOriginalTypes[controllerActionDescriptor.ControllerTypeInfo.FullName];
                var methodInfo           = sourceControllerType.GetMethod(controllerActionDescriptor.MethodInfo.Name);
                responseCache = methodInfo.GetCustomAttributes(typeof(CacheAttribute), true);
            }
            else
            {
                responseCache = controllerActionDescriptor.MethodInfo.GetCustomAttributes(typeof(CacheAttribute), true);
            }
            if (responseCache.Count() != 1)
            {
                cacheTimeSetting = string.Empty;
                return(0);
            }

            var cache = responseCache[0] as CacheAttribute;

            cacheTimeSetting = cache.CacheProfileName;
            return(CacheTimeSettings.GetCacheSeconds(cache.CacheProfileName));
        }
Пример #2
0
        private static T GetAndCache <T>(Delegate func, string cacheTimeSetting, string flag = "", params object[] input)
        {
            var cacheResult = default(T);
            var cacheKey    = GetDataCacheKey <T>(func, flag);
            var cacheTime   = TimeSpan.FromSeconds(CacheTimeSettings.GetCacheSeconds(cacheTimeSetting));

            // 从本地缓存返回
            AppCache.TryGetValue(cacheKey, out cacheResult);

            // 从本地文件返回
            if (cacheResult == null && IsStatic(cacheTimeSetting))
            {
                var cacheFileName = GetCacheFileName(cacheKey);
                if (File.Exists(cacheFileName))
                {
                    cacheResult = JsonConvert.DeserializeObject <T>(File.ReadAllText(cacheFileName));
                    AppCache.Set(cacheKey, cacheResult, cacheTime);// 恢复本地缓存
                }
            }

            // 从redis缓存返回
            if (cacheResult == null && !IsStatic(cacheTimeSetting))
            {
                cacheResult = RedisCache.Get <T>(cacheKey);
                if (cacheResult != null)
                {
                    AppCache.Set(cacheKey, cacheResult, cacheTime);// 恢复本地缓存
                    //if (IsStatic(cacheTimeSetting))
                    //{
                    //    CacheFile(cacheKey, JsonConvert.SerializeObject(cacheResult), typeof(T).FullName);
                    //}
                }
            }

            // 从数据源返回
            if (cacheResult == null)
            {
                // 如果其他线程已在获取,则等待,超时再重新获取
                var isNeedUpdate = CacheManager.CheckUpdateAndFlag(cacheKey, cacheTimeSetting);//, "UpdateCache:Data"
                if (!isNeedUpdate)
                {
                    var index = 0;
                    while (cacheResult == null && index < 30)
                    {
                        index += 1;
                        AppCache.TryGetValue <T>(cacheKey, out cacheResult);
                        if (cacheResult == null)
                        {
                            cacheResult = RedisCache.Get <T>(cacheKey);
                        }
                        if (cacheResult == null)
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                        Lib.StopwatchLog.RecordElapsedMilliseconds($"1 {cacheKey}");
                    }
                    if (cacheResult != null)
                    {
                        return(cacheResult);
                    }
                }

                cacheResult = (T)func.DynamicInvoke(input);
                Set(cacheKey, cacheResult, cacheTime, true, IsStatic(cacheTimeSetting));
                return(cacheResult);
            }

            if (cacheTimeSetting != CacheTimeSettings.Second && CacheManager.CheckUpdateAndFlag(cacheKey, cacheTimeSetting) == true)
            {
                Task.Run(async() =>
                {
                    await Task.Delay(1);
                    cacheResult = (T)func.DynamicInvoke(input);
                    Set(cacheKey, cacheResult, cacheTime, false, IsStatic(cacheTimeSetting));
                });
            }

            // 缓存预热,每隔10分钟,先异步更新缓存,并续期,待下次访问时替换为最新缓存
            return(cacheResult);
        }