nam*_*tax 5 api coldfusion rate-limiting application.cfc last.fm
嗨我正在使用ColdFusion来调用last.fm api,使用来自此处的cfc bundle .
我担心超过请求限制,即每秒发送一次IP地址的5个请求,平均为5分钟.
cfc包有一个中央组件,它调用所有其他组件,这些组件被分成"艺术家","轨道"等部分......这个中心组件"lastFmApi.cfc".在我的应用程序中启动,并持续应用程序的生命周期
// Application.cfc example
<cffunction name="onApplicationStart">
<cfset var apiKey = '[your api key here]' />
<cfset var apiSecret = '[your api secret here]' />
<cfset application.lastFm = CreateObject('component', 'org.FrankFusion.lastFm.lastFmApi').init(apiKey, apiSecret) />
</cffunction>
Run Code Online (Sandbox Code Playgroud)
现在,如果我想通过处理程序/控制器调用api,例如我的艺术家处理程序......我可以这样做
<cffunction name="artistPage" cache="5 mins">
<cfset qAlbums = application.lastFm.user.getArtist(url.artistName) />
</cffunction>
Run Code Online (Sandbox Code Playgroud)
我对缓存有点困惑,但我在这个处理程序中缓存每次调用api 5分钟,但这有什么不同,因为每次有人点击新的艺术家页面时,这仍然算作对api的新鲜打击?
想知道如何最好地解决这个问题
谢谢
由于它是简单的数据模型,我不会使用自定义缓存引擎使事情变得复杂。我将简单的结构/查询:searchTerm/结果、时间戳放在某处。在那里你可以这样做:
<cffunction name="artistPage" cache="5 mins">
<cfargument name="artistName" type="string" required="true"/>
<cfif structKeyExists(application.artistCache, arguments.artistName) and structfindkey(application.artistCache, arguments.artistName)>
<cfif (gettickcount() - application.artistCache[arguments.artistName].timestamp ) lte 5000 >
<cfset result = application.artistCache[arguments.artistName].result >
<cfelse>
<cfset qAlbums = application.lastFm.user.getArtist(arguments.artistName) />
<cfset tempStruct = structnew()>
<cfset structNew.result = qAlbums >
<cfset structNew.timestamp = getTickCount() >
<cfset structInsert(application.artistCache, arguments.artistName, tempstruct, true) >
<cfset result = qAlbums >
</cfif>
<cfreturn result >
</cffunction>
Run Code Online (Sandbox Code Playgroud)
编辑:是的,您还应该在某个地方放置一个方法,该方法将删除时间戳差异大于缓存有效期的结构键。
建议使用外观模式,以便将来可以更改。
抱歉,有错别字:)
| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |