MENU

iOS App 开发缓存设计

May 12, 2017 • Read: 910 • iOS

缓存类型

缓存策略从功能上划分为两种:一、优化型缓存;二、存储型缓存。如果从形式划分,缓存策略又分为内存缓存和磁盘缓存。如下图所示:
缓存划分

  • 优化型缓存
    优化型缓存,从字面意思来讲即为了优化。主要目的是为了提高用户体验、减少数据流量的浪费、减小服务器压力。
  • 存储型缓存
    存储型缓存正如存储二字,主要是通过预缓存方式,将一些数据缓存起来。用在离线也能查看、使用的应用中。例如:地图、音乐软件等。
  • 内存缓存
    正如我们知道,CPU的处理速度是非常快的,它在处理数据时会先到 Cache 中去寻找,如果命中缓存,就不需要随机存取器中读取数据而造成资源浪费。这是操作系统中的内存缓存定义,而针对 iOS、Android 等平台App而言,我理解中的内存缓存是指从服务器请求到的数据保存在变量或属性中,当下次需要使用的时候,就不用再次从服务器请求资源,从而节约用户流量和时间,提高了用户使用体验。其实App中的内存缓存和操作系统中的内存缓存很相似,App中的资源对应CPU处理的数据,App中对象缓存在内存中的数据对应操作系统的Cache,当App在内存对象中没有命中缓存时向服务器请求资源就好比是CPU向主存访问资源。

App内存缓存过程

  • 磁盘缓存
    在内存缓存中,我们提到在对象缓存中没有命中,我们会向服务器请求资源。其实中间还有一步就是向磁盘缓存请求,当再次没有命中的时候,再向服务器请求。App获取服务器资源后,根据缓存策略决定是否向磁盘进行缓存。如果向磁盘进行缓存,iOS开发中可以向沙盒文件下存入缓存文件。

App磁盘缓存过程

缓存方式

URL缓存

  • URL缓存是指,在请求数据的时候使用URL的方式进行获取。而通过URL请求的数据,只有GET能被缓存,POST是不能被缓存的。因为GET请求的结果总是幂等的。但是如果非要使用POST缓存也是可以的,因为二者从本质上来讲是没有很大区别的,只需要将POST作一些特殊的操作,例如将信息摘要放在URL中,下一次进行POST请求的时候,摘要相同则直接获取缓存。
  • 在iOS开发中,可以使用NSURLCache(Obj-C)、URLCache(Swift)这些类来进行URL缓存。

数据模型缓存

  • 在iOS开发中,很多时候都会使用MVC、MVVM框架进行开发。在这些框架中,都有一个模型。模型即是用来展示的数据。这部分数据可以从服务器获取,当然也可以利用缓存在本地的资源。例如,我们在进行QQ聊天的时候界面上显示的文字、图片等信息,就可以利用数据模型来展示。而我们可以将这些数据模型缓存下来,在你想看聊天记录的时候向上翻一翻就能立马找到,不用向磁盘或者服务器请求。在我的理解中,数据模型缓存很大一方面是针对于内存缓存,因为数据模型是保存在内存中,经常和App打交道。所以数据模型缓存应该保存数据不大,而且经常使用的数据。
  • 当然一些时间比较长、而且不经常使用的数据我们也可以使用数据模型,比如十天前聊过天的好友的聊天记录,我们就可以利用NSKeyedArchiver来进行简单的归档和解档。当对我们自己定义的数据模型对象进行归档的时候,就需要实现NSCoding协议

属性列表化

  • 我们使用的数据除了可以用数据模型进行缓存之外,还可以利用plist文件进行缓存。plist文件以键值对的形式保存数据。正如其名字所示,属性列表化主要用来缓存一些属性、列表文件的一些数据。

数据库缓存

  • 针对一些比较大的数据例如歌曲、地图信息等,可以使用数据库进行缓存。在本地建立数据库,根据需要缓存的数据类型进行建库建表,当需要使用的时候从数据库中读取即可。

缓存方式总结

缓存方式有各种各样,我们可以根据数据的类型和设备来进行选择。但是仅仅有缓存方式是不足以实现整个缓存系统的。还需要有缓存策略的支撑。

缓存策略

  • 缓存策略主要从以下三个方面进行考虑:1. 缓存什么内容 2. 何时进行缓存 3.缓存空间不足时怎么进行缓存替换?
  • 缓存策略有以下几类
策略名称策略实现模式算法
基于访问模式按各缓存项的被访问时间来组织缓存队列,决定替换对象LRU
基于访问频率按照缓存项被访问的频率来组织缓存LFU、LRU-2、2Q、LIRS
访问时间和频率兼顾通过访问时间和频率兼顾,使得数据在二者之间进行变化时,也能有很好的缓存性能FBR、LRFU、ALRFU
  • 正如世界上没有绝对的事情,我们使用缓存策略也需要随着应用情况、数据规模、数据应用场景等等各类情况的不同而进行抉择。

缓存系统设计

  • 缓存系统主要由缓存池和磁盘缓存组成。缓存池则对应内存缓存、磁盘对应磁盘缓存。缓存池中存储小且频繁使用的数据,其中目的是为了提高用户体验、减少流量浪费、减小服务器压力。而磁盘缓存则缓存较大文件供用户可以在离线或无网络的时候也能进行访问。
  • 缓存首先是要获取数据,当在初始化状态的时候,应用、缓存池、磁盘都没有数据,此时可以对应用产生的需要保存的数据如:聊天记录、临时图片、属性文件等进行缓存,抛入到缓存池,而需要请求的数据则由缓存池->磁盘->服务器的过程进行获取。当获取到数据之后,根据数据类型判断是否需要存入缓存池或磁盘。

                                         缓存保存过程

    缓存保存过程

                                         缓存获取过程

    缓存获取过程

  • 针对缓存池,需要考虑缓存算法,缓存数据保存,线程安全、缓存容量、缓存数据一致性。具体结构如图所示:(其中箭头代表控制结构,被指向的物体代表被控制)
    缓存控制过程
  • 系统内部对外不可见,但是可以开发接口供程序员自定义如:缓存算法,更新策略。同时进入缓存池的操作应该是无感知的。即调用一个函数,就能将对象和数据放入缓存池,至于是进入内存还是磁盘则由缓存池进行控制。
Tags: iOS开发
Archives QR Code
QR Code for this page
Tipping QR Code
Leave a Comment

已有 5 条评论
  1. 吴先生 吴先生

    mark

  2. 好文,拜读,以后常看学习。

  3. Hello@(小乖)

  4. Doctors have many challenges to face as they are perennially surrounded by patients, diseases, hospital duties and over-extended or odd shift timings. Universally, doctor is considered to be a noble profession and respectable one at that, but a doctor also has to work under immense pressures, emotional strains and other physical challenges.

    A regular physician like most of us at some point face will have to deal with personal situations such as important family affairs, family holidays, sickness or pregnancy that may force them to abandon medical duties. At the same time, a hospital or a healthcare facility is also constantly faced with emergency situations that demand all hands on deck round-the-clock. Therefore, every hospital, clinic or nursing home is compelled to hire locum tenens or substitute doctor in order to keep the staffing under control at all times.

    In fact, locum doctors are the most valuable asset for the medical community because they provide quality medical care and act as a helping-hand in emergency situations when the medical facilities need them the most.

    Unlike regular or permanent doctors, locum doctor jobs are also ideal career options for medical interns and graduates because they offer a wide array of medical exposure in varied medical specialties, work cultures and healthcare systems. Locum jobs are challenging and flexible, thus an increasing number of medical professionals have benefitted from these jobs, so whether one is looking for a family physicians position or in a hospital or in a clinic, locum jobs for doctors are available at all levels and in different healthcare systems.

    In addition, being a locum doctor gives a medical professional the control over their working hours, location of work and choice of area of specialisation. Technically, locum positions are not restricted to general physicians but they are also extended to other fields of medical specialisations such as cardiology, neurology and many more.

    Travelling can be an integral part of locum jobs, and these distinctive features are a boon for many dedicated medical professionals who are eager to expand their medical careers with loads of multi-cultural medical experiences. The fact that locum agencies in the UK recruit tens of thousands of locums from across the globe in various NHS hospitals, private clinics, nursing homes and other public hospitals speaks volume of the popularity of locum jobs.

    Locating or getting a locum tenens job is a simple task as long as you are registered with one of the many reputable locum agencies. These agencies act as the middle man between locum tenens and medical facilities, and they also look after all the details pertaining to travel for locum tenens, accommodation and the nature of locum work.

    Thus, maintaining a healthy locum doctor-agency relationship benefits both the parties, and it also increases the probability of getting recommendable employment opportunities and businesses or vice-versa.

  5. ome people, especially those running on busy daily schedules tend to use the pills to help maintain weight since they can not afford to follow all the diet programs. This is not advised. It is recommended that one seek advice from a professional in this field before using the pills. This can save one from many dangers associated with the misuse.

    The diet pills should always be taken whole. Some people tend to divide the pills to serve a longer period of time. This is not advised and can lead to ineffectiveness. If it is required that one takes a complete tablet, it means that a certain amount of the ingredients are required to achieve the desired goal. It is also recommended that one does not crush the pill and dissolve it in beverages. Chemicals found in beverages have the potential of neutralizing the desired nutrients in the pill thereby leading to ineffectiveness. The best way to take the tablets is swallowing them whole with a glass of water.

    https://www.cialissansordonnancefr24.com/achat-de-cialis/