One of the network acceleration methods, combined download of JS file resources

Source: Internet
Author: User
Anyone who has experience downloading ftp files has experience. Downloading a 100 MB file is much faster than downloading a MB file. This is because domain name resolution is required for downloading, connection establishment and other additional overhead time, So 100 additional overhead is required for downloading 100 files. Therefore... syntaxHighlighter. all (); anyone who has experience downloading ftp files has experience. Downloading a 100 M file is much faster than downloading a M file. This is because domain names need to be resolved for downloading, connection establishment and other additional overhead time, So 100 additional overhead is required for downloading 100 files. Therefore, it is much faster to integrate the 100 files into one file. Based on the above method, you can perform similar processing on JS files in HTML, regardless of the number of js files, through the configuration file, merge N js files into one file for download. Use httpHandler for implementation. The Code is as follows: in the web. config file: Only merge the request files of MergedJS. js. Which file is requested? The querystring parameter is specified. For example, if MergedJS. js is used in this example? Jsid = myjs. For other types of js files, the default request method is IIS. Jquery-ui-1.8.16.custom.min.js, jquery-1.4.1.js, jquery-1.7.min.js, jquery-1.5.2.min.js, jquery-1.4.1.min.js, jquery. maskedinput-1.3.js, json2.min. js, jquery. hotkeys-0.0.3.js, jquery. loadmask. min. js, My. js "/>The preceding configuration specifies the files used by myjs for merging. MergeJS. GetJS HttpHandler is mainly used to process js. This involves a caching problem. Normally, js files are cached in the local browser by default, but for MergedJS. js, which is processed through httpHandler, is regarded as dynamic content and will not be cached in the browser, but will get the latest content from the server every time. Therefore, the browser cache can be forcibly used by encoding. Because I/O operations performed by js repeatedly are performance bottlenecks, in global, all js pre-reads are first stored in dictionary, which is output directly from dictionary when used to avoid I/O operations. The code can determine whether the js file has been modified. If yes, the value in the dictionary file will also be updated. The Code of Handler processing is as follows :/// /// Summary of GetJS ///Public class GetJS: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "application/x-javascript"; // indicates the javascript file type string jsid = context. request. params ["jsid"]. toString (); // obtain all files that require Merge string [] jsfiles = System. configuration. configurationManager. appSettings [jsid]. toString (). replace ("\ r ",""). replace ("\ n ",""). split (','); // record the last modification time of the file, and retrieve Time. DateTime lastChangeTime = new DateTime (); StringBuilder sb = new StringBuilder (); foreach (var js in jsfiles) {if (Global. dic_jsfiles.ContainsKey (js) {DateTime tmplastDateTime = new FileInfo (Global. dic_jsfiles [js]. filefullName ). lastWriteTime; tmplastDateTime = DateTime. parse (tmplastDateTime. toString ("yyyy-MM-dd HH: mm: ss"); // remove millisecond information. If (tmplastDateTime> Global. dic_jsfiles [js]. lastModifiedTime) // if the last modification time changes, update the dic_jsfiles content {Global. dic_jsfiles [js]. content = Common. readFile (Global. dic_jsfiles [js]. filefullName); // read the new file Global. dic_jsfiles [js]. lastModifiedTime = tmplastDateTime;} else // if not, read the file {string filename = context. request. physicalApplicationPath + "Scripts \" + js. trim (); Global. dic_jsfiles.Add (filename, new jsFileI Nfo () {filefullName = filename, content = Common. readFile (filename), lastModifiedTime = new FileInfo (filename ). lastWriteTime}); Common. readFile (Global. dic_jsfiles [js]. filefullName);} sb. append (Global. dic_jsfiles [js]. content + ";") ;}// last modification time of the file lastChangeTime = Global. dic_jsfiles.Max (m => m. value. lastModifiedTime); DateTime If_Modified_Since = new DateTime (); if (context. request. headers ["If-Modified- Since "] = null | context. request. headers ["If-Modified-Since"] = "") www.2cto.com {If_Modified_Since = new DateTime (1900, 1, 1 ); // If there is no value for If-Modified-Since in the Read Request Header, the default value is 19000101} else {If_Modified_Since = DateTime. parse (context. request. headers ["If-Modified-Since"]);} if (If_Modified_Since> = lastChangeTime) // If the time in the client request header is last Modified, returns code 304 {context. response. statusCode = 304; // return Return code 304 to read the cached context. response. end ();} else // otherwise {// display the Content context. response. addHeader ("last-modified", lastChangeTime. toString ();} context. response. write (sb. toString ();} private string ReadFile (string filename) {using (StreamReader sr = new StreamReader (filename, System. text. UTF8Encoding. UTF8) {return sr. readToEnd () ;}} public bool IsReusable {get {return false ;}}: Combined download: js download It takes 73 ms. However, separate downloading is time-consuming. Of course, it is not a combination of files, so the download speed will be fast. The specific download speed and policy will be affected by the browser. Some browsers have a large number of request threads simultaneously, the download speed is also affected. But in general, if the number of files is large, it will always be faster to download a single file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.