当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS系统缓存方面开发的相关基础

iOS系统缓存方面开发的相关基础

2019年07月24日  | 移动技术网IT编程  | 我要评论

千金填房,泓德基金管理有限公司,功夫西游精力值

一、关于同一个url的多次请求
 
  有时候,对同一个url请求多次,返回的数据可能都是一样的,比如服务器上的某张图片,无论下载多少次,返回的数据都是一样的。

上面的情况会造成以下问题
 
(1)用户流量的浪费
 
(2)程序响应速度不够快
 
解决上面的问题,一般考虑对数据进行缓存。
 
 
二、缓存

 
  为了提高程序的响应速度,可以考虑使用缓存(内存缓存\硬盘缓存)

第一次请求数据时,内存缓存中没有数据,硬盘缓存中没有数据。
 
缓存数据的过程

当服务器返回数据时,需要做以下步骤
 
(1)使用服务器的数据(比如解析、显示)
 
(2)将服务器的数据缓存到硬盘(沙盒)
 
此时缓存的情况是:内存缓存中有数据,硬盘缓存中有数据。
 
再次请求数据分为两种情况:
 
(1)如果程序并没有被关闭,一直在运行
 
  那么此时内存缓存中有数据,硬盘缓存中有数据。如果此时再次请求数据,直接使用内存缓存中的数据即可
 
(2)如果程序重新启动
 
  那么此时内存缓存已经消失,没有数据,硬盘缓存依旧存在,还有数据。如果此时再次请求数据,需要读取内存中缓存的数据。
 
提示:从硬盘缓存中读取数据后,内存缓存中又有数据了
 
 
 
三、缓存的实现
 
1.说明:
 
由于get请求一般用来查询数据,post请求一般是发大量数据给服务器处理(变动性比较大)
 
因此一般只对get请求进行缓存,而不对post请求进行缓存
 
  在ios中,可以使用nsurlcache类缓存数据
 
  ios 5之前:只支持内存缓存。从ios 5开始:同时支持内存缓存和硬盘缓存
 
 
 
2.nsurlcache
 
ios中得缓存技术用到了nsurlcache类。
 
缓存原理:一个nsurlrequest对应一个nscachedurlresponse
 
缓存技术:把缓存的数据都保存到数据库中。
 
 
 
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 // 未实现

 


 
6.缓存的注意事项
 
缓存的设置需要根据具体的情况考虑,如果请求某个url的返回数据:
 
  (1)经常更新:不能用缓存!比如股票、彩票数据
 
  (2)一成不变:果断用缓存
 
  (3)偶尔更新:可以定期更改缓存策略 或者 清除缓存
 
提示:如果大量使用缓存,会越积越大,建议定期清除缓存

四、本地缓存开发相关
为了节约流量,同时也是为了更好的用户体验,目前很多应用都使用本地缓存机制,其中以网易新闻的缓存功能最为出色。我自己的应用也想加入本地缓存的功能,于是我从网上查阅了相关的资料,发现总体上说有两种方法。一种是自己写缓存的处理,一种是采用asihttprequest中的asidownloadcache。

方法一:一般将服务器第一次返回的数据保存在沙盒里面。这样在手机断网的情况下可以从本地读取数据了。
1.保存到沙盒的代码:
 

复制代码 代码如下:

+ (void)savecache:(int)type andid:(int)_id andstring:(nsstring *)str; 

    nsuserdefaults * setting = [nsuserdefaults standarduserdefaults]; 
    nsstring * key = [nsstring stringwithformat:@"detail-%d-%d",type, _id]; 
    [setting setobject:str forkey:key]; 
    [setting synchronize]; 


2.读取本地沙盒的代码
 
读取之前首先根据type和id判断本地是否有
 
复制代码 代码如下:

+ (nsstring *)getcache:(int)type andid:(int)_id 

    nsuserdefaults * settings = [nsuserdefaults standarduserdefaults]; 
    nsstring *key = [nsstring stringwithformat:@"detail-%d-%d",type, _id]; 
     
    nsstring *value = [settings objectforkey:key]; 
    return value; 


如果沙盒里面有数据
 
复制代码 代码如下:

nsstring *value = [tool getcache:5 andid:self.qiutime]; 
        if (value) { 
            nsdictionary *backdict = [value jsonvalue]; 
            if ([backdict objectforkey:@"items"]) { 
                nsarray *array=[nsarray arraywitharray:[backdict objectforkey:@"items"]]; 
                for (nsdictionary *qiushi in array) { 
                    qiushi *qs=[[[qiushi alloc]initwithdictionary:qiushi] autorelease]; 
                    [self.list addobject:qs]; 
                } 
            } 
            [self.tableview reloaddata]; 
            
        } 
         
        [self.tableview tableviewdidfinishedloadingwithmessage:@"数据全部加载完了.."]; 
        self.tableview.reachedtheend  = yes; 


方法二:使用asihttprequest和asidownloadcache实现本地缓存
 
1、设置全局的cache
    在appdelegate.h中添加一个全局变量
复制代码 代码如下:

@interface appdelegate : uiresponder  

    asidownloadcache *mycache; 

@property (strong, nonatomic) uiwindow *window; 
@property (nonatomic,retain) asidownloadcache *mycache; 


   在appdelegate.m中的- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions方法中添加如下代码

复制代码 代码如下:

//自定义缓存 
asidownloadcache *cache = [[asidownloadcache alloc] init]; 
self.mycache = cache; 
[cache release]; 
     
//设置缓存路径 
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); 
nsstring *documentdirectory = [paths objectatindex:0]; 
[self.mycache setstoragepath:[documentdirectory stringbyappendingpathcomponent:@"resource"]]; 
[self.mycache setdefaultcachepolicy:asionlyloadifnotcachedcachepolicy]; 
  
 
    在appdelegate.m中的dealloc方法中添加如下语句
 
复制代码 代码如下:

[mycache release]; 

    到这里为止,就完成了全局变量的声明。
 
    2、设置缓存策略
    在实现asihttprequest请求的地方设置request的存储方式,代码如下

复制代码 代码如下:

nsstring *str = @"http://....../getpicturenews.aspx"; 
nsurl *url = [nsurl urlwithstring:str]; 
asihttprequest *request = [asihttprequest requestwithurl:url]; 
//获取全局变量 
appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; 
//设置缓存方式 
[request setdownloadcache:appdelegate.mycache]; 
//设置缓存数据存储策略,这里采取的是如果无更新或无法联网就读取缓存数据 
[request setcachestoragepolicy:asicachepermanentlycachestoragepolicy]; 
request.delegate = self; 
[request startasynchronous]; 

    3、清理缓存数据
 
    我在这里采用的是手动清理数据的方式,在适当的地方添加如下代码,我将清理缓存放在了应用的设置模块:

复制代码 代码如下:

appdelegate *appdelegate = [[uiapplication sharedapplication] delegate]; 
[appdelegate.mycache clearcachedresponsesforstoragepolicy:asicachepermanentlycachestoragepolicy];  
 

    这里清理的是asicachepermanentlycachestoragepolicy这种存储策略的缓存数据,如果更换其他的参数的话,即可清理对应存储策略的缓存数据。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网