Exemplo n.º 1
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))));
        }
Exemplo n.º 2
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))));
        }
Exemplo n.º 3
0
            /// <summary>
            /// 使用 <paramref name="parExp"/> 替换表达式中的 <see cref="IDataSegment"/> 对象。
            /// </summary>
            /// <param name="expression"></param>
            /// <param name="parExp"></param>
            /// <returns></returns>
            public static Expression Repalce(Expression expression, ParameterExpression parExp)
            {
                var replaer = new SegmentReplacer {
                    parExp = parExp
                };

                return(replaer.Visit(expression));
            }
Exemplo n.º 4
0
            /// <summary>
            /// 使用 <paramref name="segment"/> 替换表达式中的 <see cref="IDataSegment"/> 对象。
            /// </summary>
            /// <param name="expression"></param>
            /// <param name="segment"></param>
            /// <returns></returns>
            public static Expression Repalce(Expression expression, IDataSegment segment)
            {
                var replaer = new SegmentReplacer {
                    dataSegment = segment
                };

                return(replaer.Visit(expression));
            }
Exemplo n.º 5
0
        /// <summary>
        /// 尝试获取指定表达式的缓存。
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="context"></param>
        /// <param name="creator">当缓存不存在时,创建缓存数据的函数。</param>
        /// <returns></returns>
        Delegate IQueryCache.TryGetDelegate(Expression expression, QueryCacheContext context, Func <LambdaExpression> creator)
        {
            var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>();
            var option  = section == null ? TranslateOptions.Default : section.Options;

            var result = CacheableChecker.Check(expression);

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

            var generator = _serviceProvider.TryGetService <IQueryCacheKeyGenerator>(() => ExpressionKeyGenerator.Instance);
            var cacheKey  = _serviceProvider.GetCacheKey(generator.Generate(expression, "Trans"));

            Tracer.Debug($"QueryCache access to '{cacheKey}'");
            var cacheMgr = _serviceProvider.TryGetService <IMemoryCacheManager>(() => MemoryCacheManager.Instance);

            return(cacheMgr.TryGet(cacheKey, () =>
            {
                var lambdaExp = creator() 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(GetExpire(result, option, context))));
        }
Exemplo n.º 6
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());
            }

            ClearExpiredKeys();

            var lazy = new Lazy <CacheItem>(() =>
            {
                //将表达式内的 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(new CacheItem
                {
                    Delegate = lambdaExp.Compile(),
                    Expired = DateTime.Now.AddSeconds(option.ParseCacheExpired)
                });
            });

            var cacheKey = GetKey(expression);
            var result   = cache.GetOrAdd(cacheKey, k => lazy.Value);

            return(result.Delegate);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 尝试获取指定表达式的缓存。
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="func">当缓存不存在时,创建缓存数据的函数。</param>
        /// <returns></returns>
        internal static Expression <Func <IDatabase, object> > TryGet(Expression expression, Func <Expression <Func <IDatabase, object> > > func)
        {
            if (!CachaebleChecker.Check(expression))
            {
                return(func());
            }

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

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

            ClearExpiredKeys();

            var lazy = new Lazy <CacheItem>(() => new CacheItem
            {
                Expression = func(),
                Expired    = DateTime.Now.AddSeconds(option.ParseCacheExpired)
            });

            var cacheKey = GetKey(expression);
            var result   = cache.GetOrAdd(cacheKey, k => lazy.Value);

            //在现有的表达式中查找 IDataSegment,去替换缓存中的 IDataSegment
            //原因是前端需要获得分页信息,如果不进行替换,将无法返回信息
            var segment = SegmentFinder.Find(expression);

            if (segment != null)
            {
                result.Expression = (Expression <Func <IDatabase, object> >)SegmentReplacer.Repalce(result.Expression, segment);
            }

            return(result.Expression);
        }