使用php了解服务器端的缓存js文件

问题描述:

I am trying to cache js files on server side and came across this link.

The whole idea is to get contents of all js files to be cached and write to a separate file which is also having a .js extension.

What I am not able to get is that, the cache file created is just another js file. How is it different from normal js files. To me it was like combing all js files into one big js files and output as a buffer. Also is it not increasing the code/text ratio because of this. Please help me understand and also to avoid the increase in code/text ratio.

I searched a lot on google and this site for answers and not able to get anything even close. So please downvote.

我试图在服务器端缓存js文件并遇到这个链接。 p>

整个想法是获取要缓存的所有js文件的内容,并写入一个也具有.js扩展名的单独文件。 p>

我无法得到的是,创建的缓存文件只是另一个js文件。 它与普通的js文件有什么不同。 对我来说,这就像将所有js文件合并为一个大js文件并输出为缓冲区。 也是因为这不会增加代码/文本比率。 请帮助我理解并避免代码/文本比率的增加。 p>

我在谷歌和这个网站上搜索了很多答案,但却无法得到任何结果。 所以请downvote。 p> div>

the purpose of this is to reduce the number of connections to the server. In this approach there is one large .js file download, rather than a bunch of smaller ones. Presumably you will adjust your php to include only the js files that you actually need.

The article you linked to is part of a series that proposes the following steps for more efficient JavaScript server to client delivery:

  1. Link a single js file from the HTML, and setup the server to let PHP (or other server-side technology handle it. The server will combine your multiple files into one, to avoid additional, time-consuming HTTP calls.

  2. Compress the output with gzip

  3. Minify the code. This can mean several things, but at a minimum removing line-breaks, tabs and spaces to reduce file size.

  4. Caching on the server: instead of processing your js files on every request to combine them, and minify the result each time, save your processed js file on disk, and serve it directly to the clients. You can write a script that updates that file when needed (when any of your source files has been modified).

  5. Caching on the client: set the proper response headers to have the browsers cache your js file locally, and only request a new one from the server when the cache expired.

I think you were reading #4 out of context, it only makes sense after #1 and #3.