Пример #1
0
        public virtual IClientCache Create(WebPageContext context)
        {
            var value = context.GetConfigValue <string>("Page", "ClientCache", string.Empty).ToLower();

            if (!string.IsNullOrEmpty(value))
            {
                switch (value)
                {
                case "forever": return(ForeverClientCache.Instance);

                case "delay": return(DelayClientCache.Instance);

                default: return(NonClientCache.Instance);
                }
            }

            //通过后缀名获取
            IClientCache cache = null;

            if (_caches.TryGetValue(context.PathExtension, out cache))
            {
                return(cache);
            }

            return(NonClientCache.Instance);
        }
Пример #2
0
 public HttpSendTransport(IClientCache clientCache, HttpSendSettings sendSettings, IReceiveObserver receiveObserver, ReceiveEndpointContext topology)
 {
     _clientCache     = clientCache;
     _sendSettings    = sendSettings;
     _receiveObserver = receiveObserver;
     _topology        = topology;
     _observers       = new SendObservable();
 }
Пример #3
0
 public HostConnectorWorker(IClientCache clientCache,
                            IOptionsMonitor <AppConfig> config,
                            ILogger <HostConnectorWorker> logger)
 {
     _clientCache = clientCache;
     _config      = config;
     _logger      = logger;
 }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CacheClientFactoryDecorator"/> class.
        /// </summary>
        /// <param name="clientFactory">The instance to decorate</param>
        /// <param name="cache">The instance to cache client instances</param>
        public CacheClientFactoryDecorator(
            IClientFactory clientFactory,
            IClientCache cache) : base(clientFactory)
        {
            Argument.NotNull(clientFactory, nameof(clientFactory));
            Argument.NotNull(cache, nameof(cache));

            _clientFactory = clientFactory;

            _cache = cache;
        }
Пример #5
0
        public ReceiveTransport(IServiceBusHost host, ClientSettings settings, IPublishEndpointProvider publishEndpointProvider,
                                ISendEndpointProvider sendEndpointProvider, IClientCache clientCache, IPipe <ClientContext> clientPipe, ReceiveTransportObservable transportObserver)
        {
            _host     = host;
            _settings = settings;
            _publishEndpointProvider = publishEndpointProvider;
            _sendEndpointProvider    = sendEndpointProvider;
            _clientCache             = clientCache;
            _clientPipe = clientPipe;

            _observers          = new ReceiveObservable();
            _transportObservers = transportObserver;
        }
Пример #6
0
 /// <summary>
 /// 注册缓存处理器,请保证cache是单例的
 /// </summary>
 /// <param name="extension"></param>
 /// <param name="cache"></param>
 public void RegisterCache(string extension, IClientCache cache)
 {
     if (!_caches.ContainsKey(extension))
     {
         lock (_caches)
         {
             if (!_caches.ContainsKey(extension))
             {
                 if (extension.StartsWith("."))
                 {
                     extension = extension.Substring(1);
                 }
                 _caches.Add(extension, cache);
                 _caches.Add(string.Format(".{0}", extension), cache);
             }
         }
     }
 }
        public static IClientCache Create(WebPageContext context)
        {
            IClientCache clientCache = null;
            var          config      = WebPagesConfiguration.Global.PageConfig;

            if (config == null || config.ClientCacheFactory == null)
            {
                //没有配置文件设置,那么查看程序集级别的注入
                clientCache = InjectionClientCacheFactory.Instance.Create(context);
            }
            else
            {
                IClientCacheFactory factory = config.ClientCacheFactory.GetInstance <IClientCacheFactory>();
                clientCache = factory.Create(context);
                if (clientCache == null)
                {
                    clientCache = InjectionClientCacheFactory.Instance.Create(context);                      //配置文件没有设置的就由系统自动设置
                }
            }
            return(clientCache);
        }
Пример #8
0
        protected virtual string ExecuteWithCacheAndSlidingExpiration(IClientCache cache,
                                                                      string url,
                                                                      string key,
                                                                      TimeSpan expiry,
                                                                      Func<IClientCache, string, TimeSpan, string>
                                                                          cacheScheme)
        {
            var fetch = cache.Get<string>(CreateCacheKey(key, url));
            if (fetch != null)
            {
                return fetch;
            }

            var result = cacheScheme.Invoke(cache, url, expiry);
            return result;
        }
Пример #9
0
        protected virtual string ExecuteWithCache(IClientCache cache,
                                                  string url,
                                                  string key,
                                                  Func<IClientCache, string, string> cacheScheme)
        {
            var fetch = cache.Get<string>(CreateCacheKey(key, url));
            if (fetch != null)
            {
                return fetch;
            }

            var result = cacheScheme.Invoke(cache, url);
            return result;
        }
Пример #10
0
        protected virtual IAsyncResult ExecutePostOrPutAsync(PostOrPut method, string url, string prefixKey,
                                                             IClientCache cache, TimeSpan slidingExpiration)
        {
            WebResponse = null;

            byte[] content;
            var request = BuildPostOrPutWebRequest(method, url, out content);

            var state = new Pair<WebRequest, Pair<byte[], Triplet<IClientCache, TimeSpan, string>>>
                            {
                                First = request,
                                Second = new Pair<byte[], Triplet<IClientCache, TimeSpan, string>>
                                             {
                                                 First = content,
                                                 Second = new Triplet<IClientCache, TimeSpan, string>
                                                              {
                                                                  First = cache,
                                                                  Second = slidingExpiration,
                                                                  Third = prefixKey
                                                              }
                                             }
                            };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Пример #11
0
        protected virtual void ExecuteGetAsync(string url, string prefixKey, IClientCache cache,
                                               TimeSpan slidingExpiration)
        {
            WebResponse = null;

            var client = CreateWebQueryClient();
            var key = CreateCacheKey(prefixKey, url);

            ThreadPool.QueueUserWorkItem(
                                            work =>
                                            ExecuteGetAsyncAndCacheWithExpiry(cache, key, url, slidingExpiration, client)
                );
        }
Пример #12
0
        // expects real cache key, not prefix
        private void ExecuteGetAsyncAndCacheWithExpiry(IClientCache cache, string key, string url,
                                                       TimeSpan slidingExpiration, IWebQueryClient client)
        {
            var fetch = cache.Get<string>(key);

            if (fetch != null)
            {
                var args = new WebQueryResponseEventArgs(fetch);
                OnQueryResponse(args);
            }
            else
            {
                var state = new Pair<IClientCache, Pair<string, TimeSpan>>
                                {
                                    First = cache,
                                    Second = new Pair<string, TimeSpan> {First = key, Second = slidingExpiration}
                                };

                var args = new WebQueryRequestEventArgs(url);
                OnQueryRequest(args);

                client.OpenReadCompleted += client_OpenReadCompleted;
                client.OpenReadAsync(new Uri(url), state);
            }
        }
Пример #13
0
 private string ExecutePostOrPutAndCacheWithExpiry(PostOrPut method, IClientCache cache, string url, string key,
                                              TimeSpan slidingExpiration, out WebException exception)
 {
     var result = ExecutePostOrPut(method, url, out exception);
     if (exception == null)
     {
         cache.Insert(CreateCacheKey(key, url), result, slidingExpiration);
     }
     return result;
 }
Пример #14
0
 protected virtual string ExecuteGet(string url, string key, IClientCache cache, TimeSpan slidingExpiration, out WebException exception )
 {
     WebException ex = null; 
     var ret = ExecuteWithCacheAndSlidingExpiration(cache, url, key, slidingExpiration,
                                                 (c, u, e) =>
                                                 ExecuteGetAndCacheWithExpiry(cache, url, key, slidingExpiration, out ex));
     exception = ex;
     return ret; 
 }
Пример #15
0
 public virtual string ExecutePostOrPut( PostOrPut method, string url, string key, IClientCache cache, out WebException exception)
 {
     WebException ex = null; 
     var ret = ExecuteWithCache(cache, url, key, (c, u) => ExecutePostOrPutAndCache(method, cache, url, key, out ex));
     exception = ex;
     return ret; 
 }
Пример #16
0
 public virtual void RequestAsync(string url, string key, IClientCache cache, TimeSpan slidingExpiration)
 {
     switch (Method)
     {
         case WebMethod.Get:
             ExecuteGetAsync(url, key, cache, slidingExpiration);
             break;
         case WebMethod.Post:
             ExecutePostOrPutAsync(PostOrPut.Post, url, key, cache, slidingExpiration);
             break;
         case WebMethod.Put:
             ExecutePostOrPutAsync(PostOrPut.Put, url, key, cache, slidingExpiration);
             break;
         case WebMethod.Delete:
             ExecuteDeleteAsync(url, key, cache, slidingExpiration);
             break;
         default:
             throw new NotSupportedException(
                 "Unsupported web method: {0}".FormatWith(Method.ToUpper())
                 );
     }
 }
 public static void Register(string extension, IClientCache cache)
 {
     InjectionClientCacheFactory.Instance.RegisterCache(extension, cache);
 }
Пример #18
0
        protected virtual IAsyncResult ExecuteDeleteAsync(string url, string prefixKey, IClientCache cache,
                                                          DateTime absoluteExpiration)
        {
            WebResponse = null;

            var request = BuildDeleteWebRequest(url);

            var state = new Pair<WebRequest, Pair<byte[], Triplet<IClientCache, DateTime, string>>>
                            {
                                First = request,
                                Second = new Pair<byte[], Triplet<IClientCache, DateTime, string>>
                                             {
                                                 First = null,
                                                 Second = new Triplet<IClientCache, DateTime, string>
                                                              {
                                                                  First = cache,
                                                                  Second = absoluteExpiration,
                                                                  Third = prefixKey
                                                              }
                                             }
                            };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Пример #19
0
        protected virtual IAsyncResult ExecuteDeleteAsync(string url, string key, IClientCache cache)
        {
            WebResponse = null;

            var request = BuildDeleteWebRequest(url);
            var state = new Pair<WebRequest, Triplet<byte[], IClientCache, string>>
                            {
                                First = request,
                                Second = new Triplet<byte[], IClientCache, string>
                                             {
                                                 First = null,
                                                 Second = cache,
                                                 Third = key
                                             }
                            };

            var args = new WebQueryRequestEventArgs(url);
            OnQueryRequest(args);

            return request.BeginGetRequestStream(PostAsyncRequestCallback, state);
        }
Пример #20
0
 private string ExecuteGetAndCacheWithExpiry(IClientCache cache, string url, string key,
                                             DateTime absoluteExpiration, out WebException exception)
 {
     var result = ExecuteGet(url, out exception );
     if (exception == null)
     {
         cache.Insert(CreateCacheKey(key, url), result, absoluteExpiration);
     }
     return result;
 }
Пример #21
0
 private string ExecuteGetAndCache(IClientCache cache, string url, string key, out WebException exception )
 {
     
     var result = ExecuteGet(url, out exception);
     if (exception == null)
     {
         cache.Insert(CreateCacheKey(key, url), result);
     }
     return result;
 }
Пример #22
0
 public virtual string ExecutePostOrPut(PostOrPut method, string url, string key, IClientCache cache, TimeSpan slidingExpiration, out WebException exception )
 {
     WebException ex = null; 
     var ret = ExecuteWithCacheAndSlidingExpiration(cache, url, key, slidingExpiration,
                                                 (c, u, e) =>
                                                 ExecutePostOrPutAndCacheWithExpiry(method, cache, url, key, slidingExpiration, out ex));
     exception = ex; 
     return ret; 
 }
Пример #23
0
 public virtual string Request(string url, string key, IClientCache cache, TimeSpan slidingExpiration, out WebException exception)
 {
     switch (Method)
     {
         case WebMethod.Get:
             return ExecuteGet(url, key, cache, slidingExpiration, out exception);
         case WebMethod.Put:
             return ExecutePostOrPut(PostOrPut.Put, url, key, cache, slidingExpiration, out exception);
         case WebMethod.Post:
             return ExecutePostOrPut(PostOrPut.Post, url, key, cache, slidingExpiration, out exception);
         case WebMethod.Delete:
             // todo implement delete with cache (for mocking and completeness)
             throw new NotImplementedException("HTTP DELETE not supported yet; use HTTP POST instead");
         default:
             throw new NotSupportedException("Unknown web method");
     }
 }
Пример #24
0
        protected virtual string ExecuteGet(string url, string key, IClientCache cache, out WebException exception)
        {
            WebException ex = null; 
            var ret = ExecuteWithCache(cache, url, key, (c, u) => ExecuteGetAndCache(cache, url, key, out ex));
            exception = ex;
            return ret; 

        }