Question

Hi I am using ColdFusion to call the last.fm api, using a cfc bundle sourced from here.

I am concerned about going over the request limit, which is 5 requests per originating IP address per second, averaged over a 5 minute period.

The cfc bundle has a central component which calls all the other components, which are split up into sections like "artist", "track" etc...This central component "lastFmApi.cfc." is initiated in my application, and persisted for the lifespan of the application

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

Now if I want to call the api through a handler/controller, for example my artist handler...I can do this

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

I am a bit confused towards caching, but am caching each call to the api in this handler for 5 mins, but does this make any difference, because each time someone hits a new artist page wont this still count as a fresh hit against the api?

Wondering how best to tackle this

Thanks

Was it helpful?

Solution

Since it's simple data model I'd not complicate things with custom cache engines. I'd put simple struct/query : searchTerm/result,timestamp somewhere. There you could do this:

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

EDIT: yes, you should put somewhere also method which would remove struct keys where timestamp difference is gt then your cache validity period.

Facade pattern is recommended in order to make this changeable in future.

Sorry for typos :)

OTHER TIPS

I would try the custom cache.

It can be a structure where keys are artist names or other unique identifiers of your entries.

If you are using CF9 or Railo 3.1.2+ you can use built-in caching (functions CachePut, CacheGet etc), it can handle for you the timeouts and stuff.

Otherwise you can store the cache into Application scope, but will need to include timestamp with each entry and check it on cache events (get/put/remove) or even on each request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top