Пример #1
0
        /// <summary>
        /// Perform a single POST requst containing multiple nested GET requests
        /// </summary>
        public Task <GetResponse[]> MultiGetAsync(GetRequest[] requests)
        {
            var multiGetOperation = new MultiGetOperation(this, convention, url, requests);

            var httpJsonRequest = jsonRequestFactory.CreateHttpJsonRequest(this, multiGetOperation.RequestUri, "POST",
                                                                           credentials, convention);

            var requestsForServer = multiGetOperation.PreparingForCachingRequest(jsonRequestFactory);

            var postedData = JsonConvert.SerializeObject(requestsForServer);

            if (multiGetOperation.CanFullyCache(jsonRequestFactory, httpJsonRequest, postedData))
            {
                var cachedResponses = multiGetOperation.HandleCachingResponse(new GetResponse[requests.Length], jsonRequestFactory);
                return(Task.Factory.StartNew(() => cachedResponses));
            }


            return(Task.Factory.FromAsync(httpJsonRequest.BeginWrite, httpJsonRequest.EndWrite, postedData, null)
                   .ContinueWith(
                       task =>
            {
                task.Wait();                                // will throw on error
                return httpJsonRequest.ReadResponseStringAsync()
                .ContinueWith(replyTask =>
                {
                    var responses = JsonConvert.DeserializeObject <GetResponse[]>(replyTask.Result);
                    return multiGetOperation.HandleCachingResponse(responses, jsonRequestFactory);
                })
                ;
            })
                   .Unwrap());
        }
Пример #2
0
        private async Task <bool> ExecuteLazyOperationsSingleStep(ResponseTimeInformation responseTimeInformation, List <GetRequest> requests, CancellationToken token = default)
        {
            var multiGetOperation = new MultiGetOperation(this);
            var multiGetCommand   = multiGetOperation.CreateRequest(requests);
            await RequestExecutor.ExecuteAsync(multiGetCommand, Context, sessionInfo : SessionInfo, token : token).ConfigureAwait(false);

            var responses = multiGetCommand.Result;

            for (var i = 0; i < PendingLazyOperations.Count; i++)
            {
                var response = responses[i];

                response.Headers.TryGetValue(Constants.Headers.RequestTime, out string tempReqTime);

                long.TryParse(tempReqTime, out long totalTime);

                responseTimeInformation.DurationBreakdown.Add(new ResponseTimeItem
                {
                    Url      = requests[i].UrlAndQuery,
                    Duration = TimeSpan.FromMilliseconds(totalTime)
                });

                if (response.RequestHasErrors())
                {
                    throw new InvalidOperationException("Got an error from server, status code: " + (int)response.StatusCode + Environment.NewLine + response.Result);
                }

                PendingLazyOperations[i].HandleResponse(response);
                if (PendingLazyOperations[i].RequiresRetry)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #3
0
        /// <summary>
        /// Retrieves multiple items from the cache.
        /// </summary>
        /// <param name="keys">The list of identifiers for the items to retrieve.</param>
        /// <param name="casValues">The CAS values for the keys.</param>
        /// <returns>a Dictionary holding all items indexed by their key.</returns>
        public IDictionary <string, object> Get(IEnumerable <string> keys, out IDictionary <string, ulong> casValues)
        {
            using (MultiGetOperation mgo = new MultiGetOperation(this.pool, keys))
            {
                mgo.Execute();

                casValues = mgo.CasValues ?? new Dictionary <string, ulong>();

                return(mgo.Result ?? new Dictionary <string, object>());
            }
        }
Пример #4
0
        private bool ExecuteLazyOperationsSingleStep(ResponseTimeInformation responseTimeInformation, List <GetRequest> requests, Stopwatch sw)
        {
            var multiGetOperation = new MultiGetOperation(this);

            using var multiGetCommand = multiGetOperation.CreateRequest(requests);
            RequestExecutor.Execute(multiGetCommand, Context, sessionInfo: SessionInfo);
            var responses = multiGetCommand.Result;

            if (multiGetCommand.AggressivelyCached == false)
            {
                IncrementRequestCount();
            }

            for (var i = 0; i < PendingLazyOperations.Count; i++)
            {
                long   totalTime;
                string tempReqTime;
                var    response = responses[i];

                response.Headers.TryGetValue(Constants.Headers.RequestTime, out tempReqTime);
                response.Elapsed = sw.Elapsed;

                long.TryParse(tempReqTime, out totalTime);

                responseTimeInformation.DurationBreakdown.Add(new ResponseTimeItem
                {
                    Url      = requests[i].UrlAndQuery,
                    Duration = TimeSpan.FromMilliseconds(totalTime)
                });

                if (response.RequestHasErrors())
                {
                    throw new InvalidOperationException("Got an error from server, status code: " + (int)response.StatusCode + Environment.NewLine + response.Result);
                }

                PendingLazyOperations[i].HandleResponse(response);
                if (PendingLazyOperations[i].RequiresRetry)
                {
                    return(true);
                }
            }
            return(false);
        }
Пример #5
0
		/// <summary>
		/// Perform a single POST requst containing multiple nested GET requests
		/// </summary>
		public Task<GetResponse[]> MultiGetAsync(GetRequest[] requests)
		{

			var multiGetOperation = new MultiGetOperation(this,  convention, url, requests);

			var httpJsonRequest = jsonRequestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(this, multiGetOperation.RequestUri, "POST", credentials, convention));

			var requestsForServer = multiGetOperation.PreparingForCachingRequest(jsonRequestFactory);

			var postedData = JsonConvert.SerializeObject(requestsForServer);

			if (multiGetOperation.CanFullyCache(jsonRequestFactory, httpJsonRequest, postedData))
			{
				var cachedResponses = multiGetOperation.HandleCachingResponse(new GetResponse[requests.Length], jsonRequestFactory)	;
				return Task.Factory.StartNew(() => cachedResponses);
			}


			return Task.Factory.FromAsync(httpJsonRequest.BeginWrite, httpJsonRequest.EndWrite, postedData, null)
				.ContinueWith(
					task =>
					{
						task.Wait();// will throw on error
						return httpJsonRequest.ReadResponseJsonAsync()
							.ContinueWith(replyTask =>
							{
								var responses = convention.CreateSerializer().Deserialize<GetResponse[]>(new RavenJTokenReader(replyTask.Result));
								return multiGetOperation.HandleCachingResponse(responses, jsonRequestFactory);
							})
						;
					})
					.Unwrap();

		}