How to use EGOCache to cache and load page faster

Discussion in 'iOS Programming' started by Thelle93, Jan 25, 2014.

  1. Thelle93 macrumors newbie

    Joined:
    Oct 7, 2012
    #1
    I have an UIWebView in my iOS app that loads different url's depending on a previous action. I wan't these pages to load as fast as possible. I found the class EGOCache (source) and I i got it working to store cacheData in library/Caches directory. But I'm not sure how to retrieve this cache to load it faster, I can't see the difference. Maybe use NSCache? What have I missed?
    Code:
    - (void)webViewDidStartLoad:(UIWebView *)webView {
            if (webView_1) {
    
    
            NSString *urlAddress = @"http://www.apple.com";
            NSURL *url = [NSURL URLWithString:urlAddress];
            NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
            [webView1 loadRequest:request];
    
            NSData *data0 = [NSURLConnection sendSynchronousRequest:
                            [NSURLRequest requestWithURL:url]
                                                 returningResponse:nil
                                                             error:nil];
    
            [[EGOCache globalCache] setData:data0 forKey:@"webCache"];
            }
        }
    Thanks!
     
  2. lefooey macrumors member

    Joined:
    Dec 26, 2009
    Location:
    Spokane, WA
    #2
    You get it back by

    Code:
    NSData *storedPage = [[EGOCache globalCache] dataForKey:@"webCache"];
    
    The above will return nil if nothing has been saved in that key.

    I'd recommend changing your key to the URL string of the page, setting a cache time ( see withTimeoutInterval: ) and checking your cache before you set up NSURLRequest.
     
  3. Thelle93 thread starter macrumors newbie

    Joined:
    Oct 7, 2012
    #3
    update

    Thanks, I think I got it working now. but how do I set the cache time?

    Code:
    NSString *urlAddress = @"http://www.apple.com";
            NSURL *url = [NSURL URLWithString:urlAddress];
    
            NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
            NSString* file = [cachePath stringByAppendingPathComponent:@"/xxx.xxx/EGOCache/EGOCache.plist"];
            BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
            
            if (fileExists)
            {
                NSData *data = [[EGOCache globalCache] dataForKey:urlAddress];
                data = [NSURLConnection sendSynchronousRequest:
                        [NSURLRequest requestWithURL:url]
                                             returningResponse:nil
                                                         error:nil];
                NSLog(@"loading cache");
                
            }else if(!fileExists){
                NSData  *data = [NSURLConnection sendSynchronousRequest:
                                 [NSURLRequest requestWithURL:url]
                                                      returningResponse:nil
                                                                  error:nil];
                [[EGOCache globalCache] setData:data forKey:urlAddress];
                NSLog(@"saved cache");
            }
            NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
    
            [webView_1 loadRequest:request];
    
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Have you looked at the EGOCache.h yet?
     
  5. Thelle93 thread starter macrumors newbie

    Joined:
    Oct 7, 2012
    #5
    Yes but still not sure... like this?:
    Code:
    NSData *data = [[EGOCache globalCache] dataForKey:urlAddress withTimeoutInterval:60]; //in seconds?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    The dataForKey: method does not include a withTimeoutInterval parameter.
     
  7. Thelle93 thread starter macrumors newbie

    Joined:
    Oct 7, 2012
    #7
    Code:
    [[EGOCache globalCache] setDefaultTimeoutInterval:60*60*24*3];
    (3 days). This should be right:)
     

Share This Page