public void AddOrUpdate(CacheKey key, HttpResponseMessage response) { var fileName = key.EnsureFolderAndGetFileName(_dataRoot); if (File.Exists(fileName)) { TraceWriter.WriteLine("Must remove file", TraceLevel.Verbose); TryRemove(key); } var ms = new MemoryStream(); _serializer.SerializeAsync(TaskHelpers.FromResult(response), ms).Wait(); ms.Position = 0; using (var fs = GetFile(fileName, FileMode.Create)) { TraceWriter.WriteLine("Before serialise", TraceLevel.Verbose); ms.CopyTo(fs); TraceWriter.WriteLine("After serialise", TraceLevel.Verbose); } var info = new FileInfo(fileName); // Update database _database.Cache .Insert(new CacheItem { Domain = key.Domain, Hash = Convert.ToBase64String(key.Hash), LastAccessed = DateTime.Now, LastUpdated = (response.Content != null) && response.Content.Headers.LastModified.HasValue ? response.Content.Headers.LastModified.Value.UtcDateTime : DateTime.Now , Size = info.Length }); TraceWriter.WriteLine("After db update", TraceLevel.Verbose); // tell quota manager _quotaManager.ItemAdded(new CacheItemMetadata { Domain = key.Domain, Key = key.Hash, LastAccessed = DateTime.Now, Size = info.Length }); }
public bool TryGetValue(CacheKey key, out HttpResponseMessage response) { response = null; string fileName = key.EnsureFolderAndGetFileName(_dataRoot); if (File.Exists(fileName)) { var ms = new MemoryStream(); using (var fs = GetFile(fileName, FileMode.Open)) { TraceWriter.WriteLine("TryGetValue - before DeserializeToResponseAsync", TraceLevel.Verbose); fs.CopyTo(ms); ms.Position = 0; } response = _serializer.DeserializeToResponseAsync(ms).Result; TraceWriter.WriteLine("TryGetValue - After DeserializeToResponseAsync", TraceLevel.Verbose); if (response.Content != null) { var task = response.Content.LoadIntoBufferAsync(); task.Wait(); TraceWriter.WriteLine("TryGetValue - After wait", TraceLevel.Verbose); } _database.Cache .UpdateByHash(new { Hash = Convert.ToBase64String(key.Hash), LastAccessed = DateTime.Now }); TraceWriter.WriteLine("After updating Last Accessed", TraceLevel.Verbose); } return(response != null); }