2009-12-05 63 views

回答

0

NSURLConnection支持NSURLCache风格的缓存,并且在幕后为您做了很多工作。它甚至有一个很好的委托方法,让您操作cachedResponse:

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse

1

你可以在下降到ASI码之前提供自己的高速缓存。

裹在有方法的类的ASI码:

-(NSString *)getContentFor:(NSURL *)url

该方法首先检查内部的NSDictionary,看它是否有存在的密钥对指定的URL。如果是,则它返回与密钥一起存储的对象。

如果没有,它将执行正常的ASIRequest。当从服务器收到请求时,它将它作为字符串存储在字典中,并使用url的键。

当然,您需要小心处理异步请求并过期旧请求。

1

任何人询问他们如何使用ASIHTTPRequest直接执行此操作可能会对此代码的branch感兴趣,因为这些代码增加了对此功能的支持作为选项。

3

ASIHTTPRequest现在支持缓存 - 检查ASIDownloadCache即。

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]] 
0

试试这个,它适用于我。

__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDownloadCache:[ASIDownloadCache sharedCache]]; 
    [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
    [request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy]; 
    [request setSecondsToCache:60*60*24]; // Cache for 24 hrs 
    [request setDelegate:self]; // A delegate must be specified 
    [request setCompletionBlock:^{ 
相关问题