NSCache

今天在优化的时候,用了NSCache,感觉没什么两样(视觉上).按理内存缓存,怎么也比从硬盘读取的要快..

    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"Start");
        
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        
        
        for (int i = 0; i< 500; i++) {
            
            UIImage *ii = [UIImage imageWithData:[Cache objectForKey:@"img"]];
            
            [arr addObject:ii];
        }
        
        
        NSLog(@"End");
        
        
    });
    
    /*  Result With Cache;
     
     2016-01-12 09:40:28.079 MVVM[67944:4076775] Start
     2016-01-12 09:40:28.088 MVVM[67944:4076775] End    ->9ms
     
     2016-01-12 09:41:02.894 MVVM[67944:4076798] Start
     2016-01-12 09:41:02.903 MVVM[67944:4076798] End    ->9ms

     2016-01-12 09:41:13.581 MVVM[67944:4077543] Start
     2016-01-12 09:41:13.589 MVVM[67944:4077543] End    ->8ms
     
     500次
     2016-01-12 09:45:51.976 MVVM[68151:4082651] Start
     2016-01-12 09:45:52.049 MVVM[68151:4082651] End    ->73ms
     
     2016-01-12 09:46:07.863 MVVM[68151:4082967] Start
     2016-01-12 09:46:07.929 MVVM[68151:4082967] End    ->66ms
     
     2016-01-12 09:46:36.374 MVVM[68151:4083477] Start
     2016-01-12 09:46:36.433 MVVM[68151:4083477] End    ->59ms
     
     */
    
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"Disk_Start");
        
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        
        
        for (int i = 0; i< 500; i++) {
            
            
            NSData *Rdata = [[NSData alloc] initWithContentsOfFile:filePath];
            
            UIImage *ii = [UIImage imageWithData:Rdata];
            
            
            [arr addObject:ii];
        }
        
        
        NSLog(@"Disk_End");
        
        
    });
    
    /*  Result With Disk
     
     2016-01-12 09:41:36.781 MVVM[67991:4078065] Disk_Start
     2016-01-12 09:41:36.846 MVVM[67991:4078065] Disk_End  ->65ms
     
     2016-01-12 09:41:53.204 MVVM[67991:4078091] Disk_Start
     2016-01-12 09:41:53.251 MVVM[67991:4078091] Disk_End  ->47ms
     
     2016-01-12 09:41:59.468 MVVM[67991:4078614] Disk_Start
     2016-01-12 09:41:59.514 MVVM[67991:4078614] Disk_End  ->46ms
     
     500次
     2016-01-12 09:44:32.986 MVVM[68073:4080907] Disk_Start
     2016-01-12 09:44:33.876 MVVM[68073:4080907] Disk_End   ->890ms
     
     2016-01-12 09:44:57.400 MVVM[68073:4081368] Disk_Start
     2016-01-12 09:44:57.976 MVVM[68073:4081368] Disk_End   ->576ms

     2016-01-12 09:45:12.159 MVVM[68073:4081577] Disk_Start
     2016-01-12 09:45:12.658 MVVM[68073:4081577] Disk_End   ->499ms

     
     */

 

cache->disk->Internet request

-(UIImage *)getImageForCacheOrDiskOrNetWithKey:(NSString*)Key
{
    
    //cache data
    
    NSCache *cache = [[NSCache alloc] init];
    
    NSData *CacheData =  [cache objectForKey:Key];
    
    if (CacheData != nil) {
        
        
        return [UIImage imageWithData:CacheData];
        
        
    }else{
        
        //Disk read
        
        NSArray *arr = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        
        NSString *cachePath = [arr firstObject];
        
        NSString *filePath = [cachePath stringByAppendingPathComponent:Key];
        
        NSData *Diskdata = [[NSData alloc] initWithContentsOfFile:filePath];
        
        
        if (Diskdata != nil) {
            
            return [UIImage imageWithData:Diskdata];
            
        }else{
            
            //internet request here;
            
            NSString *imageURLStr = @"http://pic.58pic.com/58pic/15/48/73/04f58PIC37y_1024.png";
            
            NSData *InternetData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURLStr]];
            
            return [UIImage imageWithData:InternetData];
            
        }
    }
    
}

 

造轮子是为了学习,真正要用,还是老老实实的用sdwebimage等第三方库吧..

posted @ 2016-01-12 11:07  NGI.  阅读(193)  评论(0编辑  收藏  举报
GitHub | M1989