缓存由四个文件组成。
FlashDiskCacheManager.h
FlashDiskCacheManager.m
这个类是以单例的模式提供的,您可以在您想要缓存的地方来share
URLCacheElement.h
URLCacheElement.m
这个类您永远都不要使用,每一个缓存就是一个URLCacheElement的对象,然后归档到文件中,实现磁盘缓存。
具体见FlashDiskCacheManager类的注释部分。
FlashDiskCacheManager.h
- //
- // FlashDiskCacheManager.h
- //
- //
- // Created by jeffrey on 10-12-21.
- // Copyright 2010 AppUFO. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "URLCacheElement.h"
- @interface FlashDiskCacheManager : NSObject
- {
- /** 存储已经缓存的url **/
- NSMutableDictionary* urlDictionary;
- NSKeyedUnarchiver* reader;
- }
- @property (nonatomic, retain) NSMutableDictionary *urlDictionary;
- /**
- *
- * 设置保存时间
- *
- */
- + (void) setMinStoreInterval:(double)interval;
- + (FlashDiskCacheManager*) sharedManager;
- /**
- *
- * 获取缓存的对象 如果为nil,则缓存已经失效,或者无缓存
- *
- */
- - (URLCacheElement*) cachedForWithURL:(NSURL*)url;
- /**
- *
- * 缓存请求1
- *
- */
- - (void) cachedWithURLResponse:(NSCachedURLResponse*) cachedResponse;
- /**
- *
- * 缓存请求2
- *
- */
- - (void) cachedWithData:(NSData*)data theUrl:(NSURL*)url;
- /**
- *
- * 写入闪存
- *
- */
- - (void) storeToDisk;
- @end
FlashDiskCacheManager.m
- //
- // FlashDiskCacheManager.m
- //
- //
- // Created by jeffrey on 10-12-21.
- // Copyright 2010 AppUFO. All rights reserved.
- #import "FlashDiskCacheManager.h"
- @implementation FlashDiskCacheManager
- @synthesize urlDictionary;
- static FlashDiskCacheManager* defaultManager = nil;
- /** 存储实效时间 **/
- static NSTimeInterval minStoreInterval = 10 * 60 * 60;
- + (void) setMinStoreInterval:(double)interval
- {
- minStoreInterval = interval;
- }
- + (FlashDiskCacheManager*) sharedManager
- {
- @synchronized(self)
- {
- if (defaultManager == nil)
- {
- defaultManager = [[self alloc] init];
- }
- return defaultManager;
- }
- }
- - (id) init
- {
- self = [super init];
- if (self)
- {
- NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- self.urlDictionary = [[NSMutableDictionary alloc] init];
- [urlDictionary release];
- //解档
- for (NSInteger index = 0; index < 100000; ++index)
- {
- NSString* document = [path objectAtIndex:0];
- document = [document stringByAppendingPathComponent:[NSString stringWithFormat:@"cache%d", index]];
- if ([[NSFileManager defaultManager] fileExistsAtPath:document])
- {
- NSMutableData* data = [[NSMutableData alloc] initWithContentsOfFile:document];
- reader = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
- URLCacheElement* element = [reader decodeObjectForKey:[NSString stringWithFormat:@"cache%d", index]];
- [reader finishDecoding];
- NSURL* urlKey = [[NSURL alloc] initWithString:element.name];
- NSTimeInterval now = [element.date timeIntervalSinceNow];
- if (fabs(now) - minStoreInterval > 0)
- {
- [data release];
- [urlKey release];
- [reader release];
- continue;
- }
- [urlDictionary setObject:element forKey:urlKey];
- [data release];
- [urlKey release];
- [reader release];
- }
- else
- {
- break;
- }
- }
- return self;
- }
- return nil;