A good reason to make a post!

Nothing really to say here, I just wanted to force an update to my blog so that a HttpCacheDependency class (derived from Whidbey’s CacheDependency class) had something to check for updates to trigger a cache expiry (update: It worked, code attached).

public class HttpCacheDependency : System.Web.Caching.CacheDependency
{
    private string m_ContentOnFile;
    private string m_Url;
    public HttpCacheDependency(string url)
    {
        this.m_Url = url;
        this.m_ContentOnFile = this.GetContent(url);

        TimerCallback callback = new TimerCallback(this.m_Timer_Callback);
        this.m_Timer = new Timer(
callback, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
    }

    private Timer m_Timer;
    private void m_Timer_Callback(object obj)
    {
        string newContent = this.GetContent(this.m_Url);
   
        if (this.m_ContentOnFile != newContent)
        {
            this.m_ContentOnFile = newContent;
            this.NotifyDependencyChanged(this, new EventArgs());
        }
    }

    private string GetContent(string url)
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url
);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string content = reader.ReadToEnd();

        return content;
    }
}

I can imagine that this would be useful for things like those web-based RSS feeders or other content aggregation services.

2 thoughts on “A good reason to make a post!

  1. Clarke Scott

    (I a Monty Python voice)

    “You lucky bastard. You lucky, lucky bastard.”

    Can’t wait for Whidbey!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s