Methods of caching data?

Discussion in 'iOS Programming' started by Soulstorm, Apr 30, 2010.

  1. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I am trying to implement caching in my RSS feed application. The application has many default RSS feeds implemented, and I want it to cache the data it downloads from each server and associate with the URL related, so that the next time the user wants to view that feed again, he will not have to download the file.

    The question is... how do I do that in a most efficient way?

    I initially thought of writing the complete raw NSData chinks I download from a server to the iPhone's disk, and then load it again when necessary, but how can I associate each cache file with a URL so that I can search if the URL the user sees at runtime is cached? I'm looking for ideas, and directions to that, rather than plain code.

    Thank you in advance.
     
  2. AndyCodez macrumors regular

    Joined:
    Aug 6, 2009
    #2
    Could always make a sqlite3 table, table_pk, url_data, url and do the lookups based on the url field?
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    It depends on the number of items. If small to medium (up to ~1000), then NSDictionary. Otherwise mysql table.

    Since a cache is designed to be small, and not contain every item forever, I'd start with NSDictionary and use that until it proves to be inadequate. It's one of the simplest things that could possibly work, and there's no point in adding mysql complexity until it's necessary.

    Store the NSDictionary as a plist file. Each item's URL is the key, the pathname of a local unique file is the string value.

    And it should go without saying, but I'll say it anyway: wrap it all inside a class, so the choice of NSDictionary vs. mysql is completely hidden from all other classes.
     
  5. Soulstorm thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #5
    Thank you all, your responses were very helpful.

    chown33, it shouldn't be more than 150~ associations. I mean... how many URLs can a user watch a day? :) Using an NSDictionary is certainly a good idea.

    However, can't this be done using Core Data? My application already uses Core Data, and it should be fairly easy to add an entity called "CachedFile" that holds the cached file locations, dates and URLs. The NSDictionary method is also good, but I think that using the available and already set up Core Data database would prove faster, and more memory efficient.
     

Share This Page