Fork me on GitHub

IOS开发缓存机制----内存缓存机制

 

引入:

    IOS应用程序开发中,为了减少与服务端的交互次数,加快用户的响应速度,一般都会在IOS设备中加一个缓存的机制。下面将介绍如何在IOS设备中进行缓存。

目的:

    使用缓存的目的是为了使用的应用程序能更快速的响应用户输入,是程序高效的运行。有时候我们需要将远程web服务器获取的数据缓存起来,减少对同一个url多次请求。

解读:

    内存缓存我们可以使用sdk中的NSURLCache类。NSURLRequest需要一个缓存参数来说明它请求的url何如缓存数据的,我们先看下它的CachePolicy类型。

 

1NSURLRequestUseProtocolCachePolicy NSURLRequest默认的cache policy,使用Protocol协议定义。

2NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载。

3NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载。

4NSURLRequestReturnCacheDataDontLoad 只使用cache数据,如果不存在cache,请求失败;用于没有建立网络连接离线模式;

5NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和远程的缓存数据,直接从原始地址下载,与NSURLRequestReloadIgnoringCacheData类似。

6NSURLRequestReloadRevalidatingCacheData:验证本地数据与远程数据是否相同,如果不同则下载远程数据,否则使用本地数据。

 

NSURLCache还提供了很多方法,来方便我们实现应用程序的缓存机制。

实现:

  1 //  RootViewController.h
2
3 // cacheDemo
4
5 // (内存缓存demo-声明)
6
7 // Created by iMilo TEANA on 12-2-1.
8
9 // Copyright (c) 2012年 TEANA. All rights reserved.
10
11 //
12
13
14
15 #import <UIKit/UIKit.h>
16
17
18
19 @interface RootViewController : UIViewController
20
21 <NSURLConnectionDelegate>
22
23
24
25 - (IBAction)cachePress:(UIButton *)button;
26
27
28
29 @end
30
31
32
33 //
34
35 // RootViewController.m
36
37 // cacheDemo
38
39 // (内存缓存demo-定义)
40
41 // Created by iMilo TEANA on 12-2-1.
42
43 // Copyright (c) 2012年 TEANA. All rights reserved.
44
45 //
46
47
48
49 #import "RootViewController.h"
50
51
52
53 @interface RootViewController()
54
55
56
57 @property(nonatomic, retain) NSURLConnection *connection;
58
59
60
61 @end
62
63
64
65 @implementation RootViewController
66
67
68
69 @synthesize connection = _connection;
70
71
72
73 #pragma mark -
74
75 #pragma mark - super code
76
77
78
79 - (void)dealloc
80
81 {
82
83 [_connection release];
84
85 [super dealloc];
86
87 }
88
89
90
91 - (void)viewDidLoad
92
93 {
94
95 [super viewDidLoad];
96
97 [[self view] setBackgroundColor:[UIColor redColor]];
98
99 }
100
101
102
103 - (void)viewDidUnload
104
105 {
106
107 [super viewDidUnload];
108
109 }
110
111
112
113 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
114
115 {
116
117 return (interfaceOrientation == UIInterfaceOrientationPortrait);
118
119 }
120
121
122
123 #pragma mark -
124
125 #pragma mark - user code
126
127
128
129 - (IBAction)cachePress:(UIButton *)button
130
131 {
132
133 //URL
134
135 NSString *URLStr = @"http://www.baidu.com";
136
137 if ([URLStr length] == 0)
138
139 {
140
141 NSLog(@"Nil or empty URL is given");
142
143 return;
144
145 }
146
147 //设置缓存
148
149 NSURLCache *urlCache = [NSURLCache sharedURLCache];
150
151 //设置缓存大小未1M
152
153 [urlCache setMemoryCapacity:1*1024*1024];
154
155 //创建URL
156
157 NSURL *URL = [NSURL URLWithString:URLStr];
158
159 //创建请求
160
161 NSMutableURLRequest *request =
162
163 [NSMutableURLRequest requestWithURL:URL
164
165 cachePolicy:NSURLRequestUseProtocolCachePolicy //缓存策略(默认)
166
167 timeoutInterval:60.0f];//
168
169 //从请求中获取缓存输出
170
171 NSCachedURLResponse *response = [urlCache cachedResponseForRequest:request];
172
173 //判断是否有缓存
174
175 if (response != nil)
176
177 {
178
179 NSLog(@"如果有缓存,从缓存中获取数据");
180
181 [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
182
183 }
184
185 //将链接置为空
186
187 _connection = nil;
188
189 //重新创建链接
190
191 NSURLConnection *connection =
192
193 [[NSURLConnection alloc] initWithRequest:request
194
195 delegate:self
196
197 startImmediately:YES];
198
199 [self setConnection:connection];
200
201 [connection release];
202
203 connection = nil;
204
205 }
206
207
208
209 #pragma mark -
210
211 #pragma mark - delegate code
212
213
214
215 - (void)connection:(NSURLConnection *)connection
216
217 idReceiveResponse:(NSURLResponse *)response
218
219 {
220
221 NSLog(@"将要接收数据");
222
223 }
224
225
226
227 - (NSURLRequest *)connection:(NSURLConnection *)connection
228
229 willSendRequest:(NSURLRequest *)request
230
231 redirectResponse:(NSURLResponse *)redirectResponse
232
233 {
234
235 NSLog(@"即将发送请求");
236
237 return request;
238
239 }
240
241
242
243 - (void)connection:(NSURLConnection *)connection
244
245 didReceiveData:(NSData *)data
246
247 {
248
249 NSLog(@"接收数据,数据长度为:%lu",(unsigned long)[data length]);
250
251 }
252
253
254
255 - (NSCachedURLResponse *)connection:(NSURLConnection *)connection
256
257 willCacheResponse:(NSCachedURLResponse *)cachedResponse
258
259 {
260
261 NSLog(@"将缓存输出");
262
263 return(cachedResponse);
264
265 }
266
267
268
269 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
270
271 {
272
273 NSLog(@"请求完成");
274
275 }
276
277
278
279 - (void)connection:(NSURLConnection *)connection
280
281 didFailWithError:(NSError *)error
282
283 {
284
285 NSLog(@"请求失败");
286
287 }
288
289
290
291 @end



其他:

iMilo

2012-02-01

 

 

posted on 2012-02-08 18:57  pengyingh  阅读(6351)  评论(0编辑  收藏  举报

导航