一.关于YYCache

YYCache由YYCache、YYDiskCache、YYMemoryCache和YYKVStorage组成,其中YYKVStorage可直接对sqlite和文件系统进行读写(YYDiskCache的底层实现)

存储方式 存取速度(方式) 缓存时间
YYCache 如果不指定存储方式,默认存入内存的时候同时写入磁盘 1.从内存中读,有就直接拿来用;2.没有再从磁盘读,有就拿来用,并写入内存中;3.仍然没有就返回空 和指定存储方式一样
YYMemoryCache 将对象存储在内存中 存取速度较快 短(下次重新启动运用的时候缓存会被清空)
YYDiskCache 将对象存储在磁盘中 存取速度较慢 长(下次重新启动运用的时候缓存仍然存在)

1. 内存缓存(YYMemoryCache)

存储的单元是_YYLinkedMapNode,除了key和value外,还存储了它的前后Node的地址_prev,_next.整个实现基于_YYLinkedMap,它是一个双向链表,除了存储了字典_dic外,还存储了头结点和尾节点.它实现的功能很简单,就是:有新数据了插入链表头部,访问过的数据结点移到头部,内存紧张时把尾部的结点移除.就这样实现了淘汰算法.因为内存访问速度很快,锁占用的时间少,所以用的速度最快的OSSpinLockLock

2. 硬盘缓存(YYDiskCache)

采用的是文件和数据库相互配合的方式.有一个参数inlineThreshold,默认20KB,小于它存数据库,大于它存文件.能获得效率的提高.key:path,value:cache存储在NSMapTable里.根据path获得cache,进行一系列的set,get,remove操作更底层的是YYKVStorage,它能直接对sqlite和文件系统进行读写.每次内存超过限制时,select key, filename, size from manifest order by last_access_time desc limit ?1会根据时间排序来删除最近不常用的数据.硬盘访问的时间比较长,如果用OSSpinLockLock锁会造成CPU消耗过大,所以用的dispatch_semaphore_wait来做.

二.YYCache API解读

/** The name of the cache, readonly. */
@property (copy, readonly) NSString *name;/** The underlying memory cache. see `YYMemoryCache` for more information.*/
@property (strong, readonly) YYMemoryCache *memoryCache;/** The underlying disk cache. see `YYDiskCache` for more information.*/
@property (strong, readonly) YYDiskCache *diskCache;/**Create a new instance with the specified name.Multiple instances with the same name will make the cache unstable.@param name  The name of the cache. It will create a dictionary with the name inthe app's caches dictionary for disk cache. Once initialized you should not read and write to this directory.@result A new cache object, or nil if an error occurs.*/
- (nullable instancetype)initWithName:(NSString *)name;/**Create a new instance with the specified path.Multiple instances with the same name will make the cache unstable.@param path  Full path of a directory in which the cache will write data.Once initialized you should not read and write to this directory.@result A new cache object, or nil if an error occurs.*/
- (nullable instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;/**Convenience InitializersCreate a new instance with the specified name.Multiple instances with the same name will make the cache unstable.@param name  The name of the cache. It will create a dictionary with the name inthe app's caches dictionary for disk cache. Once initialized you should not read and write to this directory.@result A new cache object, or nil if an error occurs.*/
+ (nullable instancetype)cacheWithName:(NSString *)name;/**Convenience InitializersCreate a new instance with the specified path.Multiple instances with the same name will make the cache unstable.@param path  Full path of a directory in which the cache will write data.Once initialized you should not read and write to this directory.@result A new cache object, or nil if an error occurs.*/
+ (nullable instancetype)cacheWithPath:(NSString *)path;- (instancetype)init UNAVAILABLE_ATTRIBUTE;
+ (instancetype)new UNAVAILABLE_ATTRIBUTE;#pragma mark - Access Methods
///=============================================================================
/// @name Access Methods
///=============================================================================/**Returns a boolean value that indicates whether a given key is in cache.This method may blocks the calling thread until file read finished.@param key A string identifying the value. If nil, just return NO.@return Whether the key is in cache.*/
- (BOOL)containsObjectForKey:(NSString *)key;/**Returns a boolean value with the block that indicates whether a given key is in cache.This method returns immediately and invoke the passed block in background queuewhen the operation finished.@param key   A string identifying the value. If nil, just return NO.@param block A block which will be invoked in background queue when finished.*/
- (void)containsObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, BOOL contains))block;/**Returns the value associated with a given key.This method may blocks the calling thread until file read finished.@param key A string identifying the value. If nil, just return nil.@return The value associated with key, or nil if no value is associated with key.*/
- (nullable id<NSCoding>)objectForKey:(NSString *)key;/**Returns the value associated with a given key.This method returns immediately and invoke the passed block in background queuewhen the operation finished.@param key A string identifying the value. If nil, just return nil.@param block A block which will be invoked in background queue when finished.*/
- (void)objectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, id<NSCoding> object))block;/**Sets the value of the specified key in the cache.This method may blocks the calling thread until file write finished.@param object The object to be stored in the cache. If nil, it calls `removeObjectForKey:`.@param key    The key with which to associate the value. If nil, this method has no effect.*/
- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key;/**Sets the value of the specified key in the cache.This method returns immediately and invoke the passed block in background queuewhen the operation finished.@param object The object to be stored in the cache. If nil, it calls `removeObjectForKey:`.@param block  A block which will be invoked in background queue when finished.*/
- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key withBlock:(nullable void(^)(void))block;/**Removes the value of the specified key in the cache.This method may blocks the calling thread until file delete finished.@param key The key identifying the value to be removed. If nil, this method has no effect.*/
- (void)removeObjectForKey:(NSString *)key;/**Removes the value of the specified key in the cache.This method returns immediately and invoke the passed block in background queuewhen the operation finished.@param key The key identifying the value to be removed. If nil, this method has no effect.@param block  A block which will be invoked in background queue when finished.*/
- (void)removeObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key))block;/**Empties the cache.This method may blocks the calling thread until file delete finished.*/
- (void)removeAllObjects;/**Empties the cache.This method returns immediately and invoke the passed block in background queuewhen the operation finished.@param block  A block which will be invoked in background queue when finished.*/
- (void)removeAllObjectsWithBlock:(void(^)(void))block;/**Empties the cache with block.This method returns immediately and executes the clear operation with block in background.@warning You should not send message to this instance in these blocks.@param progress This block will be invoked during removing, pass nil to ignore.@param end      This block will be invoked at the end, pass nil to ignore.*/
- (void)removeAllObjectsWithProgressBlock:(nullable void(^)(int removedCount, int totalCount))progressendBlock:(nullable void(^)(BOOL error))end;

三.YYCache 使用

1.数据存储

注意:保存的自定义对象要遵守NSCoding的协议

//模拟数据
NSString *value = @"cacheContent";
NSString *key = @"key";
YYCache *yyCache = [YYCache cacheWithName:@"yycache"];
//根据key写入缓存value
[yyCache setObject:value forKey:key];

2.判断缓存是否存在

// 根据key判断缓存是否存在
BOOL isContains = [yyCache containsObjectForKey:key];

3.移除缓存

//根据key移除缓存
[yyCache removeObjectForKey:key];

4.移除所有缓存

//移除所有缓存
[yyCache removeAllObjects];

iOS开发 数据存储之YYCache相关推荐

  1. iOS开发 数据存储之NSUserDefaults

    NSUserDefaults:用来保存应用程序设置和属性.用户保存的数据.用户再次打开程序或开机后这些数据仍然存在.NSUserDefaults可以存储的数据类型包括:NSData.NSString. ...

  2. iOS开发 数据存储之WCDB的介绍

    一.介绍 WCDB是一个高效.完整.易用的移动数据库框架,基于SQLCipher,支持iOS,macOS和Android 二.基本特性 易用,WCDB支持一句代码即可将数据取出并组合为object W ...

  3. iOS开发 数据存储之WCDB的使用

    一.类字段绑定 在WCDB内,ORM(Object Relational Mapping)是指 将一个ObjC的类,映射到数据库的表和索引: 将类的property,映射到数据库表的字段: 这一过程. ...

  4. ios应用数据存储方式(偏好设置)

    iOS开发UI篇-ios应用数据存储方式(偏好设置) 一.简单介绍 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能 每个应用 ...

  5. iOS本地数据存储安全

    iOS本地数据存储安全 移动APP通常会在设备本地存储一些数据,这可以为程序执行.更良好地性能或离线访问提供支持.由于移动设备使用地越来越广泛,设备失窃的风险也越来越大,因此不安全的本地数据存储已成为 ...

  6. iOS开发UI篇—ios应用数据存储方式(偏好设置)

    一.简单介绍 很多iOS应用都支持偏好设置,比如保存用户名.密码.字体大小等设置,iOS提供了一套标准的解决方案来为应用加入偏好设置功能 每个应用都有个NSUserDefaults实例,通过它来存取偏 ...

  7. ios应用数据存储方式(偏好设置)-转

    一.简单介绍  1.很多ios应用都支持偏好设置,比如保存用户名,密码,字体大小等设置,ios提供了一套标准的解决方案来为应用加入偏好设置功能.  2.每个应用都有个NSUserDefaults实例, ...

  8. iOS开发UI篇—ios应用数据存储方式(XML属性列表-plist)

    ● 沙盒根目录:NSString *home = NSHomeDirectory(); ● Documents:(2种⽅方式) ● 利用沙盒根目录拼接"Documents"字符串 ...

  9. IOS高级编程之二:IOS的数据存储与IO

    一.应用程序沙盒 IOS应用程序职能在系统为该应用所分配的文件区域下读写文件,这个文件区域就是应用程序沙盒.所有的非代码文件如:图片.声音.映象等等都存放在此. 在mac中command+shift+ ...

最新文章

  1. linux刷新指定URL脚本,【图片】linux下crontab定时执行本地脚本和定时访问指定url【不要牧师吧】_百度贴吧...
  2. java 定时器获得外部参数_JMeter定时器使用小结
  3. cocos2dx-3.9 集成admob
  4. 能赢球只拿12分也行 麦蒂明言不和姚明争老大
  5. Oracle字符集问题总结
  6. android studio引用module出的错:Unable to resolve dependency for‘:app@debug/........
  7. Codeforces Round #437 (Div. 2, based on MemSQL Start[c]UP 3.0 - Round 2)
  8. 老人机彻底不能用了?联通逐渐关闭2G、3G网络?回应:手机制式不支持
  9. P4822 [BJWC2012]冻结
  10. 资源 | 没有数学和编程基础,这几个数据科学项目了解一下
  11. php laravel 面试,当面试关问你Laravel Facade,说出这几个关键词就可以
  12. OptiStruct] Altair OptiStruct之前世今生
  13. Python技巧:双击python文件打开.py(博主自测完整版)
  14. 数据结构 严蔚敏 迷宫求解 代码
  15. [SSL_CHX][2021-8-20]最大质因子序列
  16. php,表单+文本域,增加表单的文本域的html
  17. 【PDF密码删除软件】Enolsoft PDF Password Remover for Mac
  18. Pascal 过程与函数
  19. python word保存图_使用python matplotlib 画图导入到word中如何保证分辨率
  20. 转:大数据面试之_01_IT 从业人员面试小技巧

热门文章

  1. 如何让2010 excel同时显示两个窗口
  2. UE4 Cinecamera焦距、视野、感应器尺寸之间的数学关系
  3. Medical Image Analyse
  4. pyqt5 +pyinstall打造属于自己的桌面版程序(学习阶段)
  5. SUSE Linux--zypper程序包管理(二)
  6. 教孩子有教养,有气质,有风度
  7. 图像分割——边缘检测——边缘连接的局部处理(Matlab)
  8. System Performance Tunning Tools
  9. C++ 解决min/max函数的报错
  10. win7安装doccano