internal static long EstimateCachedResponseSize(CachedResponse cachedResponse) { if (cachedResponse == null) { return(0L); } checked { // StatusCode long size = sizeof(int); // Headers if (cachedResponse.Headers != null) { foreach (var item in cachedResponse.Headers) { size += (item.Key.Length * sizeof(char)) + EstimateStringValuesSize(item.Value); } } // Body if (cachedResponse.Body != null) { size += cachedResponse.Body.Length; } return(size); } }
public async Task Http_context_maps_if_none_match() { //Arrange const string fakeEtag = "84de625db71b56d480d47bdc32377d23144b8c65"; var fakeIfNoneMatch = new Microsoft.Extensions.Primitives.StringValues(fakeEtag); var context = A.Fake <HttpContext>(); var response = A.Fake <HttpResponse>(); var request = A.Fake <HttpRequest>(); A.CallTo(() => request.Headers.TryGetValue(HeaderNames.IfNoneMatch, out fakeIfNoneMatch)) .Returns(true); A.CallTo(() => context.Request).Returns(request); A.CallTo(() => response.Headers.ContainsKey(HeaderNames.ETag)).Returns(true); A.CallTo(() => response.Headers[HeaderNames.ETag]).Returns(fakeEtag); A.CallTo(() => context.Response).Returns(response); //Act var cachedResponse = new CachedResponse(context, Array.Empty <byte>()); await cachedResponse.MapToContext(context); //Assert Assert.Equal(cachedResponse.Headers[HeaderNames.ETag], response.Headers[HeaderNames.ETag]); Assert.Equal(StatusCodes.Status304NotModified, response.StatusCode); Assert.Equal(0, response.ContentLength); }
/// <summary> /// Adds the current response to the cache if required /// Only stores by Path and stores the response in a dictionary. /// Do not use this as an actual cache :-) /// </summary> /// <param name="context">Current context</param> private void SetCache(NancyContext context) { if (context.Response.StatusCode != HttpStatusCode.OK) { return; } object cacheSecondsObject; if (!context.Items.TryGetValue(ContextExtensions.OUTPUT_CACHE_TIME_KEY, out cacheSecondsObject)) { return; } int cacheSeconds; if (!int.TryParse(cacheSecondsObject.ToString(), out cacheSeconds)) { return; } var cachedResponse = new CachedResponse(context.Response); _cachedResponses[context.Request.Path] = new Tuple <DateTime, Response, int>(DateTime.Now, cachedResponse, cacheSeconds); context.Response = cachedResponse; }
public void Memcached_cache_with_value_set_get() { //Arrange var client = GetMemcachedClient(); var cache = new MemcachedStore(client); var expiration = new TimeSpan(0, 1, 0); const string key = "Random-Key-4"; const string contentType = "application/json"; const int statusCode = 200; var response = new CachedResponse { ContentType = contentType, Expiry = expiration, StatusCode = statusCode, ContentLength = 1, }; //Act cache.Set(key, response, expiration); bool found = cache.TryGetValue(key, out var getResponse); //Assert Assert.Equal(response.ContentType, getResponse.ContentType); Assert.Equal(response.StatusCode, getResponse.StatusCode); Assert.Equal(response.Expiry, getResponse.Expiry); Assert.Equal(1, response.ContentLength); Assert.True(found); }
/// <summary> /// Constructor: Cache-entry overload /// </summary> /// <param name="uri"></param> /// <param name="innerUri"></param> /// <param name="partName"></param> /// <param name="cacheEntry">entry from cache</param> /// <param name="cachedPackageIsThreadSafe">is entry thread safe?</param> internal PackWebResponse(Uri uri, Uri innerUri, Uri partName, Package cacheEntry, bool cachedPackageIsThreadSafe) { _lockObject = new Object(); // required for synchronization if (uri == null) throw new ArgumentNullException("uri"); if (innerUri == null) throw new ArgumentNullException("innerUri"); if (partName == null) throw new ArgumentNullException("partName"); if (cacheEntry == null) throw new ArgumentNullException("cacheEntry"); #if DEBUG if (PackWebRequestFactory._traceSwitch.Enabled) System.Diagnostics.Trace.TraceInformation( DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond + " " + System.Threading.Thread.CurrentThread.ManagedThreadId + ": " + "PackWebResponse - Creating response from Package Cache"); #endif _uri = uri; _innerUri = innerUri; _partName = partName; // may not be null _mimeType = null; // until we find out the real value // delegate work to private class _cachedResponse = new CachedResponse(this, cacheEntry, cachedPackageIsThreadSafe); }
public bool SaveToCache() { if (_responseCapture != null) { _rule = _rule ?? GetMatchingRule(); if (_rule != null && _rule.ServerCacheTime.HasValue && _rule.ServerCacheTime.Value > TimeSpan.Zero) { if (_cachedResponse == null) { _cachedResponse = new CachedResponse(); } var buffer = _responseCapture.OutputBuffer; if (buffer != null) { _cachedResponse.CachedContent = new byte[buffer.Length]; buffer.CopyTo(_cachedResponse.CachedContent, 0); _cachedResponse.Headers = _context.Response.Headers.Keys .Where(n => !_noCacheHeaders.Contains(n.ToLower())) .Select(n => new CachedHeader { HeaderName = n, HeaderValue = _context.Response.Headers[n] }) .ToArray(); _cachedResponse.WhenCached = DateTime.UtcNow; _cache.Put(_cacheKey, _cachedResponse, _rule.ServerCacheTime.Value, _rule.CacheCategory); return(true); } } } return(false); }
public static SerializableCachedResponse From(CachedResponse cachedResponse) { if (cachedResponse == null) { return(null); } var response = new SerializableCachedResponse { Created = cachedResponse.Created, StatusCode = cachedResponse.StatusCode, Headers = cachedResponse.Headers .Select(header => new KeyValuePair <string, List <string> >(header.Key, header.Value.ToList())) .ToList() }; if (cachedResponse.Body != null) { using var stream = new MemoryStream(); cachedResponse.Body.CopyTo(stream); response.Body = stream.ToArray(); } return(response); }
private void GivenThereIsACachedResponse(CachedResponse response) { _response = response; _cache .Setup(x => x.Get(It.IsAny <string>(), It.IsAny <string>())) .Returns(_response); }
internal HttpResponseMessage CreateHttpResponseMessage(CachedResponse cached) { if (cached == null) { return(null); } var response = new HttpResponseMessage(cached.StatusCode); foreach (var header in cached.Headers) { response.Headers.Add(header.Key, header.Value); } var content = new MemoryStream(Convert.FromBase64String(cached.Body)); response.Content = new StreamContent(content); foreach (var header in cached.ContentHeaders) { response.Content.Headers.Add(header.Key, header.Value); } return(response); }
public async Task <GeocodeResponse[]> Search(string searchText) { var normalizedSearchText = searchText.ToLower(); var cacheResponse = _cacheStore.Get(searchText); if (cacheResponse == null) { var response = await RemoteSearch(normalizedSearchText); _cacheStore.CacheResponse(response, normalizedSearchText); return(response); } if (_cacheStore.IsActualCache(cacheResponse)) { return(await Task.FromResult(cacheResponse.Content)); } var geocodeResponses = await RemoteSearch(normalizedSearchText); var actualCache = new CachedResponse { Id = cacheResponse.Id, SearchText = cacheResponse.SearchText, CreatedAt = DateTime.Now, Content = geocodeResponses }; _cacheStore.UpdateCachedResponse(cacheResponse.Id, actualCache); return(geocodeResponses); }
internal async Task <CachedResponse> CreateCachedResponse(HttpResponseMessage response) { if (response == null) { return(null); } var statusCode = response.StatusCode; var headers = response.Headers.ToDictionary(v => v.Key, v => v.Value); string body = null; if (response.Content != null) { var content = await response.Content.ReadAsByteArrayAsync(); body = Convert.ToBase64String(content); } var contentHeaders = response?.Content?.Headers.ToDictionary(v => v.Key, v => v.Value); var cached = new CachedResponse(statusCode, headers, body, contentHeaders); return(cached); }
public async Task SetCache(HttpContext context, CachedResponse response, CachingOption options) { string key = options.Key.Get(context.Request); options.Store.Set(key, response, response.Expiry); await Task.CompletedTask; }
public SerializableCachedResponse(CachedResponse cachedResponse) { StatusCode = cachedResponse.StatusCode; Headers = cachedResponse.Headers; ContentHeaders = cachedResponse.ContentHeaders; Body = cachedResponse.Body; ReasonPhrase = cachedResponse.ReasonPhrase; }
public string GetCache(CachedResponse response) { var path = cachePath (response); if (File.Exists(path)) { return File.ReadAllText (path); } return null; }
public IEnumerable <byte> Serialize(CachedResponse response) { var ms = new MemoryStream(); var bs = new BinaryFormatter(); var serializableCachedResponse = new SerializableCachedResponse(response); bs.Serialize(ms, serializableCachedResponse); return(ms.ToArray()); }
private void CacheResponse(HttpContent content) { var cachedResponse = new CachedResponse { Body = content.ReadAsStringAsync().Result, ContentType = content.Headers.ContentType.ToString() }; OutputCacheAttribute.cache.Put(this.cacheKey, cachedResponse, this.timeout); }
public void should_returned_cached_item_when_it_is_in_cache() { var cachedResponse = new CachedResponse(HttpStatusCode.OK, new Dictionary <string, IEnumerable <string> >(), "", new Dictionary <string, IEnumerable <string> >()); this.Given(x => x.GivenThereIsACachedResponse(cachedResponse)) .And(x => x.GivenTheDownstreamRouteIs()) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheCacheGetIsCalledCorrectly()) .BDDfy(); }
public CachedResponse Deserialize(byte[] response) { var ms = new MemoryStream(response); var bs = new BinaryFormatter(); var deserializedCachedValue = bs.Deserialize(ms); var cachedValue = (SerializableCachedResponse)deserializedCachedValue; var value = new CachedResponse(cachedValue.StatusCode, cachedValue.Headers, cachedValue.Body, cachedValue.ContentHeaders, cachedValue.ReasonPhrase); return(value); }
public void should_returned_cached_item_when_it_is_in_cache() { var cachedResponse = new CachedResponse(); this.Given(x => x.GivenThereIsACachedResponse(cachedResponse)) .And(x => x.GivenTheDownstreamRouteIs()) .And(x => x.GivenThereIsADownstreamUrl()) .When(x => x.WhenICallTheMiddleware()) .Then(x => x.ThenTheCacheGetIsCalledCorrectly()) .BDDfy(); }
public void AddRequestTest() { var cachedResponse = new CachedResponse <string, string>(); Repeater repeater = new Repeater() { CachedResponse = cachedResponse, }; repeater.Add(5, "Test", () => ""); Assert.IsTrue(cachedResponse.ContainsKey("Test")); }
public void Cached_response_maps() { //Arrange var context = A.Fake <HttpContext>(); var props = new CachingProperty(); //Act var cachedResponse = new CachedResponse(context, Array.Empty <byte>()); //Assert Assert.True(cachedResponse.Headers.ContainsKey(props.CustomHeader)); }
public void CacheResponse(GeocodeResponse[] geocodeResponse, string searchText) { var cacheResponse = new CachedResponse { Id = ObjectId.GenerateNewId(), SearchText = searchText, CreatedAt = DateTime.Now, Content = geocodeResponse }; _cachedResponses.InsertOne(cacheResponse); }
public void Cached_response_maps_body() { //Arrange var context = A.Fake <HttpContext>(); byte[] input = Encoding.UTF8.GetBytes("Hello"); //Act var cachedResponse = new CachedResponse(context, input); //Assert Assert.Equal(cachedResponse.Body, input); }
public void Cached_response_maps_null_property() { //Arrange var context = A.Fake <HttpContext>(); byte[] input = Encoding.UTF8.GetBytes("Hello"); A.CallTo(() => context.Features.Get <CachingProperty>()).Returns(null); //Act var cachedResponse = new CachedResponse(context, input); //Assert Assert.Null(cachedResponse.Body); }
private void CacheResponse(HttpContent content) { if (content != null) { var cachedResponse = new CachedResponse { Body = content.ReadAsStringAsync().Result, ContentType = content.Headers.ContentType.ToString() }; //OutputCacheAttribute.Cache.Put(this.cacheKey, cachedResponse, this.timeout); OutputCacheAttribute.Cache.Add(this.cacheKey, cachedResponse, DateTime.Now.Add(this.timeout)); } }
public void CachedResponse_response_created() { //Arrange var fakeResponse = new FakeResponse(); //Act var cachedResponse = new CachedResponse(new SerializableResponse(fakeResponse, expirationDate)); //Assert Assert.NotNull(cachedResponse); Assert.Equal(fakeResponse.ContentType, cachedResponse.ContentType); Assert.Equal(fakeResponse.Headers, cachedResponse.Headers); Assert.Equal(fakeResponse.StatusCode, cachedResponse.StatusCode); Assert.Equal(fakeResponse.GetContents(), cachedResponse.Contents.ConvertStream()); }
public void Cached_response_maps_content_type() { //Arrange var context = A.Fake <HttpContext>(); var response = A.Fake <HttpResponse>(); A.CallTo(() => response.ContentType).Returns("application/json"); A.CallTo(() => context.Response).Returns(response); //Act var cachedResponse = new CachedResponse(context, Array.Empty <byte>()); //Assert Assert.Equal(cachedResponse.ContentType, response.ContentType); }
public void Cached_response_maps_status_code() { //Arrange var context = A.Fake <HttpContext>(); var response = A.Fake <HttpResponse>(); A.CallTo(() => response.StatusCode).Returns(200); A.CallTo(() => context.Response).Returns(response); //Act var cachedResponse = new CachedResponse(context, Array.Empty <byte>()); //Assert Assert.Equal(cachedResponse.StatusCode, response.StatusCode); }
/// <summary> /// Handle the response. /// </summary> /// <returns>an instance of the DataServiceResponse, containing individual responses for all the requests made during this SaveChanges call.</returns> protected override DataServiceResponse HandleResponse() { List <OperationResponse> responses = new List <OperationResponse>(this.cachedResponses != null ? this.cachedResponses.Count : 0); DataServiceResponse service = new DataServiceResponse(null, -1, responses, false /*isBatch*/); Exception ex = null; try { foreach (CachedResponse response in this.cachedResponses) { Descriptor descriptor = response.Descriptor; this.SaveResultProcessed(descriptor); OperationResponse operationResponse = new ChangeOperationResponse(response.Headers, descriptor); operationResponse.StatusCode = (int)response.StatusCode; if (response.Exception != null) { operationResponse.Error = response.Exception; if (ex == null) { ex = response.Exception; } } else { this.cachedResponse = response; #if DEBUG this.HandleOperationResponse(descriptor, response.Headers, response.StatusCode); #else this.HandleOperationResponse(descriptor, response.Headers); #endif } responses.Add(operationResponse); } } catch (InvalidOperationException e) { ex = e; } if (ex != null) { throw new DataServiceRequestException(Strings.DataServiceException_GeneralError, ex, service); } return(service); }
public async Task Invoke(HttpContext context) { if (context.Request.Method != "GET") { await _next(context); return; } var cacheKey = ("output:" + context.Request.Path.Value).ToLowerInvariant(); CachedResponse cachedResponse; if (_cache.TryGet(cacheKey, out cachedResponse)) { context.Response.Headers["X-LC-OutputCache-Key"] = cacheKey; context.Response.ContentType = cachedResponse.ContentType; context.Response.StatusCode = cachedResponse.StatusCode; await context.Response.WriteAsync(cachedResponse.Content); return; } using (var responseStream = new MemoryStream()) { var originalStream = context.Response.Body; context.Response.Body = responseStream; await _next(context); responseStream.Seek(0, SeekOrigin.Begin); await responseStream.CopyToAsync(originalStream); responseStream.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(responseStream)) { var content = reader.ReadToEnd(); cachedResponse = new CachedResponse(context.Response.ContentType, content, context.Response.StatusCode); _cache.Set(cacheKey, cachedResponse, new CacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(10))); } } }
public void Cached_response_maps_expiry() { //Arrange var timeSpan = new TimeSpan(0, 0, 5); var context = A.Fake <HttpContext>(); var props = A.Fake <CachingProperty>(); props.Expiration = timeSpan; A.CallTo(() => context.Features.Get <CachingProperty>()).Returns(props); //Act var cachedResponse = new CachedResponse(context, Array.Empty <byte>()); //Assert Assert.Equal(timeSpan, cachedResponse.Expiry); }
private static DownstreamResponse CreateHttpResponseMessage(CachedResponse cached) { if (cached == null) { return(null); } var streamContent = new GrpcHttpContent(cached.Body); foreach (var(key, value) in cached.ContentHeaders) { streamContent.Headers.TryAddWithoutValidation(key, value); } return(new DownstreamResponse(streamContent, cached.StatusCode, cached.Headers.ToList(), cached.ReasonPhrase)); }
public async Task Service_set_cache_default_with_request_body() { //Arrange const string scheme = "http"; const string host = "localhost"; const int port = 80; const string path = "/hello"; byte[] body = Encoding.UTF8.GetBytes("{\"Response\": \"Hello world\"}"); const int elapsedSeconds = 3; var fakeSpan = TimeSpan.FromSeconds(elapsedSeconds); var ctx = A.Fake <HttpContext>(); var option = A.Fake <CachingOption>(); var response = A.Fake <HttpResponse>(); var service = A.Fake <CarterCachingService>(); var request = A.Fake <HttpRequest>(); var headers = A.Fake <IHeaderDictionary>(); var props = A.Fake <CachingProperty>(); props.Expiration = TimeSpan.FromSeconds(elapsedSeconds); A.CallTo(() => ctx.Features.Get <CachingProperty>()).Returns(props); A.CallTo(() => ctx.Request).Returns(request); A.CallTo(() => ctx.Response).Returns(response); A.CallTo(() => request.Scheme).Returns(scheme); A.CallTo(() => request.Host).Returns(new HostString(host, port)); A.CallTo(() => request.Path).Returns(path); A.CallTo(() => response.Headers).Returns(headers); A.CallTo(() => response.StatusCode).Returns(StatusCodes.Status200OK); A.CallTo(() => response.Headers.ContainsKey(HeaderNames.ETag)).Returns(true); A.CallTo(() => response.Headers[HeaderNames.ETag]).Returns(ctx.CalculateChecksum(body)); var cachedResponse = new CachedResponse(ctx, body); //Act await service.SetCache(ctx, cachedResponse, option); bool cacheHit = await service.CheckCache(ctx, option); //Assert Assert.True(cacheHit); Assert.True(ctx.Response.Headers.ContainsKey(HeaderNames.ETag)); }
private void CacheSet(string path, CachedResponse data) { cache[path] = data; }
/// <summary> /// Handle the response. /// </summary> /// <returns>an instance of the DataServiceResponse, containing individual responses for all the requests made during this SaveChanges call.</returns> protected override DataServiceResponse HandleResponse() { List<OperationResponse> responses = new List<OperationResponse>(this.cachedResponses != null ? this.cachedResponses.Count : 0); DataServiceResponse service = new DataServiceResponse(null, -1, responses, false /*isBatch*/); Exception ex = null; try { foreach (CachedResponse response in this.cachedResponses) { Descriptor descriptor = response.Descriptor; this.SaveResultProcessed(descriptor); OperationResponse operationResponse = new ChangeOperationResponse(response.Headers, descriptor); operationResponse.StatusCode = (int)response.StatusCode; if (response.Exception != null) { operationResponse.Error = response.Exception; if (ex == null) { ex = response.Exception; } } else { this.cachedResponse = response; #if DEBUG this.HandleOperationResponse(descriptor, response.Headers, response.StatusCode); #else this.HandleOperationResponse(descriptor, response.Headers); #endif } responses.Add(operationResponse); } } catch (InvalidOperationException e) { ex = e; } if (ex != null) { throw new DataServiceRequestException(Strings.DataServiceException_GeneralError, ex, service); } return service; }
public void PersistCache(CachedResponse response, string contents) { File.WriteAllText (cachePath (response), contents); }
private string cachePath(CachedResponse response) { var documentsPath = Environment.GetFolderPath (Environment.SpecialFolder.Personal); var filePath = Path.Combine (documentsPath, Enum.GetName(typeof(CachedResponse), response)); return filePath; }
private Task SendResponse(CachedResponse cachedResponse, IOwinContext ctx) { return ProcessResponseStream(cachedResponse, cachedResponse.CreateBodyStream(), ctx); }
protected override DataServiceResponse HandleResponse() { List<OperationResponse> list = new List<OperationResponse>((this.cachedResponses != null) ? this.cachedResponses.Count : 0); DataServiceResponse response = new DataServiceResponse(null, -1, list, false); Exception innerException = null; try { foreach (CachedResponse response2 in this.cachedResponses) { Descriptor descriptor = response2.Descriptor; base.SaveResultProcessed(descriptor); OperationResponse item = new ChangeOperationResponse(response2.Headers, descriptor) { StatusCode = (int) response2.StatusCode }; if (response2.Exception != null) { item.Error = response2.Exception; if (innerException == null) { innerException = response2.Exception; } } else { this.cachedResponse = response2; base.HandleOperationResponse(descriptor, response2.Headers); } list.Add(item); } } catch (InvalidOperationException exception2) { innerException = exception2; } if (innerException != null) { throw new DataServiceRequestException(System.Data.Services.Client.Strings.DataServiceException_GeneralError, innerException, response); } return response; }