js 文件缓存处理方法

213 篇文章 0 订阅
192 篇文章 0 订阅
 由于效率的缘故,浏览器通常会缓存js文件,这就给我们带了一个问题:当服务器端部署的项目中的js文件进行了修改后,如果再客户端不手动去刷新一次页面,js的修改效果就不会起作用,因为浏览器还是用的缓存在本地的js文件。

为了解决这个问题,我们采用了这个方案,每当发布新的版本部署到服务器上的时候,我们给html页面中引用的js增加一个新的后缀,形如版本号的东西。

Html代码
  1. <script language="javascript" src="view/shell/login.js?version=20081112050245"></script>
<script language="javascript" src="view/shell/login.js?version=20081112050245"></script>

为了自动完成这一功能,我们使用了ant,写了一个ant task来完成该项功能。

Java代码
  1. /**
  2. * 该任务遍历web目录,找出所有的模板文件,给js和css的引用加上版本号
  3. *
  4. * @version 1.0 2008-07-02
  5. * @author huangyuanmu
  6. * @since JDK 1.5.0_8
  7. */
  8. public class AddJsAndCssVersionToVm extends Task {
  9. private String path;
  10. public void execute() throws BuildException {
  11. Date date = new Date();
  12. SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
  13. String version = df.format(date);
  14. addVersionToVm(path, version);
  15. }
  16. /**
  17. * 遍历web目录中的vm文件,给js和css的引用加上版本号
  18. *
  19. * @author huangyuanmu 2008-07-02
  20. * @param path
  21. */
  22. private void addVersionToVm(String path, String version) {
  23. File dir = new File(path);
  24. File[] files = dir.listFiles();
  25. if (files == null)
  26. return;
  27. for (int i = 0; i < files.length; i++) {
  28. if (files.isDirectory()) {
  29. addVersionToVm(files.getAbsolutePath(), version);
  30. } else {
  31. String strFileName = files.getAbsolutePath().toLowerCase();
  32. // 如果是符合条件的文件,则添加版本信息
  33. if (strFileName.endsWith(".vm")
  34. || strFileName.endsWith(".html")
  35. || strFileName.endsWith(".jsp")) {
  36. // RandomAccessFile raf = null;
  37. InputStream is = null;
  38. OutputStream os = null;
  39. List<String> contentList = new ArrayList<String>();
  40. // 读文件
  41. try {
  42. is = new FileInputStream(files);
  43. Reader r = new InputStreamReader(is);
  44. BufferedReader br = new BufferedReader(r);
  45. String line = null;
  46. while ((line = br.readLine()) != null) {
  47. String modLine = getModLine(line, version);
  48. if (modLine != null) {
  49. line = modLine;
  50. }
  51. line = line + "\r\n";
  52. contentList.add(line);
  53. }
  54. // 关闭流
  55. br.close();
  56. r.close();
  57. } catch (Exception e) {
  58. System.out.println("读文件失败");
  59. e.printStackTrace();
  60. } finally {
  61. if (null != is) {
  62. try {
  63. is.close();
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. }
  69. // 写文件
  70. try {
  71. os = new FileOutputStream(files);
  72. Writer w = new OutputStreamWriter(os);
  73. BufferedWriter bw = new BufferedWriter(w);
  74. for (Iterator<String> it = contentList.iterator(); it
  75. .hasNext();) {
  76. String line = it.next();
  77. bw.write(line);
  78. }
  79. // 更新到文件
  80. bw.flush();
  81. // 关闭流
  82. bw.close();
  83. w.close();
  84. } catch (Exception e) {
  85. System.out.println("写文件失败");
  86. e.printStackTrace();
  87. } finally {
  88. if (null != os) {
  89. try {
  90. os.close();
  91. } catch (Exception e) {
  92. e.printStackTrace();
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * 查找行中是否有js或css的引用,如果有,则加上版本号
  102. *
  103. * @author huangyuanmu 2008-07-03
  104. * @param line
  105. */
  106. private String getModLine(String line, String version) {
  107. // 增加js版本
  108. line.trim();
  109. if (line.startsWith("<script") && line.endsWith("</script>")) {
  110. int pos = line.indexOf(".js");
  111. String modLine = line.substring(0, pos) + ".js?version="
  112. + version + "\"></script>";
  113. return modLine;
  114. } else if (line.startsWith("<link")
  115. && line.endsWith("rel=\"stylesheet\" type=\"text/css\">")) {
  116. int pos = line.indexOf(".css");
  117. String modLine = line.substring(0, pos) + ".css?version="
  118. + version
  119. + "\" rel=\"stylesheet\" type=\"text/css\">";
  120. return modLine;
  121. } else {
  122. return null;
  123. }
  124. }
  125. public void setPath(String path) {
  126. this.path = path;
  127. }
  128. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值