Skip to content

Instantly share code, notes, and snippets.

@varunachar
Last active September 29, 2016 10:04
Show Gist options
  • Save varunachar/5335579 to your computer and use it in GitHub Desktop.
Save varunachar/5335579 to your computer and use it in GitHub Desktop.
A cache buster for jsp. Meant for webapp sitting behind a Apache 2 server. Cache is busted by appending a file with it's last modified time and then including it in a JSP. Example : <script type="text/javascript" src="js/trelta-all.min.123345.js" ></script> You don't need to modify the actual name of the file on the hard disk, since we've define…
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>Cache Buster</display-name>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<uri>com.trelta.commons.web.bust</uri>
<function>
<name>bust</name>
<function-class>com.trelta.commons.web.CacheBuster</function-class>
<function-signature>java.lang.String bust(java.lang.String, java.lang.String)</function-signature>
<description>Check last modification time of file and bust cache if file changed</description>
</function>
</taglib>
package com.trelta.commons.web;
import java.io.File;
import org.apache.commons.io.FilenameUtils;
import com.trelta.commons.utils.constants.Settings;
/**
* Cache Buster which appends the last modified time of a file to it's name
* and generates the appropriate tag which can then be included in a jsp
* @author Varun Achar
* @version 1.0
*/
public class CacheBuster
{
/**
* Generate the tag that should be added to the jsp using the last modified
* time of the file. This will bust the cache in case the file is changed
*
* @param path
* The path to the file starting from the document root,
* i.e. the path that is used normally in a tag.
* @param type
* The type of file to build. Possible values are <b>js</b> and <b>css</b>
* @return The tag to include in the jsp
*/
public static String bust(String path, String type)
{
if (CommonUtil.isBlank(path))
{
throw new IllegalArgumentException("path is invalid. Value passed was : " + String.valueOf(path));
}
if (CommonUtil.isBlank(type) || ((!type.equalsIgnoreCase("js")) && (!type.equalsIgnoreCase("css"))))
{
throw new IllegalArgumentException("type is invalid. Value passed was : " + String.valueOf(type));
}
if (Settings.IS_DEVELOPMENT_MODE)
{
if (type.equalsIgnoreCase("js"))
{
return buildScript("/" + path);
}
return buildStyle("/" + path);
}
File file = new File(Settings.DOCUMENT_ROOT + "/" + path);
String extensionLess = FilenameUtils.removeExtension(path);
if (type.equalsIgnoreCase("js"))
{
return buildScript("/" + extensionLess + "." + file.lastModified() + "."
+ FilenameUtils.getExtension(path));
}
return buildStyle("/" + extensionLess + "." + file.lastModified() + "." + FilenameUtils.getExtension(path));
}
private static String buildScript(String path)
{
return "<script type=\"text/javascript\" src=\"" + path + "\"></script>";
}
private static String buildStyle(String path)
{
return "<link rel=\"stylesheet\" type=\"text/css\" href=\"" + path + "\" media=\"screen\"></link>";
}
}
# Other configurations...
# File based cache buster. Redirects request for example.12131231.js to
# example.js. This approach is better than example.js?v=123 since some
# populare cache proxies, e.g. Squid, don't cache resources with a querystring.
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)\.(\d+)\.(js|css|png|jpg|gif)$ $1.$3 [L]
</IfModule>
<!DOCTYPE html>
<%@ taglib prefix="cache" uri="com.trelta.commons.web.bust"%>
<html>
<head>
${cache:bust("css/landing/login-library.css", "css")}
</head>
<body>
${cache:bust("jquery/jquery.min.js", "js")}
</body>
</html>
<!-- Other configuration -->
<jsp-config>
<taglib>
<taglib-uri>uri-used-in-bust.tld</taglib-uri>
<taglib-location>/WEB-INF/tags/bust.tld</taglib-location>
</taglib>
</jsp-config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment