<div><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">
</span></span></span></div>
<div><span style="font-size: 24px;"><b><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">模块</span></span></b></span></div>
<div><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);"><b>YYCache</b> </span></span></span></div>
<div><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">这个类是底层YYMemoryCache和YYDiskCache的对外的统一接口。里面的方法不具体执行,只是调用了</span></span></span><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">YYMemoryCache和</span></span></span><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">YYDiskCache的方法。</span></span></span></div>
<div><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">
</span></span></span></div>
<div><b><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">YYMemoryCache</span></span></span></b></div>
<div><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">这是一个内存缓存类,里面实现双向链表和淘汰算法。为了防止多线程操作,使用了</span></span></span><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">OSSpinLock</span></span></span><span style="box-sizing: border-box; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background: transparent;">自旋锁,</span></div>
<div><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">OSSpinLock 不需要进入内核,只是简单的重复检测锁是否释放。如果此任务需要执行很久的话,效率会非常低。但是由于它节省了系统调用和上下文切换的时间,所以当任务确实很快的时候,效率很好。</span></span></span><span style="color: rgb(29, 29, 29);"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="box-sizing: border-box; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background: transparent;">对于内存缓存的存取来说,它非常合适。</span></span></span></div>
<div><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">
</span></span></span></div>
<div><b><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">YYDiskCache</span></span></span></b></div>
<div><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">它是以YYKVStorageItem类的前端,它和YYCache一样,都是不执行具体代码,只是调用</span></span></span><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">YYKVStorageItem的方法来具体执行,它使用了</span></span></span></div>
<div>
<div style="box-sizing: border-box; padding: 0px 0px 0.6em; border: 0px; outline: 0px; vertical-align: baseline; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 1; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial;">
<div><span style="color: rgb(29, 29, 29);"><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="box-sizing: border-box; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background: transparent;">dispatch_semaphore 是信号量,但当信号总量设为 </span><span style="box-sizing: border-box; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background: transparent;">1</span><span style="box-sizing: border-box; padding: 0px; border: 0px; outline: 0px; font-size: 16px; vertical-align: baseline; background: transparent;"> 时也可以当作锁来。在没有等待情况出现时,它的性能比 pthread_mutex 还要高,但一旦有等待情况出现时,性能就会下降许多。相对于 OSSpinLock 来说,它的优势在于等待时不会消耗 CPU 资源。对磁盘缓存来说,它比较合适。</span></span></span></span></div>
<div><span style="color: rgb(29, 29, 29);"><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;">
</span></span></span></div>
<div><b><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">YYKVStorageItem</span></span></span></b></div>
<div><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">它是以</span></span></span><b><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">YYDiskCache</span></span></span></b><span style="font-size: 16px;"><span style="font-family: 'PingFang SC', 'Helvetica Neue', Helvetica, Arial, Verdana, 'Hiragino Sans GB', 'Noto Sans CJK SC', 'Source Han Sans CN', STHeiti, 'Microsoft YaHei', 'WenQuanYi Micro Hei', SimSun, sans-serif;"><span style="color: rgb(29, 29, 29);">类的后端,负责执行存文件或者存在数据库(小于20K的数据)里面。它也是使用了</span></span></span><span style="font-size: 16px;"><span style="font-family: Lato, 'Microsoft Jhenghei', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;"><span style="color: rgb(85, 85, 85);">OSSpinLock。</span></span></span></div>
</div>
</div>
YYCache源码阅读
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 2016年国庆假期终于把此书过完,整理笔记和体会于此。 关于书名 书名源于俄罗斯的演员斯坦尼斯拉夫斯基创作的《演员...