Exemple #1
0
        /// <summary>
        /// 尝试获取指定表达式的缓存。
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="func">当缓存不存在时,创建缓存数据的函数。</param>
        /// <returns></returns>
        internal static Delegate TryGetDelegate(Expression expression, Func <LambdaExpression> func)
        {
            if (!CachaebleChecker.Check(expression))
            {
                return(func().Compile());
            }

            var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>();
            var option  = section == null ? TranslateOptions.Default : section.Options;

            if (!option.ParseCacheEnabled)
            {
                return(func().Compile());
            }

            var lazy = new Lazy <Delegate>(() =>
            {
                //将表达式内的 Segment 替换成参数
                var segParExp = Expression.Parameter(typeof(IDataSegment), "g");
                var lambdaExp = func() as LambdaExpression;
                var newExp    = SegmentReplacer.Repalce(lambdaExp.Body, segParExp);
                lambdaExp     = Expression.Lambda(newExp, lambdaExp.Parameters[0], segParExp);

                return(lambdaExp.Compile());
            });

            var cacheKey = ExpressionKeyGenerator.GetKey(expression, "Trans");

            return(MemoryCacheManager.Instance.TryGet(cacheKey, () => lazy.Value, () => new RelativeTime(TimeSpan.FromSeconds(option.ParseCacheExpired))));
        }
Exemple #2
0
        /// <summary>
        /// 尝试获取指定表达式的缓存。
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="func">当缓存不存在时,创建缓存数据的函数。</param>
        /// <returns></returns>
        internal static Delegate TryGetDelegate(Expression expression, Func <LambdaExpression> func)
        {
            var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>();
            var option  = section == null ? TranslateOptions.Default : section.Options;

            var result = CacheableChecker.Check(expression);

            if (!result.Required || (result.Enabled == null && !option.CacheParsing) || result.Enabled == false)
            {
                return(func().Compile());
            }

            var cacheKey = ExpressionKeyGenerator.GetKey(expression, "Trans");

            cacheKey = NativeCacheKeyContext.GetKey(cacheKey);

            return(MemoryCacheManager.Instance.TryGet(cacheKey, () =>
            {
                var lambdaExp = func() as LambdaExpression;
                var segment = SegmentFinder.Find(expression);
                if (segment != null)
                {
                    //将表达式内的 Segment 替换成参数
                    var segParExp = Expression.Parameter(typeof(IDataSegment), "g");
                    var newExp = SegmentReplacer.Repalce(lambdaExp.Body, segParExp);
                    var parameters = new List <ParameterExpression>(lambdaExp.Parameters);
                    parameters.Insert(1, segParExp);
                    lambdaExp = Expression.Lambda(newExp, parameters.ToArray());
                }

                return lambdaExp.Compile();
            },
                                                      () => new RelativeTime(result.Expired ?? TimeSpan.FromSeconds(option.CacheParsingTimes))));
        }
Exemple #3
0
        /// <summary>
        /// 尝试通过表达式获取执行后的结果缓存。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        internal static T TryGet <T>(Expression expression, Func <T> func)
        {
            var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>();
            var option  = section == null ? TranslateOptions.Default : section.Options;
            var result  = CacheableChecker.Check(expression);

            //没有开启数据缓存
            if ((result.Enabled == null && !option.CacheExecution) || result.Enabled == false)
            {
                return(func());
            }

            var cacheMgr = CacheManagerFactory.CreateManager();

            if (cacheMgr == null)
            {
                return(func());
            }

            var cacheKey = ExpressionKeyGenerator.GetKey(expression, "Exec");

            cacheKey = NativeCacheKeyContext.GetKey(cacheKey);

            Reference(cacheKey, expression);

            var segment = SegmentFinder.Find(expression);
            var pager   = segment as DataPager;

            var cacheFunc = new Func <CacheItem <T> >(() =>
            {
                var data = EnumerateData(func());

                var isEnumerable = typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(IEnumerable <>);
                if (isEnumerable)
                {
                    var elementType = typeof(T).GetEnumerableElementType();
                    var method      = typeof(Enumerable).GetMethod(nameof(Enumerable.ToList)).MakeGenericMethod(elementType);
                    data            = (T)method.Invoke(null, new object[] { data });
                }

                var total = 0;
                if (pager != null)
                {
                    total = pager.RecordCount;
                }

                return(new CacheItem <T> {
                    Data = data, Total = total
                });
            });

            var cacheItem = cacheMgr.TryGet(cacheKey, cacheFunc, () => new RelativeTime(result.Expired ?? TimeSpan.FromSeconds(option.CacheExecutionTimes)));

            if (pager != null)
            {
                pager.RecordCount = cacheItem.Total;
            }

            return(cacheItem.Data);
        }
Exemple #4
0
        /// <summary>
        /// 尝试通过表达式获取执行后的结果缓存。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        internal static T TryGet <T>(Expression expression, Func <T> func)
        {
            var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>();
            var option  = section == null ? TranslateOptions.Default : section.Options;

            //没有开启数据缓存
            if (!option.DataCacheEnabled)
            {
                return(func());
            }

            var cacheMgr = CacheManagerFactory.CreateManager();

            if (cacheMgr == null)
            {
                return(func());
            }

            var cacheKey = ExpressionKeyGenerator.GetKey(expression, "Exec");

            Reference(cacheKey, expression);

            var segment = SegmentFinder.Find(expression);
            var pager   = segment as DataPager;

            var cacheFunc = new Func <CacheItem <T> >(() =>
            {
                var data  = func();
                var total = 0;
                if (pager != null)
                {
                    total = pager.RecordCount;
                }

                return(new CacheItem <T> {
                    Data = data, Total = total
                });
            });

            var cacheItem = cacheMgr.TryGet(cacheKey, cacheFunc, () => new RelativeTime(TimeSpan.FromSeconds(option.DataCacheExpired)));

            if (pager != null)
            {
                pager.RecordCount = cacheItem.Total;
            }

            return(cacheItem.Data);
        }