配置优化
近期观摩斗鱼前端代码,发现斗鱼将某些常用配置静态化了,以下是一些其中一个例子
// 斗鱼定制特效配置
// 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%驾临直播间"}]
})
经过了解,斗鱼将常用网站配置静态化了,其响应头如下
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)
配置静态化,并独立配置服务的好处是
便于修改
在60秒内用户可以连续使用本地缓存,可以极大减小服务器开销
如果缓存超时了,依然可以304继续使用本地缓存,继续减小开销
对于不经常修改的网站配置,这样的方法挺管用的,并且加载速度加快了,值得学习
大家各有各的缓存策略
观摩过多个网站,都有这样那样的缓存策略。都值得研究一番