Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using paths as keys #8

Open
jfradj opened this issue Jun 26, 2011 · 4 comments
Open

Using paths as keys #8

jfradj opened this issue Jun 26, 2011 · 4 comments

Comments

@jfradj
Copy link

jfradj commented Jun 26, 2011

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.

@respectTheCode
Copy link

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
Copy link
Author

jfradj commented Aug 6, 2012

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
Copy link

Can you post an example?

@jfradj
Copy link
Author

jfradj commented Aug 6, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants