Ejemplo n.º 1
0
            /// <summary>
            /// 检查表达式是否能够被缓存。
            /// </summary>
            /// <param name="expression"></param>
            /// <returns></returns>
            public static bool Check(Expression expression)
            {
                var checker = new CachaebleChecker();

                checker.Visit(expression);
                return(checker.cacheable);
            }
Ejemplo 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))));
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }