Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching strategy for Android app

I am thinking about how to ideally implement a cache layer in my Android app.

Currently I have generic Activities which display data coming from a remote server. The data is represented by a DTO TemplateInstance. Each TemplateInstance has a Map with Components 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).

Currently my app loads a TemplateInstance from the server each time an Activity is started.

I would now like to implement a cache layer in the app, so that

  1. the time to display data is reduced to a minimum,
  2. the data is refreshed when it is changed on the server.

My strategy for this looks like this:

  • The started Activity loads the TemplateInstance from a local storage by an ID (if exists)
  • A UpdateService checks in the background if the TemplateInstance has changed on the server (using a version field in the database)
  • 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

I implemented this already successfully with db4o. There are just two problems with this solution:

  • db4o is under GPL (I cannot use it)
  • db4o is really slow when I load TemplateInstances which have many images (4 seconds for a query)

Now I am looking for the best replacement for db4o. My ideas about that are until now:

  • SQLite is not suitable because of the structure of the data
  • I don´t need database functionality - retrieving objects by ID would be enough
  • Holding the objects in memory would be significantly faster
  • The memory state should be saved to disk when application exits, so the objects can be reinstantiated at startup

What do you think is the best solution for this?

My research on this brought me to EHCache and JCS, 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?

like image 516
Konsumierer Avatar asked Feb 02 '11 11:02

Konsumierer


1 Answers

If I understand your situation correctly, I think you should implement your own caching solution.

I would use an HashMap<id, TemplateInstance>. The HashMap is serializable and you could store/load it using ObjectOutputStream and ObjectInputStream, respectively.

like image 159
goncalossilva Avatar answered Oct 13 '22 00:10

goncalossilva