I'm using NSURLCache and I'd like to set a custom key for a NSURLRequest. Is that possible?
Complete explanation:
I'm developing a mapping app, that uses OpenStreetMap tiles. OpenStreetMap provides multiple servers to serve the tiles, to reduce the load on each server. I'm using these servers randomly. So for instance, the following URLs will give the same tile:
Obviously, it causes some problems for my caching, because if I cache a tile from server A, next time, if I try to load from server B, the NSURLCache won't find the tile.
So, I'd like to set myself the cache key, in order to handle that case. Is that possible?
You can subclass NSURLCache and override the implementations of cachedResponseForRequest:
and storeCachedResponse:forRequest:
-- then use setSharedURLCache:
to set it to your subclass.
The easiest thing is probably to call super
for storing (or not override), but then on the lookup, if it's a tile request, check all the possibilities (using super
) and return the response if you get a non-nil one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With