August 12, 2009

In this blog post we learn how we can use the Google App Engine SDK API with Gaelyk and on the side we learn some Groovy syntax suggar. In a previous blog post we learned how easy it is to write and deploy a Groovlet with Gaelyk. We are going to extend this Groovlet with caching of the Yahoo! Pipe results. Each time a user requests the Groovlet, also the Yahoo! Pipe is requested. The results are transformed to a HTML page. Since we are using the URLFetchService from the Google App Engine, each request has a cost. Luckely we can make a lot of invocations on the URLFetchService, but still we have to take into account the number of invocations is limited.

To reduce the number of invocations on the URLFetchService and to speed things up, we will cache the Yahoo! Pipe results with the Google App Engine MemCacheService. Because the MemCacheService is part of the Google App Engine SDK API we are also limited to a number of invocations. But this number is much bigger than for the URLFetchService, so we can serve much more requests before we hit the limit.

Using the MemCacheService is very easy: the service is already injected into the Groovlet by Gaelyk and is available as memcacheService. The service itself is basically a hashmap we can store data in and retrieve data from. To save the Yahoo! Pipe results we only have to invoke the put(key, value) method. A downside of the Yahoo! Pipe request is that we don't get any response headers like Last-Modified or ETag. Normally we could have used this headers to determine if the results have changed and if we had to make a new request, or that we could use the cached value. But because we don't get this information we use a different caching mechanism: we store the results for five minutes in the cache. When a user requests the Groovlet again after five minutes we refresh the cache and make a new request for the Yahoo! Pipe. In a next blog post we see how we can add response headers Last-Modified and ETag to our Groovlet. Clients can use this information to use cached results if necessary. This will lessen the number of invocations on the URLFetchService and MemCacheService and increase the response time of the Groovlet.

Here is the code of the rewritten Groovlet with caching of the Yahoo! Pipe results: