"304 Not Modified"响应是如何生成的?
浏览器如何确定HTTP请求的响应是否为304?
它是由浏览器设置还是从服务器发送?
如果是由服务器发送,服务器如何知道缓存中可用的数据,也如何将304设置为图像?
我的猜测,如果它是由浏览器生成的:
function is_modified()
{
return get_data_from_cache() === get_data_from_url();
}
function get_data_from_cache()
{
return some_hash_or_xxx_function(cache_data);
}
function get_data_from_url()
{
return some_hash_or_xxx_function(new_data);
}
function some_hash_or_xxx_function(data)
{
// Do something with the data.
// What is that algorithm?
return result;
}
console.log(is_modified());
我依赖于第三方API提供程序来获取数据、解析并将其推送到我的数据库。每次请求时,数据可能会更改,也可能不会更改,但标头始终发送200
。我不想解析、检查数据库中的最后一个唯一ID等等…来确定数据的更改,也不想直接比较结果,而是使用md5()
、sha1()
和crc32()
散列化结果,这很有效,但我想知道确定304
的算法。
我想使用同样的算法来确定我的数据是否有变化。