避免缓存分页页面更新的策略

Strategies to avoid caching updates for paginated pages

如果您有 20 篇关于 /消息/ 每个分页页面上有 20 个,例如 /news/page2/ /news/page399/ 如果您在第 1 页添加一篇额外的文章,理论上您需要为这 400 页的每个页面更新缓存(在 Akamai 或其他平台上)。

新闻网站有什么巧妙的方法来避免这种情况?

我们想过在最后添加最新的文章。但这对于一个新闻网站来说感觉有点奇怪。也许只缓存每个部分的前 10 页是一种选择?

简而言之,您需要停止将页面视为一个整体实体,而应关注各个页面元素。 HTML 可以使用相对较低的 TTL 进行缓存,而图像、JS、CSS 和字体等更静态的元素可以使用较高的 TTL 进行缓存。这可以防止您担心清除缓存,并让 CDN 为您处理负载。

Akamai 客户社区非常棒 blog post that looks at managing caches with frequently updated websites。关键是要在 TTL 和更新内容的频率之间找到正确的平衡,以及支持 IMS 请求和 "HTTP/1.1 304 Not Modified" 响应。

来自博客post:

For the rapid[ly] update[d] content, we need to balance caching and update times. Here is a good formula I recommend;

  1. HTML caching: Instead of revalidating with origin for every end user request, or having a fixed low TTL of a minute or two – imagine that the vast majority of users could receive updates every 60 seconds, with a small portion of users sometimes seeing content up to 2 mins old - but with a very significant reduction in the amount of those content refresh requests made towards origin.
  2. Images, fonts, icons can cache for a long time, as these rarely change. In the rare occurrence that these items do change, new filenames can be used to ensure new images are picked up quickly – or Akamais CCU/CCU API can be used to force content refresh. TTLs of 7 days or 30 days are common here – though I have seen sites caching for months (or more!) for very popular, very static content.
  3. The origin needs to be enabled to support ‘If-Modified-Since’ HTTP GET and HTTP 304 responses.

The times specified in (1) could be adjusted upwards or downwards, but generally I find that 60 or 120 seconds TTL on the HTML content for the vast of users tends to be a sweet spot, though some customers like the 3 to 5 minute range. The number could be in the higher range if you find your business needs for updates are less frequent. The number could also be smaller but bear in mind that we are trying to keep origin requests down – lower numbers will mean more frequent refreshes from Akamai to Origin.

...

What if I told you this CMS driven site ensures nearly all of its users HTML, JS and CSS content is refreshing from origin every 3.5 minutes - but at no point did the origin receive more than 28 hits per second from Akamai or use more than 3mbps bandwidth?