Menu

Nakov.com logo

Thoughts on Software Engineering

Caching Google RPC Services (in GWT)

In my current GWT project I have some data comming from the server side but is unlikely to be changed during the execution of the project. This is the the list of languages available in the system. I wanted to cache the requests to the RPC service that returns the list of available languages so that this list to be dowloaded only once.

What I done:

public class LanguageServiceAsyncCache {

  private static LanguageDTO[] languagesCache = null;

  public static void getLanguages(final AsyncCallback callback) {
    if (languagesCache != null) {
      // Languages are cached, return the cached instance
      callback.onSuccess(languagesCache);
    }
    else {
      // Execute a RPC service to get the languages list
      LanguagesServiceAsync languagesServiceAsync =
        ServiceUtils.getLanguagesServiceAsync();
      languagesServiceAsync.getLanguages(new AsyncCallback() {
        public void onSuccess(Object result) {
          languagesCache = (LanguageDTO[]) result;
          callback.onSuccess(languagesCache);
       }
       public void onFailure(Throwable caught) {
         callback.onFailure(caught);
      }
    });
  }
}
}

In the above code the LanguageDTO class is simple JavaBean that transports the data from the server to the client (see Data Transfer Object pattern). The ServiceUtils is a simple class that keeps the client proxies of the RPC services as singletons. It is used to ensure that teh service proxy for each RPC srevice is created no more than once.

Comments (0)

RSS feed for comments on this post. TrackBack URL

LEAVE A COMMENT