﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-狂淘-文章分类-topApi</title><link>http://www.blogjava.net/wangbbs2007/category/45121.html</link><description>www.kuangtao.net</description><language>zh-cn</language><lastBuildDate>Wed, 02 Jun 2010 03:36:23 GMT</lastBuildDate><pubDate>Wed, 02 Jun 2010 03:36:23 GMT</pubDate><ttl>60</ttl><item><title>解决API频率过高,页面空白,调用不到数据 淘宝TOP API 缓存实战 </title><link>http://www.blogjava.net/wangbbs2007/articles/321927.html</link><dc:creator>狂淘</dc:creator><author>狂淘</author><pubDate>Wed, 26 May 2010 08:48:00 GMT</pubDate><guid>http://www.blogjava.net/wangbbs2007/articles/321927.html</guid><wfw:comment>http://www.blogjava.net/wangbbs2007/comments/321927.html</wfw:comment><comments>http://www.blogjava.net/wangbbs2007/articles/321927.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/wangbbs2007/comments/commentRss/321927.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/wangbbs2007/services/trackbacks/321927.html</trackback:ping><description><![CDATA[您是不是还在为调用API超过限制而苦恼? 是不是还在为网站空白获取不到数据而郁闷?<br />
下面我们就来说说如何减少API调用次以及避免网站页面空白:<br />
<br />
<span style="color: red"><strong>一.减少API调用 </strong></span><br />
<br />
&nbsp; &nbsp; 我们都知道现在的API对每天,每分种都有一定的上限.一但超过这个上限就调不出任何数据,必须等到第二天才可以,所以尽量降低API的调用次数与频率才是关键. 为了解决这个种问题可以从以下几个方面进行处理:<br />
<br />
&nbsp; &nbsp; <span style="color: blue">A. 降低程序的复杂程序,以减少对API的调用. </span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;比如在商品上显示店铺的信用,好评率等就需要再调用户相关的API进行获取, 虽然商品信息上多了一些资讯.但是增加了API调用次数,同时也降低了网站的打开速度. 建议去掉一些意义不大的信息来降低API的调用次数<br />
<br />
&nbsp; &nbsp; <span style="color: blue">B.缓存API调用过的数据,以降低API的调用频率与次数 </span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;缓存这种词大家再熟悉不过了, 缓存的好处我就不多说了.今天就来说说如何缓存API数据以加快页面显示,减少API调用.原理就不多说了.直接修改您的代码:<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<strong>1).如果您的程序是用淘宝官方的PHP DEMO 或 file_get_contents()函数来获取API数据. </strong><br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;将<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = file_get_contents($url);<em><br />
</em>修改成:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $cacheid=md5(preg_replace('/timestamp=([\d|-]+)([\d|:]+)/i','',$url));$dirpath = substr($cacheid,0,1); $file =substr($cacheid,0,1).'/'.$cacheid; $cachetime =24*3600;if(file_exists($file) &amp;&amp; (time() - date('U',@filemtime($file))) &lt; $cachetime){$result =@file_get_contents($file);}else{$result = @file_get_contents($url);@mkdir($dirpath);@file_put_contents($file,$result);}<em><br />
</em>这样您的同一个API,同样的参数每24小时才会去访问一次API,上面的变量我做一下说明:<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;缓存ID: $cacheid 为每次调用API的参数所组成的MD5 由于timestamp会变化,所以timestamp不在其内<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;保存的目录: $dirpath&nbsp;&nbsp;为每个MD5的第一个字母,这样最多会生成36个目录<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;保存的文件: $file 采用MD5直接做为缓存文件名<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;缓存时间: $cachetime&nbsp; &nbsp;24小时 X 3600 秒,也就是一天的时间,如果需要更长只需要将24调的更大一些<br />
<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;凡是需要缓存的地方都用以上方法替换以后就可以缓存API结果了. 替换后您看看是不是页面打开更快了? API的调用次数也降下来了?<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<strong>2).如果您使用了taoapi.com上面的PHP SDK </strong><br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;只需要在您调用API时,设置一下缓存时间就行<br />
<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $Taoapi-&gt;Cache-&gt;setCacheTime(24); <em><br />
</em>设置数据缓存的时间,单位:小时;0表示不缓存,不设置默认为 0<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;具体的调用方式可以查看: <a href="http://www.taoapi.com/index.php/index/index/action/phpSdk" target="_blank">http://www.taoapi.com/index.php/index/index/action/phpSdk</a><br />
<br />
<br />
&nbsp; &nbsp; <span style="color: blue">C.静态化更新频率不高的页面 </span><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;一些更新频率不高的页面可以采用静态化页面,这样不仅对网站,API,服务器来说都是有好处的.具体的操作方式与缓存机制类同.这里我就不做重点了. 列一下相关的函数: ob_start();ob_get_contents();file_get_contents();file_put_contents();http_build_query();<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;原理: <br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;根据URL检测静态面页文件是否存在<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp; <strong>1)如果存在,并且没有过期: </strong><br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;终止程序运行,直接输出该页面文件<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<strong>2)如果存在,但已经过期: </strong><br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;程序继续运行.程序结束时用HTML内容替换现有的静态页面文件<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<strong>3)如果不存在: </strong><br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;程序继续运行.程序结束时用HTML内容建立一个新的静态页面文件<br />
<br />
<span style="color: red"><strong>二.避免网站页面空白 </strong></span><br />
<br />
&nbsp; &nbsp; API加入了缓存以后调用次数下降了,但是当网络忙或API接口繁忙时可能出现获取不到数据而出现空白页面.这时推荐大家采用一种最简单的方式: 自动重试 和采用过期缓存<br />
<br />
&nbsp; &nbsp; <span style="color: blue">1)自动重试: </span><br />
<br />
&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;当通过API第一次没有获取到数据时,再次重新获取,虽然会增加一次调用次数.但这是可接受的.因为没有什么比给用户看到空白页面更糟糕的了,这里建议最好不要超过3次. 由于自动重试需要写的代码比较多这里就不列出来了, 知道原理后可以自己写一个或者使用taoapi.com 上面的PHPSDK 支持自动重试,默认为3次<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: blue"> 2)采用过期缓存: </span><br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;如果做了缓存,在缓存更新时遇到没有获取数据(虽然这种情况非常少),就采用过期的缓存先给用户.比起空白的页面要好的多.在下一次用户访问时再重新更新缓存. 这样只要用缓存存在,您的页面永远不会空白. 目前taoapi.com 上的PHP SDK暂时还不支持这种机制,但会在尽期内加入,敬请关注<br />
<br />
<br />
今天就先聊到这里,像内存缓存.数据库缓存等这些以后有机会再和大家聊聊. 如果您在过程碰到疑问 欢迎前来找到讨论, 我的旺旺:浪子arvin&nbsp;&nbsp;QQ: 8769852,如果在API方面有其他问题推荐你先去taoapi.com测试一把,说不定问题就不是问题了,在此感谢您有耐心听我说了这么久!<br />
<a href="http://www.kuangtao.net">www.kuangtao.net</a>&nbsp; <a href="http://www.kuangtao.net">淘宝网首页</a><br /><img src ="http://www.blogjava.net/wangbbs2007/aggbug/321927.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/wangbbs2007/" target="_blank">狂淘</a> 2010-05-26 16:48 <a href="http://www.blogjava.net/wangbbs2007/articles/321927.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>