예제 #1
0
        private static TimeSpan?GetTtl(CacheConfiguration config, MessageExchange exchange)
        {
            if (config.Ttl != null)
            {
                return(config.Ttl.Value);
            }

            var expiresHeader = Headers.GetString(exchange.In.Headers, Headers.Expires);

            if (!string.IsNullOrEmpty(expiresHeader))
            {
                var expiration = Expires.Parse(expiresHeader);
                if (expiration != null)
                {
                    if (expiration.Period != null)
                    {
                        return(expiration.Period);
                    }

                    var now = DateTimeOffset.Now;
                    if (expiration.Date != null && expiration.Date.Value > now)
                    {
                        return(expiration.Date.Value - now);
                    }
                }
            }

            return(null);
        }
예제 #2
0
        /// <inheritdoc />
        public void Reply <TResponse>(TResponse response, Expires expires = null) where TResponse : class
        {
            if ((this.cacheConfiguration.Enabled ?? false) && this.cacheConfiguration.Ttl != null)
            {
                this.cacheConfiguration.Cache.Set(this.context.Message, response, this.cacheConfiguration.Ttl.Value);
            }

            this.context.Reply(response, expires);
        }
        /// <summary>
        /// Если в кеше, есть ответное сообщение на входящее сообщение, тогда возвращает объект из кеша.
        /// </summary>
        /// <param name="exchange">Конвейер обработки сообщений.</param>
        /// <param name="invoker">Фильтры вызывающий конвейер.</param>
        /// <returns>Задача обработки сообщений.</returns>
        public Task <MessageExchange> Process(MessageExchange exchange, MessageExchangeFilterInvoker invoker)
        {
            if (!exchange.IsIncompleteRequest)
            {
                return(invoker.Continue(exchange));
            }

            string         hash   = this.hasher.CalculateHashOf(exchange.Out).ToString();
            Maybe <object> cached = this.cacheProvider.Find <object>(hash);

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

            return(invoker.Continue(exchange)
                   .ContinueWith(
                       t =>
            {
                MessageExchange resultExchange = t.Result;
                if (!resultExchange.IsCompleteRequest)
                {
                    return resultExchange;
                }

                string expiresHeader = Headers.GetString(resultExchange.In.Headers, Headers.Expires);
                if (!string.IsNullOrEmpty(expiresHeader))
                {
                    Expires expiration = Expires.Parse(expiresHeader);

                    if (expiration.Period.HasValue)
                    {
                        this.cacheProvider.Put(hash, resultExchange.In.Payload, expiration.Period.Value);
                    }
                    else
                    {
                        this.cacheProvider.Put(hash, resultExchange.In.Payload, expiration.Date.Value);
                    }
                }

                return resultExchange;
            }));
        }