0

I am building an application and want to use NSCache to store data for caching.

There will be approx 5 API for which I need to cache data. Should I user NSCache? I did research for NSCache but I have some doubts regarding this.

I did go through below link.

https://developer.apple.com/library/content/documentation/Performance/Conceptual/ManagingMemory/Articles/CachingandPurgeableMemory.html

I found some interesting things there.

NSCache provides two other useful "limit" features: limiting the number of cached elements and limiting the total cost of all elements in the cache. To limit the number of elements that the cache is allowed to have, call the method setCountLimit:. For example, if you try to add 11 items to a cache whose countLimit is set to 10, the cache could automatically discard one of the elements.

If we set limit to 10 and if we try to add 11th item then which particular item it will discard that is 1st one or 10th one? Or it can be a random item.

If I want to store 5 different NSCache object for all APIs then how can I do it? I should make 5 different NSCache objects or I should store 5 different dictionary in single NSCache object?

Please let me know the best way to deal with it.

Jayeshkumar Sojitra
  • 2,501
  • 4
  • 31
  • 44
  • What do you want to store in cache? As long as they are disposable objects for which you are comfortable reloading from network where necessary, then chances are `NSCache` works fine for your specific situation. – Michael Fourre Feb 01 '17 at 08:27
  • I am going to make a dating application. For which I need to store profiles. Should I use NSCache in this case? – Jayeshkumar Sojitra Feb 01 '17 at 09:16
  • I don't think you should. If you mean to be storing profiles, it's probably best to set up some kind of CoreData schema where you can store the profiles for later retrieval. You may want to use NSCache for something like the profile pictures (images) of the users for example, but probably not to store the entire profiles themselves. You severely limit your offline app usage potential when relying on cache to save profile results. – Michael Fourre Feb 01 '17 at 09:20
  • In profile there will be approx 12 attributes. Might be there will be thousands of profiles available. And if we store in core data then also it's not a good practice, Isn't it? So what will be the best profile. – Jayeshkumar Sojitra Feb 01 '17 at 09:33
  • Why isn't it good practice to store that information in CoreData? It probably isn't good practice _not_ to store it in CoreData. – Michael Fourre Feb 01 '17 at 09:34
  • Core data operation will be heavy on main thread. And we don't need all that profile stored in application. We just need temporary caching. We don't need to delete it until user deletes application. – Jayeshkumar Sojitra Feb 01 '17 at 09:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134565/discussion-between-jayesh-sojitra-and-michael-fourre). – Jayeshkumar Sojitra Feb 01 '17 at 09:39

1 Answers1

1

NSCache works basically as an NSMutableDictionary, the main difference is that even if is mutable is thread safe (usually mutable NSFoundation objects are not thread safe). So you get and set objects using keys.
Yes, the documentation is not clear, but I remember (and it makes sense) that is written that you should always detect if an object is cache and if not reload it from your primary source or manage that particular case. So it is not very important which one is removed, just make sure that at some point an object can not be there anymore and you are managing that situation.
I usually create different caches based on different context, but most of the time one would suffice.
I have few advices:

  • Your answer is tagged as Swift, thus pay attention that NSCache (in swift 2, don't know in 3) works only with objects and not struct or enumerations (value types unless thay can be bridged).
  • Remember that http protocol has its own cache system and communication, do not reinvent the wheel
Andrea
  • 26,120
  • 10
  • 85
  • 131