Jan Sommer
Jan Sommer

Reputation: 3798

Store Stream in cache

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?

Upvotes: 5

Views: 5357

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1063058

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).

Upvotes: 8

Dave
Dave

Reputation: 14178

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.

Upvotes: 2

Related Questions