您的位置:首页 > 移动开发 > IOS开发

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

2013-04-13 19:07 309 查看
引入:
在IOS应用程序开发中,为了减少与服务端的交互次数,加快用户的响应速度,一般都会在IOS设备中加一个缓存的机制。下面将介绍如何在IOS设备中进行缓存。
目的:
使用缓存的目的是为了使用的应用程序能更快速的响应用户输入,是程序高效的运行。有时候我们需要将远程web服务器获取的数据缓存起来,减少对同一个url多次请求。
解读:
内存缓存我们可以使用sdk中的NSURLCache类。NSURLRequest需要一个缓存参数来说明它请求的url何如缓存数据的,我们先看下它的CachePolicy类型。

1、NSURLRequestUseProtocolCachePolicy
NSURLRequest默认的cache policy,使用Protocol协议定义。
2、NSURLRequestReloadIgnoringCacheData
忽略缓存直接从原始地址下载。
3、NSURLRequestReturnCacheDataElseLoad
只有在cache中不存在data时才从原始地址下载。
4、NSURLRequestReturnCacheDataDontLoad
只使用cache数据,如果不存在cache,请求失败;用于没有建立网络连接离线模式;
5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和远程的缓存数据,直接从原始地址下载,与NSURLRequestReloadIgnoringCacheData类似。
6、NSURLRequestReloadRevalidatingCacheData:验证本地数据与远程数据是否相同,如果不同则下载远程数据,否则使用本地数据。

NSURLCache还提供了很多方法,来方便我们实现应用程序的缓存机制。
实现:
//
//
RootViewController.h
//
cacheDemo
//
(内存缓存demo-声明)
//
Created by iMilo TEANA on 12-2-1.
//
Copyright (c) 2012年 TEANA. All rights reserved.
//

#import
<UIKit/UIKit.h>

@interface
RootViewController : UIViewController
<NSURLConnectionDelegate>

- (IBAction)cachePress:(UIButton
*)button;

@end

//
//
RootViewController.m
//
cacheDemo
//
(内存缓存demo-定义)
//
Created by iMilo TEANA on 12-2-1.
//
Copyright (c) 2012年 TEANA. All rights reserved.
//

#import
"RootViewController.h"

@interface
RootViewController()

@property(nonatomic,
retain)
NSURLConnection *connection;

@end

@implementation
RootViewController

@synthesize
connection = _connection;

#pragma mark -
#pragma mark - super code

- (void)dealloc
{

[_connection
release];

[super
dealloc];
}

- (void)viewDidLoad
{

[super
viewDidLoad];

[[self
view]
setBackgroundColor:[UIColor
redColor]];
}

- (void)viewDidUnload
{

[super
viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return (interfaceOrientation ==
UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark - user code

- (IBAction)cachePress:(UIButton
*)button
{

//URL

NSString *URLStr =
@"http://www.baidu.com";

if ([URLStr
length] ==
0)

{

NSLog(@"Nil
or empty URL is given");

return;

}

//设置缓存

NSURLCache *urlCache = [NSURLCache
sharedURLCache];

//设置缓存大小未1M

[urlCache setMemoryCapacity:1*1024*1024];

//创建URL

NSURL *URL = [NSURL
URLWithString:URLStr];

//创建请求

NSMutableURLRequest *request =


[NSMutableURLRequest
requestWithURL:URL


cachePolicy:NSURLRequestUseProtocolCachePolicy
//缓存策略(默认)

timeoutInterval:60.0f];//

//从请求中获取缓存输出

NSCachedURLResponse *response = [urlCache
cachedResponseForRequest:request];

//判断是否有缓存

if (response !=
nil)


{

NSLog(@"如果有缓存,从缓存中获取数据");

[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];

}

//将链接置为空

_connection =
nil;

//重新创建链接

NSURLConnection *connection =


[[NSURLConnection
alloc]
initWithRequest:request


delegate:self


startImmediately:YES];

[self
setConnection:connection];

[connection release];

connection = nil;
}

#pragma mark -
#pragma mark - delegate code

- (void)connection:(NSURLConnection
*)connection
idReceiveResponse:(NSURLResponse
*)response
{

NSLog(@"将要接收数据");
}

- (NSURLRequest
*)connection:(NSURLConnection *)connection

willSendRequest:(NSURLRequest *)request

redirectResponse:(NSURLResponse *)redirectResponse
{

NSLog(@"即将发送请求");

return request;
}

- (void)connection:(NSURLConnection
*)connection

didReceiveData:(NSData *)data
{


NSLog(@"接收数据,数据长度为:%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
原文:http://mmz06.blog.163.com/blog/static/1214169620121131829877/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: