Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store Stream in cache

Tags:

c#

caching

I'd like to cache the response I get from a HttpWebRequest. I need both the ResponseStream and the headers. Using HttpRequestCachePolicy(HttpCacheAgeControl.MaxAge, TimeSpan.FromDays(1)) when creating the request doesn't seem to work (IsFromCache is never true for some reason), and I'm a bit scared of manually caching the entire HttpWebResponse since it's containing a stream of gzipped data, and I have a bad feeling about storing streams in the ASP.NET cache.

The response is mapped to an object like this (simplified):

public readonly Stream Response;
public readonly string Etag;

private MyObject(Stream response, string etag)
{
    this.Response = response;
    this.Etag = etag;
}

Since the object also contains the response stream I face the same issue here.

How do I cache this?

like image 985
Jan Sommer Avatar asked Oct 02 '12 13:10

Jan Sommer


2 Answers

A Stream is a pipe, not a bucket. If you need to store it, you must first get the actual contents. For example, you could read it all into a byte[]. For example:

using(var ms = new MemoryStream()) {
    response.CopyTo(ms);
    byte[] payload = ms.ToArray();
}

You can store the byte[] trivially. Then if you want a Stream later, you can use new MemoryStream(payload).

like image 159
Marc Gravell Avatar answered Oct 03 '22 17:10

Marc Gravell


Can you create a custom class that contains a byte array for storing the data and another field with HttpWebRequest.Headers for the headers? Then cache that.

like image 28
Dave Avatar answered Oct 03 '22 17:10

Dave