title: Caching strategy for Android app
body: <p>I am thinking about how to ideally implement a <strong>cache layer</strong> in my Android app.</p>
  
  <p>Currently I have generic Activities which display data coming from a remote server. The data is represented by a DTO <em>TemplateInstance</em>. Each <em>TemplateInstance</em> has a Map with <em>Components</em> in it and each of the components can have child components. The components themselves can be Text (String), Image (ByteArray) or Time (or whatever by sub-classing Component).</p>
  
  <p>Currently my app loads a TemplateInstance from the server each time an Activity is started.</p>
  
  <p>I would now like to implement a <strong>cache layer</strong> in the app, so that </p>
  
  <ol>
  <li><strong>the time to display data is reduced to a minimum,</strong></li>
  <li><strong>the data is refreshed when it is changed on the server.</strong></li>
  </ol>
  
  <p>My strategy for this looks like this:</p>
  
  <ul>
  <li>The started Activity loads the TemplateInstance from a local storage by an ID (if exists)</li>
  <li>A UpdateService checks in the background if the TemplateInstance has changed on the server (using a version field in the database)</li>
  <li>If the server version is greater than the local one or there is no local TemplateInstance then retrieve the data from the server, update the local store and update the view</li>
  </ul>
  
  <p>I implemented this already successfully with <strong>db4o</strong>. There are just two problems with this solution:</p>
  
  <ul>
  <li>db4o is under GPL (I cannot use it)</li>
  <li>db4o is really slow when I load TemplateInstances which have many images (4 seconds for a query)</li>
  </ul>
  
  <p>Now I am looking for the best replacement for db4o. My ideas about that are until now:</p>
  
  <ul>
  <li>SQLite is not suitable because of the structure of the data</li>
  <li>I don´t need database functionality - retrieving objects by ID would be enough</li>
  <li>Holding the objects in memory would be significantly faster</li>
  <li>The memory state should be saved to disk when application exits, so the objects can be reinstantiated at startup</li>
  </ul>
  
  <p>What do you think is the best solution for this?</p>
  
  <p>My research on this brought me to <strong>EHCache</strong> and <strong>JCS</strong>, which I have never used. Do you think they are appropriate, also in respect of resources on an Android phone? Or do you have other suggestions?</p>