銆€iOS 缃戠粶璇锋眰缂撳瓨锛歂SURLCache璇﹁В

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆€iOS 缃戠粶璇锋眰缂撳瓨锛歂SURLCache璇﹁В相关的知识,希望对你有一定的参考价值。

鏍囩锛?/p>

鎴戣杩囦竴浜涘紑婧愰」鐩殑缃戠粶璇锋眰缂撳瓨鐨勪唬鐮侊紝鍩烘湰涓婇兘鏄噰鐢ㄥ湪鏈湴瀛樻枃浠剁殑鏂瑰紡杩涜缂撳瓨銆傚鏋滀綘鎵撶畻鍦ㄤ綘鐨勯」鐩腑鍔犲叆缃戠粶璇锋眰鐨勭紦瀛橈紝鍙兘浣犲苟涓嶉渶瑕佽嚜宸遍€犱竴涓疆瀛愶紝浜?/span>瑙d竴涓?/span> NSURLCache 灏辫冻澶熴€?/span>鏈枃涓哄ぇ瀹舵帴鏀剁殑灏辨槸ios寮€鍙?/span>涓殑NSURLCache鐩稿叧浣跨敤锛屼竴璧锋潵鐪嬬湅鍚с€?

缂撳瓨

棣栧厛锛?/span> NSURLCache 鎻愪緵鐨勬槸鍐呭瓨浠ュ強纾佺洏鐨勭患鍚堢紦瀛樻満鍒躲€傝澶氭枃绔犺皥鍒帮紝浣跨敤NSURLCache 涔嬪墠闇€瑕佸湪 AppDelegate 涓紦瀛樼┖闂寸殑璁剧疆锛?/span>

- (BOOL)application:(UIApplication *)application

didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

NSURLCache *URLCache = [[NSURLCachealloc] initWithMemoryCapacity:4 * 1024 * 1024

diskCapacity:20 * 1024 * 1024

diskPath:nil];

[NSURLCachesetSharedURLCache:URLCache];

}

鐒惰€屽鏋滀綘涓嶆坊鍔犱笂闈㈢殑浠g爜锛屽苟涓旇繍琛屽涓嬩唬鐮侊紝鍙互鐪嬪埌锛?/span>

print(NSURLCache.sharedURLCache().diskCapacity)//output://10000000

print(NSURLCache.sharedURLCache().memoryCapacity)//output://512000

涔熷氨鏄锛屽叾瀹為粯璁ゅ氨宸茬粡璁剧疆濂戒簡512kb鐨勫唴瀛樼紦瀛樼┖闂达紝浠ュ強10MB鐨勭鐩樼紦瀛樼┖闂淬€傚彲鑳戒綘鐨勪唬鐮佷腑骞舵病鏈夊啓浠讳綍涓?NSURLCache 鏈夊叧鐨勪笢瑗匡紝浣嗗叾瀹炲畠宸茬粡榛橀粯鐨勫紑濮嬪府浣犺繘琛岀紦瀛樹簡銆?/span>

宸茬粡缂撳瓨涓婁簡锛屼絾鏄€庝箞浣跨敤缂撳瓨鍛紵璇风户缁線涓嬨€?/span>

缂撳瓨绛栫暐

GET

涓嶇敤澶氳锛?/span> NSURLCache 鍙細瀵逛綘鐨?/span> GET 璇锋眰杩涜缂撳瓨銆?/span>

NSURLRequestCachePolicy

NSURLRequest 涓湁涓睘鎬э細

public var cachePolicy: NSURLRequestCachePolicy { get }

浣犲彲浠ラ€氳繃杩欎釜灞炴€ф潵璁剧疆璇锋眰鐨勭紦瀛樼瓥鐣ワ紝

public enum NSURLRequestCachePolicy : UInt {

case UseProtocolCachePolicy // 榛樿鍊?/span>

case ReloadIgnoringLocalCacheData // 涓嶄娇鐢ㄧ紦瀛樻暟鎹?/span>

case ReloadIgnoringLocalAndRemoteCacheData // Unimplemented

public static var ReloadIgnoringCacheData: NSURLRequestCachePolicy { get }

case ReturnCacheDataElseLoad // 鏃犺缂撳瓨鏄惁杩囨湡閮芥槸鐢ㄧ紦瀛橈紝娌℃湁缂撳瓨灏辫繘琛岀綉缁滆姹?/span>

case ReturnCacheDataDontLoad // 鏃犺缂撳瓨鏄惁杩囨湡閮芥槸鐢ㄧ紦瀛橈紝娌℃湁缂撳瓨涔熶笉浼氳繘琛岀綉缁滆姹?/span>

case ReloadRevalidatingCacheData // Unimplemented

}

鍏跺疄鍏朵粬鍑犱釜鍊奸兘姣旇緝濂界悊瑙o紝鍞嫭榛樿鍊?/span> UseProtocolCachePolicy 璁╂垜涓嶅お鎳傘€?/span>

瀛楅潰涓婄殑鎰忔€濇槸 鎸夌収鍗忚鐨勭紦瀛樼瓥鐣ヨ繘琛岀紦瀛?/span> 锛岄偅涔堣繖鏄粈涔堝崗璁憿锛?/span> http鍗忚銆?/span>

璇︾粏锛?/span>RFC 2616, Section 13

鏈嶅姟鍣ㄨ繑鍥炵殑鍝嶅簲澶翠腑浼氭湁杩欐牱鐨勫瓧娈碉細 Cache-Control: max-age or Cache-Control: s- maxage 锛岄€氳繃 Cache-Control 鏉ユ寚瀹氱紦瀛樼瓥鐣ワ紝 max-age 鏉ヨ〃绀鸿繃鏈熸椂闂淬€傛牴鎹繖浜涘瓧娈电紦瀛樻満鍒跺啀閲囩敤濡備笅绛栫暐锛?/span>

濡傛灉鏈湴娌℃湁缂撳瓨鏁版嵁锛屽垯杩涜缃戠粶璇锋眰銆?/span>

濡傛灉鏈湴鏈夌紦瀛橈紝骞朵笖缂撳瓨娌℃湁澶辨晥锛屽垯浣跨敤缂撳瓨銆?/span>

濡傛灉缂撳瓨宸茬粡澶辨晥锛屽垯璇㈤棶鏈嶅姟鍣ㄦ暟鎹槸鍚︽敼鍙橈紝濡傛灉娌℃敼鍙橈紝渚濈劧浣跨敤缂撳瓨锛屽鏋滄敼鍙樹簡鍒欒姹傛柊鏁版嵁銆?/span>

濡傛灉娌℃湁鎸囧畾鏄惁澶辨晥锛岄偅涔堢郴缁熷皢鑷繁鍒ゆ柇缂撳瓨鏄惁澶辨晥銆傦紙閫氬父璁や负鏄?/span>6-24灏忔椂鐨勬湁鏁堟椂闂达級

鍏跺疄鎴戜互鍓嶅 Cache-Control 涔嬬被鐨勪篃骞朵笉澶簡瑙?/span> T_T锛岃嚜宸遍粯榛樼殑print浜嗕竴涓嬪搷搴斿ご锛屼綘鍙互鐪嬪埌锛?/span>

print((responseas? NSHTTPURLResponse)?.allHeaderFields)

//鍝嶅簲澶翠腑锛?/span>Cache-Control: no-cache

杩欎篃灏辨槸涓轰粈涔堬紝铏界劧 NSURLCache 涓€鐩村湪榛橀粯鐨勭紦瀛橈紝浣嗘槸鎴戝苟娌℃湁鎰熷彈鍒帮紝褰撶劧鎴栬浣犻偅閲屼笉涓€鏍枫€傝繖涓?/span> no-cache 灏辫〃绀轰笉缂撳瓨銆?/span>

杩欓噷瑕侀澶栨彁涓€鍙ワ紝鐪嬪埌缃戜笂鏈夊悓瀛﹁鑷繁鍑虹幇浜嗘煇涓姹傛暟鎹竴鐩翠娇鐢ㄧ紦瀛橈紝娌℃湁琚洿鏂般€傝繖绉嶆儏鍐靛彲鑳藉氨鏄湇鍔″櫒杩斿洖鐨?/span> Cache-Control 鏈夎銆?/span>

鎵撳紑娌欑洅璺緞涓嬬殑Library/Caches 涓紝浣犲彲浠ョ湅鍒扮紦瀛樻枃浠讹細


杩欏彲浠ヨ鏄庡瓨鍦ㄧ鐩樹笂鐨勬暟鎹槸瀛樺湪鏁版嵁搴撻噷鐨勶紝鎬ц兘涓嶇敤鎷呭績銆傛墦寮€鏁版嵁搴撴枃浠跺氨鍙互鐪嬪埌璇锋眰鐨勬暟鎹€?/span>


鍦?/span> cfurl_cache_response 琛ㄤ腑鍙互鐪嬪埌鏈変竴涓瓧娈垫槸 request_key 锛岄€氳繃閲岄潰鐨勫€煎彲浠ユ帹鏂瘡涓€涓?/span> response 鏄€氳繃璇锋眰鐨?/span> url+鍙傛暟 鏉ヤ綔涓?/span> key 鍌ㄥ瓨鐨勩€?/span>

褰撶劧锛岀粡杩囨垜鐨勫娆¤瘯楠岋紝鍦?/span> Cache-Control: no-cache 鐨勬儏鍐典笅锛?/span> NSURLCache涔熶細杩涜缂撳瓨锛屼絾鏄苟涓嶄娇鐢ㄧ紦瀛樻暟鎹€?/span>

鎬荤粨涓€涓嬶細榛樿鎯呭喌涓?/span> NSURLCache 鐨勭紦瀛樼瓥鐣ユ槸鏍规嵁http鍗忚鏉ョ殑锛屾湇鍔″櫒閫氳繃 Cache-Control: max-age 瀛楁鏉ュ憡璇?/span> NSURLCache 鏄惁闇€瑕佺紦瀛樻暟鎹€?/span>

缂撳瓨灏佽

濡傛灉浣犱笉鎵撶畻閲囩敤http鍗忚鐨勭紦瀛樼瓥鐣ワ紝渚濈劧鍙互浣跨敤 NSURLCache 杩涜缂撳瓨銆?/span>

public funccachedResponseForRequest(request: NSURLRequest) -> NSCachedURLResponse?

浣犲彲浠ラ€氳繃杩欎釜鏂规硶锛屼紶鍏ヨ姹傦紝鏉ヨ幏鍙栫紦瀛樸€?/span> NSCachedURLResponse 淇濆瓨浜嗕笂娆¤姹傜殑鏁版嵁浠ュ強鍝嶅簲澶淬€?/span>

public funcstoreCachedResponse(cachedResponse: NSCachedURLResponse, forRequestrequest: NSURLRequest)

NSURLSessionDelegate 鍗忚涓湁濡備笅鏂规硶锛屽彲浠ュ鍗冲皢缂撳瓨鐨勬暟鎹繘琛屼慨鏀癸紝娣诲姞userInfo,鍦ㄤ唬鐞嗘柟娉曚腑蹇呴』璋冪敤completionHandler锛屼紶鍏ュ皢瑕佺紦瀛樼殑鏁版嵁锛屽鏋滀紶nil鍒欒〃绀轰笉缂撳瓨銆?/span>

optionalpublic funcURLSession(session: NSURLSession,

dataTask: NSURLSessionDataTask,

willCacheResponseproposedResponse: NSCachedURLResponse,

completionHandler: (NSCachedURLResponse?) -> Void)

鍦?/span> Alamofire 涓彲浠ヨ繖鏍峰啓锛?/span>

Alamofire.Manager

.sharedInstance

.delegate

.dataTaskWillCacheResponse = { (session, task, cachedResponse) -> NSCachedURLResponse? in

var userInfo = [NSObject : AnyObject]()

// 璁剧疆userInfo

return NSCachedURLResponse(response: cachedResponse.response,

data: cachedResponse.data,

userInfo: userInfo,

storagePolicy: cachedResponse.storagePolicy)

}

 

鏉ユ簮锛?span>浼箰鍦ㄧ嚎



以上是关于銆€iOS 缃戠粶璇锋眰缂撳瓨锛歂SURLCache璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章

DNS鍩熷悕瑙f瀽杩囩▼

缃戠粶缂栫▼ 濂楁帴瀛梥ocket 鍙? 绮樺寘

銆婅绠楁満缃戠粶绯诲垪銆嬧€斺€斾负浠€涔堥渶瑕佷簲灞傜綉缁滄ā鍨嬶紵

MyBatis鎺㈢┒-----缂撳瓨鏈哄埗璇﹁В

ajax寮傛璇锋眰

ajax璺ㄥ煙璇锋眰webservice webconfig閰嶇疆