Client Side Caching

Use case: Application needs to reduce the traffic between server and client when fetching UI content and also improve the response time.

Solution:

By implementing a client side caching solution the UI content will be fetched only the initial time thus redcuing the n/w traffic and improving the response time. Nexaweb provides a set of Macro API to implement this solution. The content from UI definition URLs (static xal files or dynamic url) will be saved as a macro. If the macro is null, MCO sends a request to server. The resulting XML UI  response content is then saved as an executable macro definition. Next time when the user click the same button, the mco will check  to see if the macro exists first, if it is already  in the DOM, will execute the macro instead go to the server to fetch the same content again. It is recommended to use the url as the key for macro.

Sample Code

UI definition: Two menu items: Home and Help.

Index.xal:

<xal>
 <declarations xmlns="http://openxal.org/core/mco">
  <mco id="testmco" src="testMco" onLoad="mco://testmco.init()"/>
 </declarations>
 <rootPane>
    <borderPane>
      <panel borderPosition="north">
        <button text="Main" onCommand="mco://testmco.getPage(&quot;mainPage.xal&quot;)"/>
        <button text="Help" onCommand="mco://testmco.getPage(&quot;helpPage.xal&quot;)"/>
      </panel>
      <panel borderPosition="center" id="contentPanel">
        <freePane/>
      </panel>
 </rootPane>
</xal>

mainPage.xal:

<xal>
    <xm:modifications xmlns:xm=http://openxal.org/core/xmodify>
        <xm:replace-children select="id(&apos;contentPanel&apos;)">
            <label text="this is main page"/>
        </xm:replace-children>
    </xm:modifications>           
</xal>

helpPage.xal:

<xal>
    <xm:modifications xmlns:xm=http://openxal.org/core/xmodify>
        <xm:replace-children select="id(&apos;contentPanel&apos;)">
            <label text="this is help page"/>
        </xm:replace-children>
    </xm:modifications>           
</xal>

The MCO code(testMco.java):

  public void getPage(String pagename){
  MacroContainer macrocontainer = _session.getMacroContainer();
  Macro m = macrocontainer.getMacro(pagename);
  if(m==null){
   try {
    HttpResponse res = _netservice.retrieve(pagename);
    m = macrocontainer.createMacro(new String(res.getContent()));
    macrocontainer.putMacro(pagename,m);
   } catch (NetServiceException e) {
    e.printStackTrace();
   }
  }
  try {
   m.execute();
  } catch (ParserException e) {
   e.printStackTrace();
  }
 }