enormego / EGOCache

Fast Caching for Objective-C (iPhone & Mac Compatible)
MIT License
1.33k stars 290 forks source link

Using paths as keys #8

Open jfradj opened 13 years ago

jfradj commented 13 years ago

As it is, It's not possible to use path as key because directories contained in path doesn't exists.

maybe something like this may fix the issue

I don't wanted to alter the original file (thus I can update it without asking me if I had to merge any modifications) so I changed my keys by replacing path separator by # ;-)

Cheers.

respectTheCode commented 12 years ago

The other option is to just encode the key when making the path. This solves for any characters that are not safe in file names. I modified cachePathForKey like this

static inline NSString* cachePathForKey(NSString* key) {
    NSString *encodeKey = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                              NULL,
                                                                              (CFStringRef)key,
                                                                              NULL,
                                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                              kCFStringEncodingUTF8);
    return [EGOCacheDirectory() stringByAppendingPathComponent:encodeKey];
}
jfradj commented 12 years ago

If you're using only paths as key, the fix you provided is great but in my case a lot of keys wasn't paths (they was regular keys) so using a function like CFURLCreateStringByAddingPercentEscapes has a real performance cost.

So I use an equivalent function as yours but only on demand ;-)

respectTheCode commented 12 years ago

Can you post an example?

jfradj commented 12 years ago

Just extract your encoding function out of the EGOCache cachePathForKey function:

NSString *encodeKey = (NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                                              NULL,
                                                                              (CFStringRef)key,
                                                                              NULL,
                                                                              (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                                              kCFStringEncodingUTF8);
NSData *data = [[EGOCache clearCache] dataForKey:encodeKey];