Authored by 姜枫

add page cache

  1 +
  2 +'use strict';
  3 +
  4 +const SECOND = 1;
  5 +const MINUTE = 60 * SECOND;
  6 +
  7 +const cachePage = {
  8 + '/': 5 * MINUTE,
  9 +
  10 + // 频道页
  11 + '/boys': 30 * SECOND,
  12 + '/woman': 30 * SECOND,
  13 + '/kids': 30 * SECOND,
  14 + '/lifestyle': 30 * SECOND,
  15 +
  16 + // 商品详情页
  17 + '/product/\\/pro_([\\d]+)_([\\d]+)\\/(.*)/': 30 * MINUTE,
  18 +
  19 + // 逛
  20 + '/guang': 1 * MINUTE,
  21 + '/guang/info/index': 10 * MINUTE,
  22 + '/guang/index/editor': 1 * MINUTE,
  23 + '/guang/tags/index': 1 * MINUTE,
  24 +
  25 + // 领券中心
  26 + '/activity/coupon/index': 5 * MINUTE,
  27 +
  28 + // 商品列表
  29 + '/product/list/index': 5 * MINUTE,
  30 + '/product/index/index': 5 * MINUTE,
  31 +
  32 + // 秒杀列表
  33 + '/product/seckill': 30 * SECOND,
  34 +
  35 + // 秒杀详情
  36 +
  37 + // sale
  38 + '/product/sale': 5 * MINUTE,
  39 + '/product/sale/vip': 5 * MINUTE,
  40 + '/product/sale/breakingYards': 5 * MINUTE,
  41 + '/product/sale/newSale': 5 * MINUTE,
  42 + '/product/sale/discount/detail': 5 * MINUTE,
  43 +
  44 + '/product/outlet': 30 * SECOND,
  45 +
  46 + '/product/index/brand': 2 * MINUTE,
  47 + '/product/index/about': 10 * MINUTE,
  48 + '/product/shoplist': 2 * MINUTE,
  49 +
  50 + '/product/list/new': 30 * SECOND,
  51 +
  52 + // 品牌一览
  53 + '/brands': 5 * MINUTE,
  54 + '/brands/plusstar': 5 * MINUTE,
  55 +
  56 + '/special/(\\d+)_(.*)\\.html': 5 * MINUTE
  57 +
  58 +
  59 +};
  60 +
  61 +module.exports = cachePage;
  1 +
  2 +'use strict';
  3 +
  4 +const path = require('path');
  5 +const cachePage = require('../../config/cache');
  6 +
  7 +module.exports = () => {
  8 + return (req, res, next) => {
  9 + if (req.get('X-Requested-With') === 'XMLHttpRequest') {
  10 + res.set('Cache-Control', 'no-cache');
  11 + }
  12 +
  13 + function onRender() {
  14 + let route = req.route ? req.route.path : '';
  15 + let appPath = req.app.mountpath;
  16 + let key = path.join(appPath, route.toString()); // route may be a regexp
  17 +
  18 + req.app.set('etag', false);
  19 +
  20 + // 如果存在cache配置,并且业务代码中没有设置
  21 + if (cachePage[key] && res.get('Cache-Control') !== 'no-cache') {
  22 + res.set({
  23 + 'Cache-Control': 'max-age=' + cachePage[key]
  24 + });
  25 + res.removeHeader('Pragma');
  26 + res.removeHeader('Expires');
  27 + } else {
  28 + res.set({
  29 + 'Cache-Control': 'no-cache',
  30 + Pragma: 'no-cache',
  31 + Expires: new Date(1900, 0, 1, 0, 0, 0, 0)
  32 + });
  33 + }
  34 +
  35 + }
  36 +
  37 + res.on('render', onRender);
  38 + next();
  39 + };
  40 +};
@@ -62,7 +62,7 @@ @@ -62,7 +62,7 @@
62 "uuid": "^2.0.2", 62 "uuid": "^2.0.2",
63 "winston": "^2.2.0", 63 "winston": "^2.2.0",
64 "winston-daily-rotate-file": "^1.1.4", 64 "winston-daily-rotate-file": "^1.1.4",
65 - "yoho-node-lib": "0.1.23", 65 + "yoho-node-lib": "0.1.25",
66 "yoho-zookeeper": "^1.0.3" 66 "yoho-zookeeper": "^1.0.3"
67 }, 67 },
68 "devDependencies": { 68 "devDependencies": {