日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

静态化

發(fā)布時間:2024/9/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 静态化 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

靜態(tài)化是在項目中比較常見的一種技術(shù),實現(xiàn)起來也非常簡單。無非就是先將訪問的資源保存為html文件,訪問時優(yōu)先訪問html。不過在靜態(tài)化之前還需要做一些準(zhǔn)備工作。

1.將訪問路徑編寫成rest風(fēng)格的,如下:

@RequestMapping("/index/{moduleId}/{reportId}")

?

2.將ajax請求盡可能的轉(zhuǎn)化成同步加載的,這樣靜態(tài)化的時候才會盡可能多的靜態(tài)化數(shù)據(jù),當(dāng)然是用ajax也可以,只不過是用ajax的數(shù)據(jù)就不會被靜態(tài)化了。

?

3.提前將所有界面都生成html文件,這里是用了spring的定時任務(wù)根據(jù)自己的實際情況更新數(shù)據(jù)。我們的數(shù)據(jù)倉庫每天晚上更新一次,所以我們這里的靜態(tài)化的線上定時時間也是每天晚上在數(shù)據(jù)倉庫更新了之后運行,靜態(tài)化了之后減輕了數(shù)據(jù)庫和服務(wù)器的壓力,訪問速度當(dāng)然也快了好幾倍

1 <dependency> 2 <groupId>commons-httpclient</groupId> 3 <artifactId>commons-httpclient</artifactId> 4 <version>3.1</version> 5 </dependency> httpclient依賴

?

1 package com.demo.task; 2 3 import java.io.File; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import org.apache.commons.httpclient.HttpClient; 11 import org.apache.commons.httpclient.methods.GetMethod; 12 import org.apache.log4j.Logger; 13 import org.springframework.beans.factory.annotation.Autowired; 14 import org.springframework.context.support.GenericXmlApplicationContext; 15 import org.springframework.scheduling.annotation.Scheduled; 16 import org.springframework.stereotype.Component; 17 import org.springframework.test.context.ContextConfiguration; 18 19 import com.demo.controller.paramvo.DataReportParam; 20 import com.demo.entity.DataModule; 21 import com.demo.entity.DataReport; 22 import com.demo.service.IDataModuleService; 23 import com.demo.service.IDataReportService; 24 25 /** 26 * 靜態(tài)化 27 */ 28 @Component 29 @ContextConfiguration(locations = "/applicationContext.xml") 30 public class StaticHtml { 31 32 33 private Logger logger = Logger.getLogger(StaticHtml.class); 34 /** 靜態(tài)化URL */ 35 public String static_url = "/dpf/main/index/${moduleId}/${reportId}"; 36 /** 靜態(tài)化界面名稱 */ 37 public static String static_name = "main_index_${moduleId}_${reportId}.html"; 38 /** 判斷是否是windows系統(tǒng) */ 39 public final static boolean IS_LOCAL = System.getProperty("os.name").startsWith("win")||System.getProperty("os.name").startsWith("Win"); 40 /** 網(wǎng)址(可以考慮IP,指定WEB來處理) */ 41 private final String LOGON_SITE = IS_LOCAL?"172.16.5.16":"192.168.99.96";; 42 /** 端口 */ 43 private final int LOGON_PORT = IS_LOCAL?8080:8080;; 44 private HttpClient client = new HttpClient(); 45 /** 運行鎖 */ 46 public boolean EXEC_LOCK = false; 47 public static String static_root = "/templates/temp/"; 48 /** 靜態(tài)頁面存放根目錄 */ 49 private final static String INDEX_HOME_ROOT = IS_LOCAL?"/home/admin/view"+static_root 50 :"/home/admin/view"+static_root; 51 52 /** 同步文件shell腳本 */ 53 private String scp_command = "scp -r /home/admin/view/templates/temp/ 192.168.99.97:/home/admin/view/templates/"; 54 /** 靜態(tài)化列表 URL - 是否靜態(tài)化 1-是 0-否 */ 55 public HashMap<String,Integer> static_html = new HashMap<String,Integer>(); 56 private GenericXmlApplicationContext context = new GenericXmlApplicationContext(); 57 /** 模塊 */ 58 @Autowired private IDataModuleService dataModuleService; 59 /** 報表 */ 60 @Autowired private IDataReportService dataReportService; 61 /** 62 * @功能描述: 初始化client 63 * @創(chuàng)建作者: ** 64 * @創(chuàng)建日期: 2016年10月17日 下午2:16:14 65 */ 66 public void createClient(){ 67 client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT); 68 } 69 70 /** 71 * @功能描述: 添加URL 72 * @param moduleId 模塊id 73 * @param reportId 報表id 74 */ 75 private void addUrl(String moduleId, String reportId){ 76 static_html.put(static_url.replace("${moduleId}", moduleId) 77 .replace("${reportId}", reportId),0); 78 } 79 80 /** 81 * @功能描述: 初始化URL 82 */ 83 private void initUrl(){ 84 List<DataModule> modules = dataModuleService.queryAll(); 85 for(int i=0; i< modules.size(); i++) { 86 Long moduleId = modules.get(i).getId(); 87 DataReportParam vo = new DataReportParam(); 88 vo.setModuleId(moduleId); 89 List<DataReport> reports = dataReportService.queryList(vo); 90 for(int j=0; j< reports.size(); j++) { 91 Long reportId = reports.get(j).getId(); 92 addUrl(moduleId.toString(), reportId.toString()); 93 } 94 } 95 } 96 97 /** 98 * @throws IOException 99 * @功能描述: 首頁靜態(tài)化任務(wù) 30MIN執(zhí)行一次(根據(jù)訪問量來設(shè)置更新時間) 100 * @創(chuàng)建作者: ** 101 * @創(chuàng)建日期: 2016年10月17日 下午2:15:53 102 */ 103 @Scheduled(cron="* 0/30 * * * ?") 104 public synchronized void exec() throws IOException { 105 EXEC_LOCK=true; 106 long s = System.currentTimeMillis(); 107 logger.info("-----------首頁緩存開始-----------"); 108 createClient(); 109 //初始化需要靜態(tài)化界面的url 110 static_html = new HashMap<String,Integer>(); 111 initUrl(); 112 File folder = new File(INDEX_HOME_ROOT); 113 if(!folder.exists()) 114 folder.mkdirs(); 115 int i=0; 116 int a=static_html.size(); 117 logger.info("共計緩存界面"+a+"個"); 118 System.out.println("共計緩存界面"+a+"個"); 119 for (Map.Entry<String, Integer> entry : static_html.entrySet()) { 120 i++; 121 String url = entry.getKey(); 122 Integer is_static = entry.getValue(); 123 Integer p = (int) (Double.valueOf(i)/a*100); 124 logger.info(url+" 成功("+p+"%)"); 125 System.out.println(url+" 成功("+p+"%)"); 126 if(is_static!=1){ 127 GetMethod get = new GetMethod(url); 128 //模擬表單元素 129 File file = null; 130 FileOutputStream is = null; 131 try { 132 String moduleId = url.split("/")[4]; 133 String reportId = url.split("/")[5]; 134 String fileName = static_name.replace("${moduleId}", moduleId).replace("${reportId}", reportId); 135 file = new File(INDEX_HOME_ROOT+fileName); 136 is = new FileOutputStream(file); 137 int status = client.executeMethod(get); 138 if(status>=200&&status<400){ 139 String html = get.getResponseBodyAsString(); 140 is.write(html.getBytes()); 141 logger.info(url+" 成功("+p+"%)"); 142 }else{ 143 logger.info(url+" 失敗("+p+"%)"); 144 } 145 } catch (Exception e) { 146 logger.error("首頁緩存失敗",e); 147 } finally{ 148 if(is != null){ 149 is.flush(); 150 is.close(); 151 } 152 } 153 entry.setValue(1); 154 } 155 } 156 157 logger.info("-----------緩存結(jié)束-----------"); 158 long e = System.currentTimeMillis(); 159 logger.info("-----------耗時:"+(e-s)/1000+"秒-----------"); 160 EXEC_LOCK=false; 161 } 162 163 /** 164 * @功能描述: 判斷緩存界面是否存在 165 * @創(chuàng)建作者: ** 166 * @創(chuàng)建日期: 2016年10月17日 下午3:05:26 167 * @param cityCode 168 * @return 169 */ 170 public static boolean existsStatic(Long moduleId, Long reportId){ 171 String fileName = static_name.replace("${moduleId}", moduleId.toString()).replace("${reportId}", reportId.toString()); 172 File file = new File(INDEX_HOME_ROOT+fileName); 173 if(file.exists() && file.length()>0){ 174 return true; 175 } 176 return false; 177 } 178 179 /** 180 * @功能描述: 調(diào)用靜態(tài)化界面 181 * @創(chuàng)建作者: ** 182 * @創(chuàng)建日期: 2016年9月29日 下午4:47:06 183 * @param args 184 */ 185 public void main(String[] args) { 186 try { 187 context = new GenericXmlApplicationContext(); 188 context.setValidating(false); 189 context.load("classpath*:applicationContext*.xml"); 190 context.refresh(); 191 dataModuleService = context.getBean(IDataModuleService.class); 192 dataReportService = context.getBean(IDataReportService.class); 193 exec(); 194 if(!IS_LOCAL){ 195 //同步文件夾 196 Runtime.getRuntime().exec(scp_command); 197 } 198 } catch (Exception e) { 199 logger.error("靜態(tài)化失敗", e); 200 } 201 } 202 203 } 靜態(tài)化工具類

?

4.訪問控制,如果存在靜態(tài)化界面則訪問靜態(tài)化頁面

1 @RequestMapping("/index/{moduleId}/{reportId}") 2 public ModelAndView index(@PathVariable Long moduleId, @PathVariable Long reportId) { 3 ModelAndView result = new ModelAndView("main/index"); 4 5 //判斷是否靜態(tài)化 6 if (StaticHtml.existsStatic(moduleId, reportId)) { 7 String static_name = StaticHtml.static_name.replace("${appId}", moduleId.toString()) 8 .replace("${appType}", reportId.toString()).replace(".html", ""); 9 result.setViewName(StaticHtml.static_root+static_name); 10 return result; 11 } 12 //do something 13 return result; 14 } 靜態(tài)化訪問控制

?

僅供參考,不足之處還請見諒,歡迎指正!轉(zhuǎn)載請標(biāo)明出處。如有疑問,歡迎評論或者聯(lián)系我郵箱1034570286@qq.com

轉(zhuǎn)載于:https://www.cnblogs.com/itechpark/p/yinzei_static.html

總結(jié)

以上是生活随笔為你收集整理的静态化的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。