近期观摩斗鱼前端代码,发现斗鱼将某些经常使用配置静态化了,如下是一些其中一个例子前端
// 斗鱼定制特效配置 // https://webconf.douyucdn.cn/resource/common/privilege/custom_effect.json DYConfigCallback({ "callback": "customEffectConfig", "data": [ { "ceid": "76", "effect_name": "混一哈", "uid": "48604893", "web_img": "privilege/2017-11/201711011857266287.png", "web_img_swf": "privilege/2017-11/201711011857267873.swf", "bg_height": "345", "font_height": "283", "img_height": "100", "font_color": "#964100", "name_color": "#fe3636", "occurrence_mode": "1", "welcome_content": "欢迎%username%驾临直播间" }, { "ceid": "74", "effect_name": "关心120特效", "uid": "76543472", "web_img": "privilege/2017-10/201710261158313796.png", "web_img_swf": "privilege/2017-10/201710261158311098.swf", "bg_height": "360", "font_height": "300", "img_height": "100", "font_color": "#f5b471", "name_color": "#e13025", "occurrence_mode": "1", "welcome_content": "欢迎%username%驾临直播间" }, { "ceid": "73", "effect_name": "牛总111", "uid": "92939658", "web_img": "privilege/2017-10/201710301851239716.png", "web_img_swf": "privilege/2017-10/201710301851246052.swf", "bg_height": "345", "font_height": "292", "img_height": "100", "font_color": "#ff0036", "name_color": "#eb7530", "occurrence_mode": "1", "welcome_content": "欢迎%username%驾临直播间" }, { "ceid": "72", "effect_name": "龙皇120特效", "uid": "70872354", "web_img": "privilege/2017-09/201709272001496594.png", "web_img_swf": "privilege/2017-09/201709272001496898.swf", "bg_height": "301", "font_height": "238", "img_height": "100", "font_color": "#ca8066", "name_color": "#eedbc2", "occurrence_mode": "1", "welcome_content": "欢迎%username%驾临直播间" }, { "ceid": "71", "effect_name": "关觉120特效", "uid": "98795634", "web_img": "privilege/2017-07/201707251050342825.png", "web_img_swf": "privilege/2017-07/201707251050343541.swf", "bg_height": "360", "font_height": "290", "img_height": "157", "font_color": "#f2bb64", "name_color": "#fff7cb", "occurrence_mode": "1", "welcome_content": "欢迎%username%驾临直播间" } ] })
通过了解,斗鱼将经常使用网站配置静态化了,其响应头以下web
Access-Control-Allow-Origin:https://www.douyu.com // 从原始服务器到代理缓存造成的估算时间 1s Age:1 // 缓存机制,从被访问的最大缓存时间60s Cache-Control:max-age=60 Content-Encoding:gzip Content-Type:application/json Date:Fri, 10 Nov 2017 02:12:14 GMT ETag:W/"59f9a8f5-7b7" // 缓存过时时间 Expires:Fri, 10 Nov 2017 02:13:14 GMT // 最后修改时间 Last-Modified:Wed, 01 Nov 2017 10:59:01 GMT Server:openresty X-Via:1.1 chkuan145:10 (Cdn Cache Server V2.0)
便于修改json
在60秒内用户能够连续使用本地缓存,能够极大减少服务器开销缓存
若是缓存超时了,依然能够304继续使用本地缓存,继续减少开销服务器
对于不常常修改的网站配置,这样的方法挺管用的,而且加载速度加快了,值得学习app
观摩过多个网站,都有这样那样的缓存策略。都值得研究一番学习