缓存由四个文件组成。
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;
- }
- - (URLCacheElement*) cachedForWithURL:(NSURL*)url
- {
- URLCacheElement* element = [urlDictionary objectForKey:url];
- if (element == nil)
- {
- return nil;
- }
- NSTimeInterval now = [element.date timeIntervalSinceNow];
- if (fabs(now) - minStoreInterval > 0)
- {
- [self.urlDictionary removeObjectForKey:url];
- return nil;
- }
- return element;
- }
- - (void) cachedWithURLResponse:(NSCachedURLResponse*) cachedResponse
- {
- NSURLResponse* response = [cachedResponse response];
- //要存储的url
- NSURL* url = [response URL];
- //要存储的数据
- NSData* data = [cachedResponse data];
- //存储的时间
- NSDate* date = [NSDate date];
- // 存储的用户信息
- NSDictionary* user = [cachedResponse userInfo];
- URLCacheElement* element = [[URLCacheElement alloc] initWithData:data :date :user];
- [self.urlDictionary setObject:element forKey:url];
- [element release];
- }
- - (void) cachedWithData:(NSData*)data theUrl:(NSURL*)url
- {
- //存储的时间
- NSDate* date = [NSDate date];
- // 存储的用户信息
- NSDictionary* user = nil;
- URLCacheElement* element = [[URLCacheElement alloc] initWithData:data :date :user];
- [self.urlDictionary setObject:element forKey:url];
- [element release];
- }
- - (void) storeToDisk
- {
- NSArray* path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSArray* arrayOfKeys = [urlDictionary allKeys];
- //归档
- for (NSInteger index = 0; index < [arrayOfKeys count]; ++index)
- {
- NSURL* url = (NSURL*)[arrayOfKeys objectAtIndex:index];
- NSString* name = [url absoluteString];
- //初始化路径
- NSString* document = [path objectAtIndex:0];
- document = [document stringByAppendingPathComponent:[NSString stringWithFormat:@"cache%d", index]];
- NSMutableData* tData = [[NSMutableData alloc] init];
- NSKeyedArchiver* write = [[NSKeyedArchiver alloc] initForWritingWithMutableData:tData];
- URLCacheElement* element = [urlDictionary objectForKey:[arrayOfKeys objectAtIndex:index]];
- //记录这个缓存的名字
- element.name = name;
- [write encodeObject:element forKey:[NSString stringWithFormat:@"cache%d", index]];
- [write finishEncoding];
- [tData writeToFile:document atomically:YES];
- [tData release];
- [write release];
- }
- }
- - (void) dealloc
- {
- [super dealloc];
- [urlDictionary release];
- }
- @end
URLCacheElement.h
- //
- // URLCacheElement.h
- //
- //
- // Created by jeffrey on 10-12-21.
- // Copyright 2010 AppUFO. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #define KData @"urlData"
- #define KUserInfo @"userInfo"
- #define KDate @"date"
- #define KName @"name"
- @interface URLCacheElement : NSObject<NSCoding, NSCopying>
- {
- /** url存储的数据 **/
- NSData* data;
- /** 存储的用户信息 **/
- NSDictionary* userInfo;
- /** 存储的时间 **/
- NSDate* date;
- NSString* name;
- }
- - (id) initWithData:(NSData*)indata :(NSDate*)indate :(NSDictionary*)inuserInfo;
- @property (nonatomic, retain) NSData *data;
- @property (nonatomic, retain) NSDictionary *userInfo;
- @property (nonatomic, retain) NSDate *date;
- @property (nonatomic, retain) NSString *name;
- @end
URLCacheElement.m
- //
- // URLCacheElement.m
- //
- //
- // Created by jeffrey on 10-12-21.
- // Copyright 2010 AppUFO. All rights reserved.
- #import "URLCacheElement.h"
- @implementation URLCacheElement
- @synthesize data;
- @synthesize date;
- @synthesize userInfo;
- @synthesize name;
- - (id) initWithData:(NSMutableData*)indata :(NSDate*)indate :(NSMutableDictionary*)inuserInfo
- {
- self = [super init];
- if (self)
- {
- self.data = indata;
- self.date = indate;
- self.userInfo = inuserInfo;
- return self;
- }
- return nil;
- }
- - (id) initWithCoder:(NSCoder *)aDecoder
- {
- self = [super init];
- if (self)
- {
- self.data = [aDecoder decodeObjectForKey:KData];
- self.date = [aDecoder decodeObjectForKey:KDate];
- self.userInfo = [aDecoder decodeObjectForKey:KUserInfo];
- self.name = [aDecoder decodeObjectForKey:KName];
- return self;
- }
- return nil;
- }
- - (void) encodeWithCoder:(NSCoder *)aCoder
- {
- [aCoder encodeObject:data forKey:KData];
- [aCoder encodeObject:date forKey:KDate];
- [aCoder encodeObject:userInfo forKey:KUserInfo];
- [aCoder encodeObject:name forKey:KName];
- }
- - (id) copyWithZone:(NSZone *)zone
- {
- URLCacheElement *copy = [[[self class] allocWithZone:zone] init];
- self.data = [data copy];
- self.date = [date copy];
- self.userInfo = [userInfo copy];
- name = [name copy];
- return copy;
- }
- - (void) dealloc
- {
- [super dealloc];
- }
- @end
本文转自:http://blog.csdn.net/windows_star/article/details/6117392