Commit c7154bb0 authored by Matt Tucker's avatar Matt Tucker Committed by matt

Improved favicon caching (JM-652).

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3804 b35dd754-fafc-0310-a699-88a17e54d16e
parent d49bd645
...@@ -527,6 +527,10 @@ public class Cache<K, V> implements Map<K, V> { ...@@ -527,6 +527,10 @@ public class Cache<K, V> implements Map<K, V> {
long[] array = (long[])object; long[] array = (long[])object;
return CacheSizes.sizeOfObject() + array.length * CacheSizes.sizeOfLong(); return CacheSizes.sizeOfObject() + array.length * CacheSizes.sizeOfLong();
} }
else if (object instanceof byte[]) {
byte [] array = (byte[])object;
return CacheSizes.sizeOfObject() + array.length;
}
// Default behavior -- serialize the object to determine its size. // Default behavior -- serialize the object to determine its size.
else { else {
int size = 1; int size = 1;
......
...@@ -22,10 +22,7 @@ import javax.servlet.ServletOutputStream; ...@@ -22,10 +22,7 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
...@@ -46,7 +43,7 @@ public class FaviconServlet extends HttpServlet { ...@@ -46,7 +43,7 @@ public class FaviconServlet extends HttpServlet {
/** /**
* The content-type of the images to return. * The content-type of the images to return.
*/ */
private static final String CONTENT_TYPE = "image/jpeg"; private static final String CONTENT_TYPE = "image/x-icon";
/** /**
* Bytes of the default favicon to return when one was not found on a host. * Bytes of the default favicon to return when one was not found on a host.
*/ */
...@@ -58,8 +55,11 @@ public class FaviconServlet extends HttpServlet { ...@@ -58,8 +55,11 @@ public class FaviconServlet extends HttpServlet {
/** /**
* Cache the domains that a favicon was not found. * Cache the domains that a favicon was not found.
*/ */
private Cache<String, Boolean> missesCache; private Cache<String, Integer> missesCache;
/**
* Cache the favicons that we've found.
*/
private Cache<String, byte[]> hitsCache;
public void init(ServletConfig config) throws ServletException { public void init(ServletConfig config) throws ServletException {
super.init(config); super.init(config);
...@@ -77,19 +77,21 @@ public class FaviconServlet extends HttpServlet { ...@@ -77,19 +77,21 @@ public class FaviconServlet extends HttpServlet {
e.printStackTrace(); e.printStackTrace();
} }
// Initialize caches. // Initialize caches.
missesCache = CacheManager.initializeCache("Favicon misses", "favicon", 128 * 1024); missesCache = CacheManager.initializeCache("Favicon misses", "faviconMisses", 128 * 1024);
hitsCache = CacheManager.initializeCache("Favicon hits", "faviconHits", 128 * 1024);
} }
/** /**
* Retrieve the image based on it's name. * Retrieve the image based on it's name.
* *
* @param request the httpservletrequest. * @param request the httpservletrequest.
* @param response the httpservletresponse. * @param response the httpservletresponse.
* @throws javax.servlet.ServletException * @throws javax.servlet.ServletException
* @throws java.io.IOException * @throws java.io.IOException
*/ */
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException
{
String host = request.getParameter("host"); String host = request.getParameter("host");
// Check special cases where we need to change host to get a favicon // Check special cases where we need to change host to get a favicon
host = "gmail.com".equals(host) ? "google.com" : host; host = "gmail.com".equals(host) ? "google.com" : host;
...@@ -127,18 +129,33 @@ public class FaviconServlet extends HttpServlet { ...@@ -127,18 +129,33 @@ public class FaviconServlet extends HttpServlet {
* @return the image bytes found, otherwise null. * @return the image bytes found, otherwise null.
*/ */
private byte[] getImage(String host, byte[] defaultImage) { private byte[] getImage(String host, byte[] defaultImage) {
// Check if there is cached information for the requested domain // If we've already attempted to get the favicon twice and failed,
if (missesCache.get(host) != null) { // return the default image.
if (missesCache.get(host) != null && missesCache.get(host) > 1) {
// Domain does not have a favicon so return default icon // Domain does not have a favicon so return default icon
return defaultImage; return defaultImage;
} }
// See if we've cached the favicon.
if (hitsCache.containsKey(host)) {
return hitsCache.get(host);
}
byte[] bytes = getImage("http://" + host + "/favicon.ico"); byte[] bytes = getImage("http://" + host + "/favicon.ico");
if (bytes == null) { if (bytes == null) {
// Cache that the requested domain does not have a favicon // Cache that the requested domain does not have a favicon. Check if this
missesCache.put(host, Boolean.TRUE); // is the first cache miss or the second.
if (missesCache.get(host) != null) {
missesCache.put(host, 2);
}
else {
missesCache.put(host, 1);
}
// Return byte of default icon // Return byte of default icon
bytes = defaultImage; bytes = defaultImage;
} }
// Cache the favicon.
else {
hitsCache.put(host, bytes);
}
return bytes; return bytes;
} }
...@@ -191,4 +208,4 @@ public class FaviconServlet extends HttpServlet { ...@@ -191,4 +208,4 @@ public class FaviconServlet extends HttpServlet {
} }
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment