Search code examples
cachingappcelerator

Appcelerator caching techniques. Feedback wanted!


Titanium SDK version: 1.6.1 iPhone SDK version: 4.2

I am trying to build a solution to cache JSON calls. I have done a first attempt that does the job, but is there a better solution? I am using textfiles to save the JSON output, is this OK performance wise?

http://pastie.org/1734763

Thankful for all feedback!


Solution

  • I think that'd be ok. As long as the files aren't massive in number/size it should perform quite well.

    The other approach you could try if you decide you're not happy with performance, or want to maintain less code, is to use App storage, which persists data beyond app sessions.

    Titanium.App.setString('jsonResponse', this.responseText);
    Titanium.App.setInt('expires', this.responseText.expires);
    

    Then before you make your request you can check if the cache is indeed stale:

    var expires = Titanium.App.getInt('expires');
    
    // Get the current time in milliseconds, etc.
    
    if(expires > current_time) {
        // Cache is still valid
        var response = Titanium.App.getString('jsonResponse');
        var obj = JSON.parse(response);
    }
    else {
        // Cache is stale - query for new data
    }