Azure Mobile Services as a API Cache, avoiding rate limits – Part 2

 

In a previous post I discussed the details of using Azure Mobile Services as a caching tier for your API calls.

As a quick overview, previously I showed you how you can use Azure Mobile Services a caching middle tier between your mobile application and a third party API provider. You could use this approach to reduce the number of calls that you make to your third party API and therefore avoid  API rate limit/ API throttling scenarios. Also, I walked through all the implementation steps using as an example the Bing search API. 

However, one key aspect that was pending to discuss was how you’d handle deleting expired records from the database.

The Scheduler working for you

Deleting the expired records from you database is an important task, but one that doesn’t need to happen frequently. So this task is perfectly suited for a scheduled job that runs at periodic intervals and fortunately Azure Mobile Services has this capability via the Scheduler. Like the name suggest, the Scheduler enables you to create server side scripts that run at intervals of your own choosing.

 

Note: The frequency that you can execute a scheduled script depends on the type of the tier you are using. For more information check the Azure Mobile Services’ pricing info here.

 

If you recall from the previous post we had two tables, one containing the cached records from the third party API (bingnewscache), and another where we stored the key that uniquely identifies at set of records (cachekeys). This last table was very important as it helps you handling concurrency scenarios –yes, that was the geeky part. Next, what I will show you is how you can use the Scheduler to delete the expired records from both tables.

So first, let’s create a new scheduled job by going to the portal, selecting you mobile service and then clicking on the scheduler tab.

 

image

Next, let’s create the scheduled job and assign a name.

image

 

Once the job is ready we can create a script that when executed, will delete the expired items from both tables. For this, we can simply execute a TSQL delete statement for each table as shown in the script below.

function cachecleanup() {

//Delete cache data
var sql = 'DELETE from bingnewscache WHERE cacheexpiration < GETDATE()';
 
    mssql.query(sql, {
        success: function(results) {
            console.log('Success, expired records removed');
        }, error: function(err) {
            console.log('Error: %s', err);
        }
    });
    
    
    //Delete cache key
var sql = 'DELETE from cachekeys WHERE cacheexpiration < GETDATE()';
 
    mssql.query(sql, {
        success: function(results) {
            console.log('Success, expired cachekeys removed');
        }, error: function(err) {
            console.log('Error: %s', err);
        }
    });


}

Once you script is ready, then you can schedule the execution according to what is appropriate for your particular case.

 

image

 

Easy, right? Happy Caching!!


No Comments

Post Reply