Exemplo n.º 1
0
        public Task <MessageExchange> Process(IMessageExchangeFilter filter, MessageExchange exchange, MessageExchangeFilterInvoker invoker)
        {
            if (!exchange.IsIncompleteRequest)
            {
                return(filter.Process(exchange, invoker));
            }

            var messageLabel = exchange.Out.Label.Name;

            CacheConfiguration config;

            if (!this.cacheConfiguration.TryGetValue(messageLabel, out config))
            {
                this.CollectMetrics(messageLabel, false);
                return(filter.Process(exchange, invoker));
            }

            if (!(config.Enabled ?? false))
            {
                this.CollectMetrics(messageLabel, false);
                return(filter.Process(exchange, invoker));
            }

            var cachedValue = config.Cache[exchange.Out];
            var cached      = cachedValue != null;

            this.CollectMetrics(messageLabel, cached);

            if (cached)
            {
                exchange.In = new Message(MessageLabel.Empty, cachedValue);
                return(Filter.Result(exchange));
            }

            return(filter.Process(exchange, invoker)
                   .ContinueWith(t => TryCacheResponse(t.Result, config)));
        }
Exemplo n.º 2
0
 /// <summary>
 /// The register filter.
 /// </summary>
 /// <param name="filter">
 /// The filter.
 /// </param>
 public void RegisterFilter(IMessageExchangeFilter filter)
 {
     this.filters.Add(filter);
 }