国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

iOS中的NSURLCache數(shù)據(jù)緩存類用法解析

2020-07-26 03:19:03
字體:
供稿:網(wǎng)友

 在IOS應(yīng)用程序開發(fā)中,為了減少與服務(wù)端的交互次數(shù),加快用戶的響應(yīng)速度,一般都會在IOS設(shè)備中加一個緩存的機制。使用緩存的目的是為了使用的應(yīng)用程序能更快速的響應(yīng)用戶輸入,是程序高效的運行。有時候我們需要將遠程web服務(wù)器獲取的數(shù)據(jù)緩存起來,減少對同一個url多次請求。下面將介紹如何在IOS設(shè)備中進行緩存。

 內(nèi)存緩存我們可以使用sdk中的NSURLCache類。NSURLRequest需要一個緩存參數(shù)來說明它請求的url何如緩存數(shù)據(jù)的,我們先看下它的CachePolicy類型。
 
 1、NSURLRequestUseProtocolCachePolicy NSURLRequest默認(rèn)的cache policy,使用Protocol協(xié)議定義。
 
 2、NSURLRequestReloadIgnoringCacheData 忽略緩存直接從原始地址下載。
 
 3、NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data時才從原始地址下載。
 
 4、NSURLRequestReturnCacheDataDontLoad 只使用cache數(shù)據(jù),如果不存在cache,請求失敗;用于沒有建立網(wǎng)絡(luò)連接離線模式;
 
 5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和遠程的緩存數(shù)據(jù),直接從原始地址下載,與NSURLRequestReloadIgnoringCacheData類似。
 
 6、NSURLRequestReloadRevalidatingCacheData:驗證本地數(shù)據(jù)與遠程數(shù)據(jù)是否相同,如果不同則下載遠程數(shù)據(jù),否則使用本地數(shù)據(jù)。

 一些常用方法與屬性:
 //獲取當(dāng)前應(yīng)用的緩存管理對象
+ (NSURLCache *)sharedURLCache;
//設(shè)置自定義的NSURLCache作為應(yīng)用緩存管理對象
+ (void)setSharedURLCache:(NSURLCache *)cache;
//初始化一個應(yīng)用緩存對象
/*
memoryCapacity 設(shè)置內(nèi)存緩存容量
diskCapacity 設(shè)置磁盤緩存容量
path 磁盤緩存路徑
內(nèi)容緩存會在應(yīng)用程序退出后 清空 磁盤緩存不會
*/
- (instancetype)initWithMemoryCapacity:(NSUInteger)memoryCapacity diskCapacity:(NSUInteger)diskCapacity diskPath:(nullable NSString *)path;
//獲取某一請求的緩存
- (nullable NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
//給請求設(shè)置指定的緩存
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
//移除某個請求的緩存
- (void)removeCachedResponseForRequest:(NSURLRequest *)request;
//移除所有緩存數(shù)據(jù)
- (void)removeAllCachedResponses;
//移除某個時間起的緩存設(shè)置
- (void)removeCachedResponsesSinceDate:(NSDate *)date NS_AVAILABLE(10_10, 8_0);
//內(nèi)存緩存容量大小
@property NSUInteger memoryCapacity;
//磁盤緩存容量大小
@property NSUInteger diskCapacity;
//當(dāng)前已用內(nèi)存容量
@property (readonly) NSUInteger currentMemoryUsage;
//當(dāng)前已用磁盤容量
@property (readonly) NSUInteger currentDiskUsage;

簡單例子:

#import

@interface ViewController : UIViewController

@property (strong, nonatomic) NSURLConnection *connection;
@property (strong, nonatomic) NSURLCache *urlCache;
@property (strong, nonatomic) NSURL *url;
@property (strong, nonatomic) NSMutableURLRequest *request;

- (IBAction)reloadWebView:(UIButton *)sender;

@end

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *paramURLAsString= @"http://blog.sina.com.cn/u/2526279194";
    self.urlCache = [NSURLCache sharedURLCache];
  
    [self.urlCache setMemoryCapacity:1*1024*1024];
    //創(chuàng)建一個nsurl
    self.url = [NSURL URLWithString:paramURLAsString];
    //創(chuàng)建一個請求
    self.request=[NSMutableURLRequest requestWithURL:self.url
                                                   cachePolicy:NSURLRequestUseProtocolCachePolicy
                                   timeoutInterval:30.0f];
    [self.myWebView loadRequest:self.request];
}

 
這個例子中,我們請求url為http://blog.sina.com.cn/u/2526279194的網(wǎng)站。如果這個url被緩存了,我們直接從緩存中獲取數(shù)據(jù),否則從http://blog.sina.com.cn/u/2526279194站點上重新獲取數(shù)據(jù)。我們設(shè)置了緩存大小為1M。


- (IBAction)reloadWebView:(UIButton *)sender {

    //從請求中獲取緩存輸出
    NSCachedURLResponse *response =[self.urlCache cachedResponseForRequest:self.request];
    //判斷是否有緩存
    if (response != nil){
        NSLog(@"如果有緩存輸出,從緩存中獲取數(shù)據(jù)");
        [self.request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
    }
    [self.myWebView loadRequest:self.request];
   
    self.connection = nil;
  
    NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:self.request
                                                                     delegate:self
                                                             startImmediately:YES];
    self.connection = newConnection;
}

使用下面代碼,我將請求的過程打印出來
- (void)  connection:(NSURLConnection *)connection
  didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"將接收輸出");
}
- (NSURLRequest *)connection:(NSURLConnection *)connection
             willSendRequest:(NSURLRequest *)request
            redirectResponse:(NSURLResponse *)redirectResponse{
    NSLog(@"即將發(fā)送請求");
    return(request);
}
- (void)connection:(NSURLConnection *)connection
    didReceiveData:(NSData *)data{
    NSLog(@"接受數(shù)據(jù)");
    NSLog(@"數(shù)據(jù)長度為 = %lu", (unsigned long)[data length]);
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
                  willCacheResponse:(NSCachedURLResponse *)cachedResponse{
    NSLog(@"將緩存輸出");
    return(cachedResponse);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    NSLog(@"請求完成");
}
- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error{
    NSLog(@"請求失敗");
}

@end

第一次打印結(jié)果如下
 

2013-01-31 15:28:29.923 NSURLCacheDemo[27848:907] 即將發(fā)送請求2013-01-31 15:28:30.043 NSURLCacheDemo[27848:907] 將接收輸出2013-01-31 15:28:30.045 NSURLCacheDemo[27848:907] 接受數(shù)據(jù)2013-01-31 15:28:30.047 NSURLCacheDemo[27848:907] 數(shù)據(jù)長度為 = 300472013-01-31 15:28:30.095 NSURLCacheDemo[27848:907] 接受數(shù)據(jù)2013-01-31 15:28:30.098 NSURLCacheDemo[27848:907] 數(shù)據(jù)長度為 = 35752013-01-31 15:28:30.102 NSURLCacheDemo[27848:907] 接受數(shù)據(jù)2013-01-31 15:28:30.104 NSURLCacheDemo[27848:907] 數(shù)據(jù)長度為 = 14822013-01-31 15:28:30.105 NSURLCacheDemo[27848:907] 將緩存輸出2013-01-31 15:28:30.107 NSURLCacheDemo[27848:907] 請求完成

第二次點擊打印結(jié)果如下

2013-01-31 15:28:31.599 NSURLCacheDemo[27848:907] 如果有緩存輸出,從緩存中獲取數(shù)據(jù)2013-01-31 15:28:31.607 NSURLCacheDemo[27848:907] 即將發(fā)送請求2013-01-31 15:28:31.840 NSURLCacheDemo[27848:907] 將接收輸出2013-01-31 15:28:31.843 NSURLCacheDemo[27848:907] 接受數(shù)據(jù)2013-01-31 15:28:31.845 NSURLCacheDemo[27848:907] 數(shù)據(jù)長度為 = 351042013-01-31 15:28:31.846 NSURLCacheDemo[27848:907] 請求完成

我們看到?jīng)]有“將緩存輸出”一項,請求到的數(shù)據(jù)是第一次請求的累積,也就是第二次是從內(nèi)存中獲取數(shù)據(jù)的。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 峨眉山市| 石台县| 梓潼县| 红原县| 都江堰市| 罗定市| 彝良县| 沾化县| 多伦县| 珠海市| 贞丰县| 新竹县| 东阳市| 曲沃县| 正镶白旗| 平乡县| 沙雅县| 富蕴县| 安阳县| 内丘县| 瑞昌市| 万全县| 梓潼县| 松潘县| 邻水| 辛集市| 泉州市| 鹿邑县| 堆龙德庆县| 锡林浩特市| 晋州市| 沙湾县| 鄄城县| 固安县| 徐闻县| 嘉义市| 彩票| 上虞市| 鄂伦春自治旗| 洪洞县| 黄大仙区|