使用缓存或CDN更好吗? [英] Is it better to use Cache or CDN?

查看:101
本文介绍了使用缓存或CDN更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究加载静态文件时的浏览器性能,并且这个疑问已经出现。

I was studying about browser performance when loading static files and this doubt has come.


有人说使用CDN静态文件(例如Google Code,最新的jQuery
,AJAX CDN等)在性能上更好,因为它从另一个域请求的
比整个网页都要多。

Some people say that use CDN static files (i.e. Google Code, jQuery latest, AJAX CDN,...) is better for performance, because it requests from another domain than the whole web page.

提高性能的其他方法是将 Expires 标头
设置为等于几个月后,强制浏览器缓存静态
文件并减少请求。

Other manner to improve the performance is to set the Expires header equal to some months later, forcing the browser to cache the static files and cutting down the requests.

我想知道哪种方式最好,考虑性能和
(如果我可以将两者结合起来)。

I'm wondering which manner is the best, thinking about performance and if I may combine both.

在此先感谢大家,我喜欢StackOverflow。

Thank you in advance guys, I love StackOverflow.

推荐答案

如果要执行站点的Web性能优化(WPO)(也称为前端优化(FEO)),则最好同时使用这两种技术。他们可以携手惊人地工作。尽管如果我不得不选择一个,我肯定会在任何一天选择缓存。实际上,我想说的是,即使要使用CDN,也必须为所有Web项目设置适当的资源缓存。

Ultimately it is better to employ both techniques if you are doing web performance optimization (WPO) of a site, also known as front-end optimization (FEO). They can work amazingly hand in hand. Although if I had to pick one over the other I'd definitely pick caching any day. In fact I'd say it's imperative that you setup proper resource caching for all web projects even if you are going to use a CDN.

设置 Expires 标头和资源缓存是必须的,并且应该在100%的时间内完成您的资源。确实没有不做缓存的借口。在Apache上,启用 mod_expires.c mod_headers.c 后,配置起来非常容易。 HTML5 Boilerplate项目。htaccess文件,如果您的服务器是nginx,lighttpd或IIS之类的服务器,请查看以下其他服务器配置

Setting Expires headers and caching of resources is a must and should be done 100% of the time for your resources. There really is no excuse for not doing caching. On Apache this is super easy to config after enabling mod_expires.c and mod_headers.c. The HTML5 Boilerplate project has good implementation example in the .htaccess file and if your server is something else like nginx, lighttpd or IIS check out these other server configs.

如果有人的话,这是一本好书对学习缓存感兴趣:马克·诺丁汉的缓存教程

Here's a good read if anyone is interested in learning about caching: Mark Nottingham's Caching Tutorial

您提到了 Google代码,jQuery最新版本,AJAX CDN ,我只想总体上讲CDN,包括那些您要付费并托管自己的资源的CDN,但是如果您只是使用jquery托管文件cdn或从 http:// cdnj例如s.com/

You mentioned Google Code, jQuery latest, AJAX CDN and I want to just touch on CDN in general including those you pay for and host your own resources on but the same applies if you are simply using the jquery hosted files cdn or loading something from http://cdnjs.com/ for example.

我想说CDN的重要性不如设置服务器端标头缓存,但CDN 可以可以提供显着的性能提升,但是您的内容交付网络性能会因提供商而异(>

I would say a CDN is less important than setting server side header caching but a CDN can provide significant performance gains but your content delivery network performance will vary depending on the provider.

如果您的访问量来自世界各地,并且CDN提供商在世界范围内有许多边缘/对等站点,则尤其如此。由于您将某些工作转移到CDN上以交付资源,因此,它还将大大减少您的虚拟主机带宽和cpu的使用量。

This is especially true if your traffic is a worldwide audience and the CDN provider has many worldwide edge/peer locations. It will also reduce your webhosting bandwidth significantly and cpu usage (a bit) since you're offloading some of the work to the CDN to deliver resources.

CDN可以,在少数情况下,如果CDN的延迟最终比服务器慢,则会对性能造成负面影响。另外,如果您过度优化并过度使用资源的并行化(使用cdn1,cdn2,cdn3等多个子域)可能最终会减慢用户体验并通过额外的DNS查找而导致开销。这里需要一个良好的平衡。

A CDN can, in some rarer cases, cause a negative impact on performance if the latency of the CDN ends up being slower then your server. Also if you over optimize and employ too much parallelization of resources (using multi subdomains like cdn1, cdn2, cdn3, etc) it is possible to end up slowing down the user experience and cause overhead with extra DNS lookups. A good balance is needed here.

可能发生的另一个负面影响是CDN中断。它已经发生了,并将再次发生。免费CDN更是如此。如果CDN由于任何原因而掉线,您的站点也会掉线。这是另一种可能的 单点故障(SPOF) 。对于javascript资源,无论是哪种情况,您都可以从CDN上获得技巧并从CDN加载资源,如果资源失败,则可以检测并加载本地副本。这是一个从ajax.googleapis.com加载jQuery的示例,具有后备功能(取自HTML5 Boilerplate):

One other negative impact that can happen is if the CDN is down. It has happened, and will happen again. This is more true with free CDN. If the CDN goes down for whatever reason, so does your site. It is yet another potential single point of failure (SPOF). For javascript resources you can get clever and load the resource from the CDN and should it fail, for whatever the case, then detect and load a local copy. Here's an example of loading jQuery from ajax.googleapis.com with a fallback (taken from the HTML5 Boilerplate):

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.8.2.min.js"><\/script>')</script>

如果您使用的是明显的免费API资源(jquery,google api等), CDN,您可能需要支付使用费,因此它将增加托管费用。当然,对于某些CDN,您甚至必须支付额外费用才能访问某些位置,例如,亚洲节点可能比北美要额外花费。

Besides obvious free API resources out there (jquery, google api, etc) if you're using a CDN you may have to pay a fee for usage so it is going to add to hosting costs. Of course for some CDN you have to even pay extra to get access to certain locations, for example Asian nodes might be additional cost then North America.

这篇关于使用缓存或CDN更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆