ios-数据缓存随笔

数据缓存,就是把从网络上加载过来的数据,存到本地,目的是方便以后调用相同数据的时候,直接读缓存,不需要网络请求,既节省了client端的时间成本,有节省了serve端的负载成本。

ios当中,缓存的对象NSUrlCache,iOS 5之前:只支持内存缓存。从iOS 5开始:同时支持内存缓存和硬盘缓存。

3.NSURLCache的常见用法

(1)获得全局缓存对象(没必要手动创建)NSURLCache *cache = [NSURLCache sharedURLCache]; 

(2)设置内存缓存的最大容量(字节为单位,默认为512KB)- (void)setMemoryCapacity:(NSUInteger)memoryCapacity;

(3)设置硬盘缓存的最大容量(字节为单位,默认为10M)- (void)setDiskCapacity:(NSUInteger)diskCapacity;

(4)硬盘缓存的位置:沙盒/Library/Caches

//在这里不需要设置内存中缓存的位置,因为这个位置是计算机自动分配的。只需设置硬盘缓存的位置即可。

(5)取得某个请求的缓存- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request; 

(6)清除某个请求的缓存- (void)removeCachedResponseForRequest:(NSURLRequest *)request;

(7)清除所有的缓存- (void)removeAllCachedResponses;

4.缓存GET请求

  要想对某个GET请求进行数据缓存,非常简单

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

  // 设置缓存策略

  request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;

  只要设置了缓存策略,系统会自动利用NSURLCache进行数据缓存 

5.iOS对NSURLRequest提供了7种缓存策略:(实际上能用的只有4种)

NSURLRequestUseProtocolCachePolicy // 默认的缓存策略(取决于协议)

NSURLRequestReloadIgnoringLocalCacheData // 忽略缓存,重新请求

NSURLRequestReloadIgnoringLocalAndRemoteCacheData // 未实现

NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData // 忽略缓存,重新请求

NSURLRequestReturnCacheDataElseLoad// 有缓存就用缓存,没有缓存就重新请求

NSURLRequestReturnCacheDataDontLoad// 有缓存就用缓存,没有缓存就不发请求,当做请求出错处理(用于离线模式)

NSURLRequestReloadRevalidatingCacheData // 未实现

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 2 {
 3     // 1.创建请求
 4     NSURL *url = [NSURL URLWithString:@"http://127.0.0.1:8080/YYServer/video"];
 5     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
 6     
 7     // 2.设置缓存策略(有缓存就用缓存,没有缓存就重新请求)
 8     request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
 9     
10     // 3.发送请求
11     [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
12         if (data) {
13             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
14             
15             NSLog(@"%@", dict);
16         }
17     }];
18 }
19 
20 /**
21  // 定期处理缓存
22  //    if (缓存没有达到7天) {
23  //        request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
24  //    }
25  // 获得全局的缓存对象
26  NSURLCache *cache = [NSURLCache sharedURLCache];
定期清理缓存的实现:
服务端返回时间(12312312312秒),客户端转化成天数判断,如果大于七天(这里比如说是七天缓存时间),那么先清理缓存,再请求数据,如果小于七天,读取缓存。
27 // if (缓存达到7天) 
{
28 // [cache removeCachedResponseForRequest:request];
29 // }
30
31 // lastCacheDate = 2014-06-30 11:04:30
32
33 NSCachedURLResponse *response = [cache cachedResponseForRequest:request];
34 if (response)
{
35 NSLog(@"---这个请求已经存在缓存");
36
}
else { 37 NSLog(@"---这个请求没有缓存"); 38 }

总结:

   AFNetworking,应该内部实现了缓存机制,有缓存就先加载内存中得缓存,没缓存再重新请求。(AFNetWorking,应该集成了NSUrlCache,网络类发送异步请求)。

定期清理缓存的实现:

服务端返回时间(12312312312秒),客户端转化成天数判断,如果大于七天(这里比如说是七天缓存时间),那么先清理缓存,再请求数据,如果小于七天,读取缓存。

貌似也可以实现client端进行时间对比,定期清理缓存,有想法的同学可以想一下。
原文地址:https://www.cnblogs.com/rankilau/p/4571345.html