现在的位置: 首页 > 综合 > 正文

IOS离线缓存致内存和本地的方法

2014年06月08日 ⁄ 综合 ⁄ 共 3548字 ⁄ 字号 评论关闭

缓存由四个文件组成。

 

FlashDiskCacheManager.h

FlashDiskCacheManager.m

 

这个类是以单例的模式提供的,您可以在您想要缓存的地方来share

 

URLCacheElement.h

URLCacheElement.m

 

这个类您永远都不要使用,每一个缓存就是一个URLCacheElement的对象,然后归档到文件中,实现磁盘缓存。

 

具体见FlashDiskCacheManager类的注释部分。

 

FlashDiskCacheManager.h

 

  1. //  
  2. //  FlashDiskCacheManager.h  
  3. //    
  4. //  
  5. //  Created by jeffrey on 10-12-21.  
  6. //  Copyright 2010 AppUFO. All rights reserved.  
  7. //  
  8. #import <Foundation/Foundation.h>  
  9. #import "URLCacheElement.h"  
  10. @interface FlashDiskCacheManager : NSObject  
  11. {  
  12.     /** 存储已经缓存的url **/  
  13.     NSMutableDictionary* urlDictionary;  
  14.       
  15.     NSKeyedUnarchiver* reader;  
  16. }  
  17. @property (nonatomic, retain) NSMutableDictionary *urlDictionary;  
  18. /** 
  19.  * 
  20.  * 设置保存时间 
  21.  * 
  22.  */  
  23. + (void) setMinStoreInterval:(double)interval;  
  24. + (FlashDiskCacheManager*) sharedManager;  
  25. /** 
  26.  * 
  27.  * 获取缓存的对象 如果为nil,则缓存已经失效,或者无缓存 
  28.  * 
  29.  */  
  30. - (URLCacheElement*) cachedForWithURL:(NSURL*)url;  
  31. /** 
  32.  * 
  33.  * 缓存请求1 
  34.  * 
  35.  */  
  36. - (void) cachedWithURLResponse:(NSCachedURLResponse*) cachedResponse;  
  37. /** 
  38.  * 
  39.  * 缓存请求2 
  40.  * 
  41.  */  
  42. - (void) cachedWithData:(NSData*)data theUrl:(NSURL*)url;  
  43. /** 
  44.  * 
  45.  * 写入闪存 
  46.  * 
  47.  */  
  48. - (void) storeToDisk;  
  49. @end  

 

 

FlashDiskCacheManager.m

 

  1. //  
  2. //  FlashDiskCacheManager.m  
  3. //    
  4. //  
  5. //  Created by jeffrey on 10-12-21.  
  6. //  Copyright 2010 AppUFO. All rights reserved.  
  7. #import "FlashDiskCacheManager.h"  
  8.   
  9. @implementation FlashDiskCacheManager  
  10. @synthesize urlDictionary;  
  11. static FlashDiskCacheManager* defaultManager = nil;  
  12. /** 存储实效时间 **/  
  13. static NSTimeInterval minStoreInterval = 10 * 60 * 60;  
  14. + (void) setMinStoreInterval:(double)interval  
  15. {  
  16.     minStoreInterval = interval;  
  17. }  
  18. + (FlashDiskCacheManager*) sharedManager  
  19. {  
  20.     @synchronized(self)  
  21.     {  
  22.         if (defaultManager == nil)  
  23.         {  
  24.             defaultManager = [[self alloc] init];  
  25.         }  
  26.         return defaultManager;  
  27.     }  
  28. }  
  29. - (id) init  
  30. {  
  31.     self = [super init];  
  32.     if (self)  
  33.     {  
  34.         NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
  35.         self.urlDictionary = [[NSMutableDictionary alloc] init];  
  36.         [urlDictionary release];  
  37.           
  38.         //解档  
  39.         for (NSInteger index = 0; index < 100000; ++index)  
  40.         {  
  41.             NSString* document = [path objectAtIndex:0];  
  42.             document = [document stringByAppendingPathComponent:[NSString stringWithFormat:@"cache%d", index]];  
  43.             if ([[NSFileManager defaultManager] fileExistsAtPath:document])  
  44.             {  
  45.                 NSMutableData* data = [[NSMutableData alloc] initWithContentsOfFile:document];  
  46.                 reader = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];  
  47.                 URLCacheElement* element = [reader decodeObjectForKey:[NSString stringWithFormat:@"cache%d", index]];  
  48.                 [reader finishDecoding];  
  49.                 NSURL* urlKey = [[NSURL alloc] initWithString:element.name];  
  50.                 NSTimeInterval now = [element.date timeIntervalSinceNow];  
  51.                   
  52.                 if (fabs(now) - minStoreInterval > 0)  
  53.                 {  
  54.                     [data release];  
  55.                     [urlKey release];  
  56.                     [reader release];  
  57.                     continue;  
  58.                 }  
  59.                   
  60.                 [urlDictionary setObject:element forKey:urlKey];  
  61.                 [data release];  
  62.                 [urlKey release];  
  63.                 [reader release];  
  64.             }  
  65.             else  
  66.             {  
  67.                 break;  
  68.             }  
  69.         }  
  70.         return self;  
  71.     }  
  72.     return nil;  
【上篇】
【下篇】

抱歉!评论已关闭.