2月 03 2009

Cache functionality on OpenSocial Gadget Development

Published by Eiji at 0:03:19 under OpenSocial

I’ve mentioned a little bit about a strong cache functionality on OpenSocial architecture on recent post.  Shindig has 4 types of cache.

  • Gadget XML cache
  • External resource cache accessed via makeRequest
  • feature JavaScript cache
  • External resource cache linked on HTML reference(JavaScript, CSS, img)

Gadget XML cache

The first obstacle when developing a gadget must be this Gadget XML cache. If you can’t reflect your change on your gadget xml to the rendered HTML, watch gadget xml cache. 

If you want to develop a gadget changing code on gadget xml, put nocache=1 on rendered gadget URL(inside iframe). This will turn off gadget xml cache. (Note some containers have hash(#) param on its url, so just appending it might not work.)

If you are working on iGoogle sandbox, use My Gadgets to turn off the cache on the gadget. hi5 sandbox’s cache is always turned off.

External resource cache accessed via makeRequest

Accessing external resource via makeRequest using GET is also cached. For example, if you want to fetch external RSS, this cache functionality will reduce load on the server.

To avoid this cache, you can add gadgets.io.ProxyUrlRequestParamters.REFRESH_INTERVAL param to opt_params on makeRequest and set it to 0. This will turn off the cache.

feature JavaScript cache

By adding Require@feature on gadget xml, you can add various features to your OpenSocial gadget. This “feature” thing adds functionality to your gadget by adding actual JavaScript code to your rendered gadget’s HTML. Shindig is also caching this JavaScript chunk.

Users/Gadget Developers don’t necessarily care about this but, better knowing how it works :)

External resource cache link on HTML reference(JavaScript, CSS, img)

Depending on containers, some may have functionality to cache its external references to JavaScript, CSS or img. You can find “concat” link on rendered HTML if it is cached.

This functionality is called Content Rewrite and will be legally included into OpenSocial spec from version 0.9 while it is Shindig Java version specific functionality currently. On version 0.9, you will be able to specify which file type to rewrite, how long the cache will last, and so on.

While this cache is aimed to reduce load on external servers, it will also reduce the efficiency of your development. To avoid this cache functionality, change refresh parameter on its URL to 0(normally, set to 86400 which is 24 hours). Once you access the url, the cache will be cleared.

By the way, PHP Shindig doesn’t support this yet. This means, your development is efficient by default, but once your gadget is out in the wild, your server may gain a big traffic. To avoid this, you can use proxy technique.

By prepending your resource to refer with ”/gadgets/proxy?url=”, like: http://opensocial-container/gadgets/proxy?url=http://devlog.agektmr.com/image.gif, the resource will be cached. (proxy is different from concat, it doesn’t shrink, concatenate scripts)

View Comments add to hatena hatena.comment (8) add to del.icio.us (0) add to livedoor.clip (2) add to Yahoo!Bookmark (0) Total: 10

  • 最後のproxyのURLはコンテナごとに異なるのと、locked-domain機能によってコンテナのサブドメインはガジェットURLごとに違うの場合があるため、gadgets.io.getProxyUrlで取得した方がいいと思います。
    ただ、myspaceではgadgets.io.getProxyUrlが動かないのでtry-catchで囲む必要がありますが。。。

    var params = {"REFRESH_INTERVAL" : 3600*24*7};
    var url = "http://example.com/img/logo.jpg";
    try{
    url = gadgets.io.getProxyUrl(url, params);
    }catch(e){}
  • そうですね。確かにProxyURLを使ったやり方の方が汎用性が上がってスマートです。
    Gadget API Japanグループで伊藤さんがJSを読み込むサンプルコードを紹介されてました。
    http://groups.google.co.jp/group/Google-Gadgets...

    <script type="text/javascript">
    var url = gadgets.io.getProxyUrl('http://std-ig.googlecode.com/svn/
    trunk/std-ig.js');
    document.open();
    document.write('<scr'+'ipt src="' + url + '"></scr' + 'ipt>');
    document.close();
    </script>

    後ほど追記しておきます。
blog comments powered by Disqus