5

こんにちは、ColdFusion を使用して last.fm API を呼び出しています。こちらから入手した cfc バンドルを使用しています。

5 分間の平均で、送信元 IP アドレスごとに 1 秒あたり 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>

ハンドラー/コントローラー、たとえばアーティストハンドラーを介してAPIを呼び出したい場合...これを行うことができます

<cffunction name="artistPage" cache="5 mins">
 <cfset qAlbums = application.lastFm.user.getArtist(url.artistName) />
</cffunction>

私はキャッシュについて少し混乱していますが、このハンドラーで API への各呼び出しを 5 分間キャッシュしていますが、これは違いがありますか? ?

これにどのように対処するのが最善か疑問に思う

ありがとう

4

2 に答える 2

3

これは単純なデータ モデルであるため、カスタム キャッシュ エンジンで複雑にすることはありません。シンプルな struct/query : searchTerm/result,timestamp をどこかに置きます。そこで、これを行うことができます:

<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>

編集:はい、タイムスタンプの違いがgtであり、キャッシュの有効期間である構造体キーを削除する方法もどこかに配置する必要があります。

将来的にこれを変更可能にするために、Facade パターンが推奨されます。

タイプミスごめんなさい:)

于 2010-04-26T10:49:11.417 に答える
0

カスタムキャッシュを試してみます。

キーがアーティスト名またはエントリのその他の一意の識別子である構造にすることができます。

CF9 または Railo 3.1.2+ を使用している場合は、組み込みのキャッシュ (関数 CachePut、CacheGet など) を使用でき、タイムアウトなどを処理できます。

それ以外の場合は、キャッシュを Application スコープに保存できますが、各エントリにタイムスタンプを含め、キャッシュ イベント (get/put/remove) または要求ごとにチェックする必要があります。

于 2010-04-26T10:25:20.767 に答える