爲了節約流量,同時也是爲了更好的用戶體驗,目前不少應用都使用本地緩存機制,其中以網易新聞的緩存功能最爲出色。我本身的應用也想加入本地緩存的功能,因而我從網上查閱了相關的資料,發現整體上說有兩種方法。一種是本身寫緩存的處理,一種是採用ASIHTTPRequest中的ASIDownloadCache。緩存
方法一:通常將服務器第一次返回的數據保存在沙盒裏面。這樣在手機斷網的狀況下能夠從本地讀取數據了。服務器
1.保存到沙盒的代碼:app
[plain] view plaincopyatom
+ (void)saveCache:(int)type andID:(int)_id andString:(NSString *)str; url
{ spa
NSUserDefaults * setting = [NSUserDefaults standardUserDefaults]; .net
NSString * key = [NSString stringWithFormat:@"detail-%d-%d",type, _id]; orm
[setting setObject:str forKey:key]; blog
[setting synchronize]; ip
}
2.讀取本地沙盒的代碼
讀取以前首先根據type和Id判斷本地是否有
[plain] view plaincopy
+ (NSString *)getCache:(int)type andID:(int)_id
{
NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
NSString *key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];
NSString *value = [settings objectForKey:key];
return value;
}
若是沙盒裏面有數據
[plain] view plaincopy
NSString *value = [Tool getCache:5 andID:self.QiuTime];
if (value) {
NSDictionary *backdict = [value JSONValue];
if ([backdict objectForKey:@"items"]) {
NSArray *array=[NSArray arrayWithArray:[backdict objectForKey:@"items"]];
for (NSDictionary *qiushi in array) {
QiuShi *qs=[[[QiuShi alloc]initWithDictionary:qiushi] autorelease];
[self.list addObject:qs];
}
}
[self.tableView reloadData];
}
[self.tableView tableViewDidFinishedLoadingWithMessage:@"數據所有加載完了.."];
self.tableView.reachedTheEnd = YES;
方法二:使用ASIHTTPRequest和ASIDownloadCache實現本地緩存
一、設置全局的Cache
在AppDelegate.h中添加一個全局變量
[plain] view plaincopy
@interface AppDelegate : UIResponder
{
ASIDownloadCache *myCache;
}
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,retain) ASIDownloadCache *myCache;
在AppDelegate.m中的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加以下代碼
[plain] view plaincopy
//自定義緩存
ASIDownloadCache *cache = [[ASIDownloadCache alloc] init];
self.myCache = cache;
[cache release];
//設置緩存路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
[self.myCache setStoragePath:[documentDirectory stringByAppendingPathComponent:@"resource"]];
[self.myCache setDefaultCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
在AppDelegate.m中的dealloc方法中添加以下語句
[plain] view plaincopy
[myCache release];
到這裏爲止,就完成了全局變量的聲明。
二、設置緩存策略
在實現ASIHTTPRequest請求的地方設置request的存儲方式,代碼以下
[plain] view plaincopy
NSString *str = @"http://....../getPictureNews.aspx";
NSURL *url = [NSURL URLWithString:str];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
//獲取全局變量
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
//設置緩存方式
[request setDownloadCache:appDelegate.myCache];
//設置緩存數據存儲策略,這裏採起的是若是無更新或沒法聯網就讀取緩存數據
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
request.delegate = self;
[request startAsynchronous];
三、清理緩存數據
我在這裏採用的是手動清理數據的方式,在適當的地方添加以下代碼,我將清理緩存放在了應用的設置模塊:
[plain] view plaincopy
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate.myCache clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy];