YouTube API etag缓存,etag会在每个请求时刷新

问题描述 投票:1回答:1

第一步:

我发送了GET请求

curl --location --request GET 'https://www.googleapis.com/youtube/v3/videos?key={API_KEY}&part=id&chart=mostPopular&regionCode=es&videoCategoryId=17&maxResults=25'

我收到200 OK回复,正文:

{
    "kind": "youtube#videoListResponse",
    "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/CcRzMe1sxt_kvnG7LpS3dIQoCig\"",
    "nextPageToken": "CBkQAA",
    "pageInfo": {
        "totalResults": 181,
        "resultsPerPage": 25
    },
    "items": [
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/_GxV3cYD2wcterTfViCosiAVb54\"",
            "id": "LGnQfH8dnow"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/-qYNOMCfpr8knbAS5auTTwRVt5Q\"",
            "id": "3cO8sTv9aE4"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/4VBqB7u16yEkTMk6sSYEOI4P-hs\"",
            "id": "8-7b4Ixavrg"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/ppyJEw0-OpZ3VI56IIn4vwl6Ja8\"",
            "id": "tZvxbUosFWw"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/qk0x2ve66gTYW6_g8O4BWlfrvJU\"",
            "id": "_4-KEZw9bX4"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/XP7oKSr0MwfURXwaW5nr1Qi8WbE\"",
            "id": "sgERdPX_dkM"
        },
        ...(other videos...)
    ]
}

我这边将body.etag值保存在缓存中。该值:"etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/CcRzMe1sxt_kvnG7LpS3dIQoCig\""

我再次发送了相同的GET请求,但是我向此GET请求添加了If-None-Match: "tnVOtk4NeGU6nDncDTE5m9SmuHc/CcRzMe1sxt_kvnG7LpS3dIQoCig" HTTP标头。

curl --location --request GET 'https://www.googleapis.com/youtube/v3/videos?key={API_KEY}&part=id&chart=mostPopular&regionCode=es&videoCategoryId=17&maxResults=25' \
--header 'If-None-Match: "tnVOtk4NeGU6nDncDTE5m9SmuHc/CcRzMe1sxt_kvnG7LpS3dIQoCig"'

我收到200 OK回复,正文:

{
    "kind": "youtube#videoListResponse",
    "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/_ruxn7w4X-iRBUUok6BWY66DtLU\"",
    "nextPageToken": "CBkQAA",
    "pageInfo": {
        "totalResults": 181,
        "resultsPerPage": 25
    },
    "items": [
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/_GxV3cYD2wcterTfViCosiAVb54\"",
            "id": "LGnQfH8dnow"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/-qYNOMCfpr8knbAS5auTTwRVt5Q\"",
            "id": "3cO8sTv9aE4"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/4VBqB7u16yEkTMk6sSYEOI4P-hs\"",
            "id": "8-7b4Ixavrg"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/ppyJEw0-OpZ3VI56IIn4vwl6Ja8\"",
            "id": "tZvxbUosFWw"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/qk0x2ve66gTYW6_g8O4BWlfrvJU\"",
            "id": "_4-KEZw9bX4"
        },
        {
            "kind": "youtube#video",
            "etag": "\"tnVOtk4NeGU6nDncDTE5m9SmuHc/XP7oKSr0MwfURXwaW5nr1Qi8WbE\"",
            "id": "sgERdPX_dkM"
        },
        ...(other videos...)
    ]
}

我希望收到304未修改,不是200 OK,结果相同。但是我仍然在响应正文中收到200 OK和新的etag。请参阅屏幕快照。

difference between responses

我希望只有一个第一反应。当我使用If-None-Match: "tnVOtk4NeGU6nDncDTE5m9SmuHc/CcRzMe1sxt_kvnG7LpS3dIQoCig"请求相同的查询时,我希望收到304,导致请求的数据没有变化。

youtube-api youtube-data-api
1个回答
0
投票

无论您从API请求什么,数据都已更改,您得到200。您可以通过将maxResults设置为1来检查,如果速度很快,则可以得到304。可能是视频的观看次数是原因。所以我唯一能做的,因为我正在做类似的事情,是在预定时间段内进行应用内缓存。

© www.soinside.com 2019 - 2024. All rights reserved.