-
Notifications
You must be signed in to change notification settings - Fork 6
/
TwitterProxy.ashx.cs
56 lines (43 loc) · 1.6 KB
/
TwitterProxy.ashx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
namespace HttpHandler_Proxy {
public class TwitterProxy : IHttpHandler {
public void ProcessRequest(HttpContext context) {
// This will be the case whether there's a cache hit or not.
context.Response.ContentType = "application/json";
string id = context.Request.QueryString["id"];
if (string.IsNullOrWhiteSpace(id))
id = "Encosia";
// Check to see if the twitter status is already cached,
// then retrieve and return the cached value if so.
object tweetsCache = context.Cache["tweets-" + id];
if (tweetsCache != null) {
string cachedTweets = tweetsCache.ToString();
context.Response.Write(cachedTweets);
// We're done here.
return;
}
WebClient twitter = new WebClient();
string requestUrl = "http://api.twitter.com/1/statuses/user_timeline.json?id=" + id;
string tweets = twitter.DownloadString(requestUrl);
// This monstrosity essentially just caches the WebClient result
// with a maximum lifetime of 5 minutes from now.
// If you don't care about the expiration, this can be a simple
// context.Cache["tweets"] = tweets; instead.
context.Cache.Add("tweets-" + id, tweets,
null, DateTime.Now.AddMinutes(5),
System.Web.Caching.Cache.NoSlidingExpiration,
System.Web.Caching.CacheItemPriority.Normal,
null);
context.Response.Write(tweets);
}
public bool IsReusable {
get {
return true;
}
}
}
}