Пример #1
0
        /// <summary>
        /// 异步的,尝试通过表达式获取执行后的结果缓存。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="expression"></param>
        /// <param name="context"></param>
        /// <param name="creator"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        async Task <T> IExecuteCache.TryGetAsync <T>(Expression expression, ExecuteCacheContext context, Func <CancellationToken, Task <T> > creator, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

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

            if (context.Enabled == true || (context.Enabled == null && option.CacheExecution))
            {
                TryExpire(expression);
            }

            var evaluator = _serviceProvider.TryGetService <IExecuteCacheEvaluator>();

            var result = CacheableChecker.Check(evaluator, expression);

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

            var generator = _serviceProvider.TryGetService <IExecuteCacheKeyGenerator>(() => ExpressionKeyGenerator.Instance);
            var cacheKey  = _serviceProvider.GetCacheKey(generator.Generate(expression, CACHE_KEY));

            Tracer.Debug($"DefaultExecuteCache access to '{cacheKey}'");

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

            using var edps = new EntityDeserializeProcessorScope();
            var cacheItem = _cacheMgr.TryGet(cacheKey,
                                             () => HandleCacheItem(cacheKey, expression, creator(cancellationToken).AsSync(), pager),
                                             () => new RelativeTime(GetExpire(result, option, context)));

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

            TryStartRunner();

            return(cacheItem.Data);
        }
Пример #2
0
        private TimeSpan GetExpire(CacheableCheckResult checkResult, TranslateOptions option, ExecuteCacheContext cacheOpt)
        {
            if (checkResult.Expired != null)
            {
                return(checkResult.Expired.Value);
            }
            else if (cacheOpt.Times != null)
            {
                return(cacheOpt.Times.Value);
            }

            return(option.CacheExecutionTimes);
        }