Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching JSON: HTTP cache vs database

I'm working on an application that consumes JSON feed from the backend. By default, an HTTP request will be fired every time when the user accesses the application while being connected to the Internet, and if there's no network the data should be loaded from cache. I'm using Retrofit for networking, and I see two ways to implement caching: either use an OkHttp client configured to support caching, or create an SQLite database to store data. The second option is familiar to me, therefore looks more reliable, however there's a lot of overhead related to this approach. I've never really used HTTP caching, and I'd like to know whether it's sufficient to just rely on it for offline data viewing?

like image 253
Egor Avatar asked Apr 17 '14 18:04

Egor


1 Answers

You are right about SQLite, it is better approach which is to separate HTTP requests and access to actual data. Since it is feed, it is structured data. You can do following approach:

  1. Http Requests -> JSON -> Parser -> SQLite DB insertion
  2. CursorLoader ( Or custom any loader ) from SQLite DB -> List or any other adapters

This way, you always have access to data even if there is no internet. You can trigger HTTP poll any good time and update DB. On update/change have observer of content data to trigger loader and update UI if that's shown currently to user.

like image 94
Volodymyr Lykhonis Avatar answered Sep 30 '22 00:09

Volodymyr Lykhonis