views:

84

answers:

2

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

A: 

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.

Sergii
Hi, thanks for your response..i think storing the cache in the application scope appeals to me. Then I presume that I check each entry added to the stored cache, and ensure that it doesnt exceed the limit within a specific time frame.
namtax
+3  A: 

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

zarko.susnjar
Hi thanks for your response, just trying to get a better understanding. Wonder where would i place *yourCacheStruct*, in the application.cfc? Also this method only seems to be limiting calls per function, rather than application wide? I.E you can only make 5 requests per originating IP address per second, averaged over a 5 minute period. These requests can come from anywhere in the application, from the artist page handler above, or maybe from a track page handler. So the application has to ensure that all calls from anywhere dont exceed the limit
namtax
I edited the code in this answer to give a clearer understanding of how to store the cache in application scope. In the long run, I might consider adding a cache controller that handles all of the caching of data via a few function calls, rather than rewriting it in each method that needs to make use of caching; but this should give you an idea of how it's done.
Adam Tuttle
I am not sure why @zarko decided to put `cache="5 mins"` in the function definition, though. It doesn't do anything except add some custom metadata.
Adam Tuttle
Depends how you write your app, OO or procedural. You could wrap this logic into 50 different scenarios.You can make one singleton cfc initiated in application.cfc, same like you do with lastFM cfc, and put all logic there. Methods for inserting, updating, purging etc. If you put also method for counting calls to singleton you'd have also "limitator" which could fire some nice alert to inform user that "Currently, service is overloaded, try in X minutes".
zarko.susnjar
@Adam - I copy/pasted his text to save couple of seconds typing it :)
zarko.susnjar
Thanks for this...Would application.artistCache not be changed to application.lastFMCache to encompass all calls to the API, whether that be for artist or tracks data? Also would a hit count need to be added to application.artistCache to ensure that the rate limit isnt being exceeded within the 5 min period?
namtax
Yes, put it in one struct.You could make stack for limitator also in application scope. When actual API call is sent to API, prepend timestamp into application wide array (application.hitCounter) and remove 6th+ elements. Before calling check if there are at least 5 calls in array, check ArrayMin(application.hitCounter) and if difference between current time and oldest (5th) call is more than 5mins call API, if not alert user or whatever.
zarko.susnjar
Yes, seems like a good technique, will go with this.
namtax