EGOCache icon indicating copy to clipboard operation
EGOCache copied to clipboard

Using paths as keys

Open jfradj opened this issue 13 years ago • 4 comments

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

  • (void)writeData:(NSData*)data toPath:(NSString *)path; { [[NSFileManager defaultManager] createDirectoryAtPath:[[path stringByExpandingTildeInPath] stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; [data writeToFile:path atomically:YES]; }

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.

jfradj avatar Jun 26 '11 19:06 jfradj

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];
}

respectTheCode avatar Jul 23 '12 17:07 respectTheCode

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 ;-)

jfradj avatar Aug 06 '12 15:08 jfradj

Can you post an example?

respectTheCode avatar Aug 06 '12 15:08 respectTheCode

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];

jfradj avatar Aug 06 '12 16:08 jfradj