“There are only two hard things in Computer Science: cache invalidation and naming things.” —Phil Karlton
There are four things you need to know about NSCache. The first, and most important: NSCache exists. I recently wrote a cache object in Objective-C, this was a mistake. I Google’d “objective-c cache class”:
Unhelpful Google results for “objective-c cache class”.
Top results looked uninteresting and I was feeling saucy; so I implemented one from scratch. I will be happy if this post serves no other purpose than to prevent you from making the same mistake. Now that you know the most important thing about NSCache, here are three more things you should know (especially if you still want to implement your own cache, which I recommend as an exercise). [1]
So put down that thread-unsafe NSMutableDictionary, pick up your NSCache, and stop reinventing the wheel. Or, at least know that it exists before you go making improvements.
Sources & Further Reading:
[0] Phil Karlton (Who deserves a Wikipedia page but that’s another story)
[1] Apple’s Documentation on the NSCache Object
[2] http://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used
[3] GNUStep’s implementation of NSCache - NSCache.m
To discuss this with the author, tweet @stephenbalaban.
Or go back to the home page.