iosobjective-ccachingcore-data

How To Cache Article Text Derived With Embedly API?


In my app, when a user chooses an article to read, I use Embedly's Extract API to extract the article's content html, and when I trim the NSString of all HTML tags, I have the article's full text.

Embedly has given me a certain API call limit (50,000 calls per month). If a user selects the same article multiple times, my Embedly Extract API call will be made multiple times on the same URL of that article.

If a user selects the same article 3 times, does that mean that I will have wasted 3 Embedly API calls on the same URL? If that is the case, how do I cache an article's full text?

If wasting multiple API calls on the same URL is an issue, I need to set this up so that when a user selects an article to read a second time, it won't bother to make the API call to Embedly since I already cached the article's full text.

I'm new to the whole concept of caching. I read that it should be used when data is unlikely to change. How can I go about doing this? NSUserDefaults? CoreData?

Another thing that confuses me about caching is this: How will I know to get rid of a cached article if it is no longer needed in the instance that, for example, it is no longer being shown to the user in my app's article/rss feed?

Any help or advice would be appreciated.


Solution

  • If you want the caching to happen only if the app is alive - use NSCache it's a NSDictionary-like API, and the cache automatically gets cleared if the memory pressure in iOS is high.

    You probably want a persistent cache, that works even if the user kills the app. Then the best thing you can do is implement NSCoder in the model object that keeps the result for you. Or simpler just serialize a NSDictionary using - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag.

    The best solution to save API calls is to actually have a proper backend and cache that on the backend side (assuming the links users provide are not private for them in any way, personal data, bank accounts, etc.). The backend should call out to Embedly and cache all requests there. So even the same URLs checked by the same users don't do 2 API calls.