For an application I’m working on I have a class that deals with the requests sent to a webservice. To reduce traffic load I want to cache most of the requests and their results (at least for the content that won’t change often). A request is uniquely identified by the URL used, so I want to use the URL as a key.
In a previous project I worked on I used to create a md5 hash of the URL to function as a key. I’ve began wondering how safe this approach is, since apparently there’s quite a chance that 2 different URL’s produce the same hashed value.
Can anyone suggest a safer approach, or should I still stick with md5 hashes of the URL to function as a key in my cache dictionary?
Just use an
NSMutableDictionary, with the URLs (asNSStrings) for the dictionary keys.NSDictionary(andNSMutableDictionary) are almost certainly implemented with some sort of hash table internally, but they will take care of any hash duplicate issues. If you give it a unique key, it will store it properly.Unless you’re sure that
NSMutableDictionaryis too slow for you needs (which it almost certainly won’t be), you don’t need to worry about hashing, collisions or anything like that. Just stick the data in your dictionary with a unique key (the URL).