Understanding Cache Access
URL Loading System提供了综合的disk 和 in-memory 策略的请求缓存。使用缓存有利于减少程序对网络的依赖,并且能提高程序的体验。
Using the Cache for a Request
在NSURLResquest实例中使用一个NSURLRequestCachePolicy值,去制定怎么样本地的缓存的策略。
NSURLRequestUseProtocolCachePolicy, 使用协议默认,自动默认的的会提供比较合适的策略。
NSURLRequestReloadIgnoringCacheData,使用网络数据,不是使用缓存的数据
NSURLRequestReturnCacheDataElseLoad,使用缓存数据,不管缓存数据的时间和截止日期,如果实在找不到,再用网络。
orNSURLRequestReturnCacheDataDontLoad.让网络的请求中,只有本地有数据缓存的才返回数据。如果本地没有缓存的数据,那么即便创建NSURLConnection或者NSURLDownload去请求也是不行的。这个有点离线的意思。
当前只有HTTP和HTTPS request是被缓存的,FTP和file 协议会尝试去获取originating数据在cache policy允许的情况下。定义NSURLProtocol类,可以选择行进行缓存。
Cache Use Senmantics for the HTTP Protocol
日常中比较复杂的的缓存使用情况是,当一个请求是用HTTP协议并且和设置缓存的策略为NSURLReqestUseProtocolCachePolicy。
如果一个request并没有NSCacheURLResponse,那么URL loading系统会从源数据出获取数据。
如果request存在cache response,URL loading系统会检查这个response,然后再去决定这个request的内容是特殊的,需要重新去验证。
如果content必须要重新验证,那么URL loading 系统会创建一个HEAD request去发给服务器,判断现有的资源有没有改变了。如果content没有改变,URL Loading系统会返回 cacheed response。如果变化了,URL Loading系统就重新获取数据。
如果cached response 没有指定说必须需要去验证,那么URL Loading系统检查一下缓存的response存在的最大过期时间。如果缓存的response足够新,那么就直接返回,如果不够新,就发送一个HEAD request给服务器,然后决定需要的resource是否已经改变,如果已经改变就就重新获取,如果没有改变就返回cached的response。
Controlling Cache Programmatically
默认情况下,连接产生的数据会依赖于request的cache policy的策略而缓存下来,就像NSURLProtocol子类控制的那样。
如果你的程序需要精准的去控制你的缓存的数据(在protocol支持cache的情况下),你可以实现一个delegate的方法以允许使用一种pre-request(预请求)的方式决定request是否需要特别去缓存。
- 对于NSURLSession来说,data和upload task,实现
URLSession:dataTask:willCacheResponse:completionHandler:方法。这个代理方法这会在data and upload task的时候被调用。关于download task的缓存策略是被特殊指定的。 - 对于NSURLConnection,实现
connection:willCacheResponse:method.
对于你的NSURLSession来说,delagate方法调用completion handler block去告诉session,那些需要去被缓存。对于NSURLConnection,delegate返回那些连接过程中需要被缓存的对象。
这两种例子中,delegate典型的提供下面之一的解决方案:
- 提供provided response对象去允许caching
- A newly created response object to cache a modified response—for example, a response with a storage policy that allows caching to memory but not to disk
NULLto prevent caching
delagate尅把response对象存入到userinfo的字典中然后于NSCacheURLResponse对象建立联系。这样就是response都存下来了。
注意:如果使用NSURLSession并且实现这个代理方法,你的代理方法必须掉调用completion handler,不然就回内存泄露。
7-1是阻止on-desk caching HTTP response的例子,只把HTTPF response放到内存中去。并且把缓存的response的时间添加到user info字典中去了。
-(NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSCachedURLResponse *newCachedResponse = cachedResponse; NSDictionary *newUserInfo;
newUserInfo = [NSDictionary dictionaryWithObject:[NSDate date]
forKey:@"Cached Date"];
if ([[[[cachedResponse response] URL] scheme] isEqual:@"https"]) {
#if ALLOW_IN_MEMORY_CACHING
newCachedResponse = [[NSCachedURLResponse alloc]
initWithResponse:[cachedResponse response]
data:[cachedResponse data]
userInfo:newUserInfo
storagePolicy:NSURLCacheStorageAllowedInMemoryOnly];
#else // !ALLOW_IN_MEMORY_CACHING
newCachedResponse = nil
#endif // ALLOW_IN_MEMORY_CACHING
} else {
newCachedResponse = [[NSCachedURLResponse alloc]
initWithResponse:[cachedResponse response]
data:[cachedResponse data]
userInfo:newUserInfo
storagePolicy:[cachedResponse storagePolicy]];
}
return newCachedResponse;
}
Understanding Cache Access的更多相关文章
- 2.SDL规范文档
01.安全设计Checklist 输入验证 校验跨信任边界传递的不可信数据(策略检查数据合法性,含白名单机制等)格式化字符串时,依然要检验用户输入的合法性,避免可造成系统信息泄露或者拒绝服务 禁止向J ...
- Parallelized coherent read and writeback transaction processing system for use in a packet switched cache coherent multiprocessor system
A multiprocessor computer system is provided having a multiplicity of sub-systems and a main memory ...
- Cache memory power reduction techniques
Methods and apparatus to provide for power consumption reduction in memories (such as cache memories ...
- 如何使用event 10049分析定位library cache lock and library cache pin
Oracle Library Cache 的 lock 与 pin 说明 一. 相关的基本概念 之前整理了一篇blog,讲了Library Cache 的机制,参考: Oracle Library c ...
- Guava Cache本地缓存
Guava介绍 Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库. 这个库是为了方便编码,并减少编码错误. 这个库提供用于集合,缓存,支持原语,并发性,常见注解,字 ...
- PSEUDO LEAST RECENTLY USED (PLRU) CACHE REPLACEMENT
A multi-way cache system includes multi-way cache storage circuitry, a pseudo least recently used (P ...
- Partitioned Replacement for Cache Memory
In a particular embodiment, a circuit device includes a translation look-aside buffer (TLB) configur ...
- Virtual address cache memory, processor and multiprocessor
An embodiment provides a virtual address cache memory including: a TLB virtual page memory configure ...
- Oracle-buffer cache、shared pool
http://blog.csdn.net/panfelix/article/details/38347059 buffer pool 和shared pool 详解 http://blog.csd ...
随机推荐
- 【洛谷 P2900】 [USACO08MAR]土地征用Land Acquisition(斜率优化,单调栈)
题目链接 双倍经验 设\(H\)表示长,\(W\)表示宽. 若\(H_i<H_j\)且\(W_i<W_j\),显然\(i\)对答案没有贡献. 于是把所有点按\(H\)排序,然后依次加入一个 ...
- 【leetcode 简单】第一题 两数之和
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...
- Vue 脱坑记
问题汇总 Q:安装超时(install timeout) 方案有这么些: cnpm : 国内对npm的镜像版本 /* cnpm website: https://npm.taobao.org/ */ ...
- L - SOS Gym - 101775L 博弈
题目链接:https://cn.vjudge.net/contest/274151#problem/L 题目大意:给你一个1*n的方格,两个人轮流放字母,每一次可以放"S"或者&q ...
- Metlnfo CMS全版本漏洞收集
根据https://www.seebug.org/appdir/MetInfo 进行书写. [版本:Metlnfo 4.0] 漏洞标题:Metlnfo cms任意用户密码修改 漏洞文件:member/ ...
- Android上HDMI介绍(基于高通平台)
本文重点针对HDMI在android上的应用,而比较相关的就是overlay机制.overlay在这里只是简单的介绍,后续会有文章再专门详述. 我没记错的话,高通从7X30开始,平台就可以支持HDMI ...
- Math类的数学计算功能
//Math类的数学计算功能 public class MathTest { public static void main(String[] args) { /*----------下面是三角运算- ...
- linux shell 一些命令
https://stackoverflow.com/questions/918886/how-do-i-split-a-string-on-a-delimiter-in-bash wc: https: ...
- OPENSSL问题,使用fsockopen()函数提示错误
环境配置 系统环境 CentOS7.2WDCP v3.2.2 lanmp PHP 多版本 指定使用5.6 OpenSSL 1.0.2h 3 May 2016 php.ini相关设置allow_url ...