我们最近安装了 Robohash 作为 Gravatar 的一个很好的后备:
http://static2.scirra.net/avatars/128/5df4bf5d460c9497fdb35578e923ad1f.png
如您所见,robohashes 非常出色,并且来自我们的静态域。URL实际上是重写的:
<action type="Rewrite" url="gravatar.ashx?hash={R:2}&size={R:1}" appendQueryString="false" />
在同一个 web.config 文件中,我们有缓存配置文件:
<staticContent>
<clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
</staticContent>
<caching>
<profiles>
<add extension=".ashx" policy="CacheForTimePeriod" kernelCachePolicy="DontCache" duration="01:00:00" />
<add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />
<add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />
<add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />
<add extension=".ico" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" location="Any" />
</profiles>
</caching>
为了更好地衡量 gravatar.ashx 文件,我们还设置了缓存策略:
<%@ WebHandler Language="C#" Class="GravatarImage" %>
using System;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net;
public class GravatarImage : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// Adds document content type
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(60));
context.Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0));
context.Response.AddHeader("Last-Modified", DateTime.Now.ToLongDateString());
// Get params and send initial request to gravatar
string Hash = context.Request.QueryString["hash"];
string Size = context.Request.QueryString["size"];
string URL = "http://www.gravatar.com/avatar/" + Hash + "?r=pg&s=" + Size + "&d=404";
// Make request to gravatar
bool Fetched = makeAvatarRequest(URL);
// Backup to robo hash
if (!Fetched)
{
URL = "http://robohash.org/" + Hash + ".png?size=" + Size + "x" + Size + "&bgset=bg2";
Fetched = makeAvatarRequest(URL);
}
// Fallback if gravatar doesn't match and robohash is down
if (!Fetched)
{
}
// Cache this handler response for 1 hour.
HttpCachePolicy c = context.Response.Cache;
c.SetCacheability(HttpCacheability.Public);
c.SetMaxAge(new TimeSpan(1, 0, 0));
}
// Attempt a request for avatar
private bool makeAvatarRequest(string URL)
{
try
{
WebRequest request = WebRequest.Create(URL);
using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
displayImage(responseStream);
return true;
}
}
}
catch (WebException ex)
{
return false;
}
}
// Display the image from stream
private void displayImage(Stream stream)
{
HttpContext.Current.Response.ContentType = "image/png";
Image img = Image.FromStream(stream);
MemoryStream temp = new MemoryStream();
img.Save(temp, ImageFormat.Png);
byte[] buffer = temp.GetBuffer();
HttpContext.Current.Response.OutputStream.Write(buffer, 0, buffer.Length);
img.Dispose();
temp.Dispose();
}
public bool IsReusable {
get {
return false;
}
}
}
注意我们在底部使用 Response.cache AND CachePolicy
当我使用 YSlow 时,页面上的每个图像都有一个未来的到期日期,除了这些没有到期日期的头像。每次请求页面时,都会再次获取它们。
该脚本的想法是从外部 URL 获取头像并将其缓存 1 小时。然后从我们的网站提供。
任何人都可以在这里帮助我们吗?使用没有缓存的头像的示例页面是: http ://www.scirra.com/forum/construct-in-ludum-dare-21_topic44523.html