服务器端和客户端缓存相对较大的JSON文件

时间:2022-05-15 15:45:41

I need to generate and deliver a relatively large JSON file to users. By large, I mean 400 KB. It's the join of two tables, and it will probably get larger over time. I'm planning on using it to fill in the gaps in an HTML table, linking up items via IDs from the database.

我需要为用户生成并提供一个相对较大的JSON文件。很大,我的意思是400 KB。它是两个表的连接,随着时间的推移它可能会变大。我打算用它来填补HTML表格中的空白,通过数据库中的ID链接项目。

To save bandwidth, I only wanted the client to download this JSON file once per day. (The easy options seem to be "every time" and "never"... grr.) In reality, it will update less often server-side, but I don't want it less frequent. 90% of the browsers hitting this are IE7/8, if that makes a difference.

为了节省带宽,我只希望客户端每天下载一次这个JSON文件。 (简单的选项似乎是“每次”和“从不”...... grr。)实际上,它会更少地更新服务器端,但我不希望它不那么频繁。如果这会产生影响,那么90%的浏览器都是IE7 / 8。

For server-side caching, I'm currently just dropping the JSON file into a text file on the server if it's older than a few hours. I understand I could use memcached instead. Does memcached have a considerable advantage over a file? (Single server, 1k visitors per day, I don't have memcached installed yet.)

对于服务器端缓存,我现在只是将JSON文件丢弃到服务器上的文本文件中,如果它超过几个小时。我知道我可以使用memcached。 memcached相比文件有相当大的优势吗? (单个服务器,每天1k访问者,我还没有安装memcached。)

Per the PHP manual comments, I tried this:

根据PHP手册评论,我试过这个:

$expires = 60*60*24;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');

But it appears to do nothing. Chrome is informing me that it is "OK" each and every time I load the generating page directly, and no headers regarding cache control show up under the network/headers tab. I'm not explicitly hitting refresh. Doesn't look like I'm caching at all. Are there any other ways to save the client some time and bandwidth? Best idea I have is creative tinkering with the .ajax() cache option.

但它似乎什么都不做。 Chrome通知我每次直接加载生成页面都是“OK”,并且没有关于缓存控制的标题显示在网络/标题选项卡下。我没有明确地打击刷新。看起来我根本不在缓存。有没有其他方法可以节省客户端的时间和带宽?我最好的想法是使用.ajax()缓存选项进行创意修补。

I haven't actually gotten as far as implementing the Ajax call yet. Baby steps.

到目前为止,我还没有实现Ajax调用。宝贝的步骤。

(Sorry if I'm a bit wordy. Yell at me and I'll improve the question as much as I can when it is needed. I've done some searching here and on Google and haven't come across anything super useful yet. Maybe I don't have the right key terms.)

(对不起,如果我有点罗嗦。对我说,我会在需要的时候尽可能地改善这个问题。我已经在这里和谷歌做了一些搜索,但没有找到任何超级有用的东西也许我没有正确的关键术语。)

Note: I'm running PHP under IIS 7, so it's CGI. I feel like I read that might make a difference.

注意:我在IIS 7下运行PHP,所以它是CGI。我觉得我读到的可能会有所作为。

3 个解决方案

#1


3  

Sending the correct cache headers is only half the steps needed. You still need to send 304 status saying nothing has changed since the last time the client got the content. This can get tricky. I recommend using .htaccess to do this caching instead. More can be found here

发送正确的缓存标头只需要一半的步骤。您仍然需要发送304状态,表示自客户端上次获取内容以来没有任何更改。这可能会变得棘手。我建议使用.htaccess来执行此缓存。更多可以在这里找到

Example

ExpiresActive On
ExpiresDefault "access plus 1 day"

Edit--

If you had to do it all in php then you have to manually check the request headers and send 304. This how you can do it:

如果你必须在php中完成所有操作,那么你必须手动检查请求标题并发送304.这是你如何做到的:

$last_modified_time = filemtime($file); 
$etag = md5_file($file); 
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); 
header("Etag: $etag"); 
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || 
    trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
    header("HTTP/1.1 304 Not Modified"); 
    exit; 
}

#2


1  

Have you thought of using browser local storage? You use javascript to check if the data is stored locally. If it's not, or it's old, then you fetch it from the server and store it locally. Almost all "modern" browsers support local storage, meaning IE8, Firefox 3.5, Safari 4, Chrome and above. Sorry, but IE7 does not support it.

你有没有想过使用浏览器本地存储?您使用javascript检查数据是否存储在本地。如果它不是,或者它已经过时,那么您从服务器获取它并将其存储在本地。几乎所有“现代”浏览器都支持本地存储,即IE8,Firefox 3.5,Safari 4,Chrome及更高版本。对不起,但IE7不支持它。

The biggest advantage to memcache is that it is a distributed storage system. So you could have 4 load balanced web servers all accessing the same data store. That it's memory based means better performance, but it doesn't seem like you would be having performance issues.

memcache的最大优点是它是一个分布式存储系统。所以你可以有4个负载均衡的Web服务器都访问同一个数据存储。它基于内存意味着更好的性能,但似乎你不会遇到性能问题。

#3


0  

to save bsndwith, you can also use compresion on the transmission. start the php file with a call to ob_start('ob_gzhandler'); but make sure your content-type is set to text/plain or text/html, because ie7 doesn't think gzipped application/json content exists

为了保存bsndwith,你也可以使用compresion进行传输。通过调用ob_start('ob_gzhandler')启动php文件;但请确保您的内容类型设置为text / plain或text / html,因为ie7不认为gzip压缩应用程序/ json内容存在

#1


3  

Sending the correct cache headers is only half the steps needed. You still need to send 304 status saying nothing has changed since the last time the client got the content. This can get tricky. I recommend using .htaccess to do this caching instead. More can be found here

发送正确的缓存标头只需要一半的步骤。您仍然需要发送304状态,表示自客户端上次获取内容以来没有任何更改。这可能会变得棘手。我建议使用.htaccess来执行此缓存。更多可以在这里找到

Example

ExpiresActive On
ExpiresDefault "access plus 1 day"

Edit--

If you had to do it all in php then you have to manually check the request headers and send 304. This how you can do it:

如果你必须在php中完成所有操作,那么你必须手动检查请求标题并发送304.这是你如何做到的:

$last_modified_time = filemtime($file); 
$etag = md5_file($file); 
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT"); 
header("Etag: $etag"); 
if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || 
    trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
    header("HTTP/1.1 304 Not Modified"); 
    exit; 
}

#2


1  

Have you thought of using browser local storage? You use javascript to check if the data is stored locally. If it's not, or it's old, then you fetch it from the server and store it locally. Almost all "modern" browsers support local storage, meaning IE8, Firefox 3.5, Safari 4, Chrome and above. Sorry, but IE7 does not support it.

你有没有想过使用浏览器本地存储?您使用javascript检查数据是否存储在本地。如果它不是,或者它已经过时,那么您从服务器获取它并将其存储在本地。几乎所有“现代”浏览器都支持本地存储,即IE8,Firefox 3.5,Safari 4,Chrome及更高版本。对不起,但IE7不支持它。

The biggest advantage to memcache is that it is a distributed storage system. So you could have 4 load balanced web servers all accessing the same data store. That it's memory based means better performance, but it doesn't seem like you would be having performance issues.

memcache的最大优点是它是一个分布式存储系统。所以你可以有4个负载均衡的Web服务器都访问同一个数据存储。它基于内存意味着更好的性能,但似乎你不会遇到性能问题。

#3


0  

to save bsndwith, you can also use compresion on the transmission. start the php file with a call to ob_start('ob_gzhandler'); but make sure your content-type is set to text/plain or text/html, because ie7 doesn't think gzipped application/json content exists

为了保存bsndwith,你也可以使用compresion进行传输。通过调用ob_start('ob_gzhandler')启动php文件;但请确保您的内容类型设置为text / plain或text / html,因为ie7不认为gzip压缩应用程序/ json内容存在