/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="context"></param> /// <param name="absoluteExpiration"></param> public void Set(string key, NancyContext context, DateTime absoluteExpiration) { if (string.IsNullOrEmpty(key)) { return; } if (context.Response is Response response && absoluteExpiration > DateTime.UtcNow) { lock (_lock) { string fileName = Hash(key); if (File.Exists(Path.Combine(_cacheDirectory, fileName))) { return; } var serializedResponse = new SerializableResponse(context.Response, absoluteExpiration); string json = _javaScriptSerializer.Serialize(serializedResponse); File.WriteAllText(Path.Combine(_cacheDirectory, fileName), json); FileKeyExpirationRecord.Add(fileName, absoluteExpiration); } } DeleteExpiredCacheFiles(); }
public void Set(string key, NancyContext context, DateTime absoluteExpiration) { SetCache(); if (_cache == null) { return; } _cache[key] = new SerializableResponse(context.Response, absoluteExpiration); }
public CachedResponse(SerializableResponse response) { ContentType = response.ContentType; Headers = response.Headers; StatusCode = response.StatusCode; OldResponseOutput = response.Contents; Contents = GetContents(this.OldResponseOutput); Expiration = response.Expiration; Headers["X-Nancy-LightningCache-Expiration"] = response.Expiration.ToString(CultureInfo.InvariantCulture); }
protected SerializableResponse JsonResponse(Object data) { var sr = new SerializableResponse { ContentType = "application/json" }; sr.Headers["Date"] = DateTime.Now.ToString("r"); sr.Headers["Last-Modified"] = DateTime.Now.ToString("r"); sr.Content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data)); return(sr); }
/// <summary> /// Sets the value from the Concurrent Dictionary using the key provided. /// Also includes an expiration date. /// </summary> /// <param name="key">The unique key provided by the client</param> /// <param name="context">The nancy context that contains the Response</param> /// <param name="absoluteExpiration">The expiration of the value on the dictionary</param> public void Set(string key, NancyContext context, DateTime absoluteExpiration) { if (string.IsNullOrEmpty(key)) { return; } if (context.Response is Response response && absoluteExpiration > DateTime.UtcNow) { _cache[key] = new SerializableResponse(response, absoluteExpiration); } }
public void Serializable_response_created_empty() { //Arrange var fakeResponse = new FakeResponse(); //Act var serializableResponse = new SerializableResponse(); //Assert Assert.NotNull(serializableResponse); Assert.Null(serializableResponse.ContentType); Assert.Null(serializableResponse.Headers); Assert.Null(serializableResponse.Contents); }
public void Serializable_response_created() { //Arrange var fakeResponse = new FakeResponse(); //Act var serializableResponse = new SerializableResponse(fakeResponse, expirationDate); //Assert Assert.NotNull(serializableResponse); Assert.Equal(fakeResponse.ContentType, serializableResponse.ContentType); Assert.Equal(fakeResponse.Headers, serializableResponse.Headers); Assert.Equal(fakeResponse.StatusCode, serializableResponse.StatusCode); Assert.Equal(fakeResponse.GetContents(), serializableResponse.Contents); }
public void Set(string key, NancyContext context, DateTime absoluteExpiration) { var span = absoluteExpiration - DateTime.UtcNow; using (var bucket = _cluster.OpenBucket(_bucketname)) { var serialize = new SerializableResponse(context.Response, absoluteExpiration); var result = bucket.Upsert(key, serialize, span); if (!result.Success) { throw new Exception($"Could not complete operation of Upsert, using cluster configuration: {_cluster.Configuration}"); } } }
/// <summary> /// Sets the response of the context object as a serialized cached into the cache. /// </summary> /// <param name="key"></param> /// <param name="context"></param> /// <param name="absoluteExpiration"></param> public void Set(string key, NancyContext context, DateTime absoluteExpiration) { var span = absoluteExpiration - DateTime.UtcNow; if (context.Response is Response && span.TotalSeconds > 0) { var serialize = new SerializableResponse(context.Response, absoluteExpiration); bool ack = _cache.StringSet(key, Serialize(serialize), expiry: span); if (!ack) { throw new Exception($"Could not complete operation of Set, using redis configuration: {_redis.Configuration}"); } } }
/// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="context"></param> /// <param name="absoluteExpiration"></param> public void Set(string key, NancyContext context, DateTime absoluteExpiration) { lock (_lock) { var fileName = Hash(key); if (File.Exists(Path.Combine(_cacheDirectory, fileName))) { File.Delete(Path.Combine(_cacheDirectory, fileName)); } var serializedResponse = new SerializableResponse(context.Response, absoluteExpiration); var json = _javaScriptSerializer.Serialize(serializedResponse); File.WriteAllText(Path.Combine(_cacheDirectory, fileName), json); DeleteExpiredCacheFiles(); FileKeyExpirationRecord[fileName] = absoluteExpiration; } }
protected SerializableResponse RemoteRequest(String realUrl, SerializableRequest sr) { if (sr.QueryParams.ContainsKey("runlocal")) { throw new Exception(); } //throw new Exception(); var res = new SerializableResponse(); var method = HttpMethod.Get; if (sr.Method == "GET") { method = HttpMethod.Get; } if (sr.Method == "POST") { method = HttpMethod.Post; } if (sr.Method == "PUT") { method = HttpMethod.Put; } if (sr.Method == "DELETE") { method = HttpMethod.Delete; } var qp = ""; if (sr.QueryParams.Any()) { qp = "?" + string.Join("&", sr.QueryParams.Select(kvp => kvp.Key + "=" + kvp.Value)); } var requestMessage = new HttpRequestMessage(method, realUrl + qp); foreach (var reqh in sr.Headers) { if (reqh.Key == "Host") { var uri = new Uri(realUrl); requestMessage.Headers.Add(reqh.Key, uri.Host); } else { requestMessage.Headers.Add(reqh.Key, reqh.Value); } } if (sr.Method == "POST" || sr.Method == "PUT") { requestMessage.Content = new ByteArrayContent(sr.Content); } HttpClientHandler handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }; var client = new HttpClient(handler); var result = client.SendAsync(requestMessage); result.Wait(); var resh = result.Result; res.ContentType = resh.Content.Headers.ContentType.MediaType; res.Headers["Date"] = DateTime.Now.ToString("r"); res.Headers["Last-Modified"] = DateTime.Now.ToString("r"); var rb = resh.Content.ReadAsByteArrayAsync(); rb.Wait(); res.Content = rb.Result; return(res); }