網站配置優化

配置優化

近期觀摩鬥魚前端代碼,發現鬥魚將某些經常使用配置靜態化了,如下是一些其中一個例子前端

// 鬥魚定製特效配置
// 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)

配置靜態化,並獨立配置服務的好處是

  1. 便於修改json

  2. 在60秒內用戶能夠連續使用本地緩存,能夠極大減少服務器開銷緩存

  3. 若是緩存超時了,依然能夠304繼續使用本地緩存,繼續減少開銷服務器

對於不常常修改的網站配置,這樣的方法挺管用的,而且加載速度加快了,值得學習app

你們各有各的緩存策略

觀摩過多個網站,都有這樣那樣的緩存策略。都值得研究一番學習

相關文章
相關標籤/搜索