iOS开发 数据存储之YYCache
来源:互联网 发布:淘宝奇葩商品 二向箔 编辑:程序博客网 时间:2024/05/21 07:57
一.关于YYCache
YYCache由YYCache、YYDiskCache、YYMemoryCache和YYKVStorage组成,其中YYKVStorage可直接对sqlite和文件系统进行读写(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 in the 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 Initializers 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 in the 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 Initializers 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)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 queue when 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 queue when 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 queue when 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 queue when 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 queue when 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))progress endBlock:(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
- iOS开发之数据存储
- IOS缓存管理之YYCache使用
- IOS缓存管理之YYCache使用
- iOS开发之数据存储方案
- iOS 开发数据存储之magic record
- 【iOS-Android开发对比】之 数据存储
- 13、iOS开发之数据存储
- iOS软件开发 数据存储之SQLite3
- iOS开发之数据存储(一)
- iOS开发之SQLite3数据存储
- iOS开发之Core Data数据存储
- IOS开发学习笔记之数据存储
- iOS开发 数据存储之NSUserDefaults
- iOS开发 数据存储之NSKeyedArchiver
- iOS开发 数据存储之write
- iOS开发 数据存储之FMDB
- iOS开发 数据存储之SQLite3
- c#第三天 三大循环语句和一维数组
- hdu1492(丑数因子数)
- OKHTTP使用,包含有每次的特殊标识,个人使用超赞
- 栈和栈帧
- MySQL修改用户权限
- iOS开发 数据存储之YYCache
- 语义分析的一些方法(一)
- php BC高精确度函数库
- hdu 6047 Maximum Sequence【思维】
- Qt creator添加套件
- jquery实现贪吃蛇功能
- JVM中jmap无法连接java进程的问题
- http与https连接获取数据
- 计算机视觉----图像底层特征提取之边缘提取(一)