DFCache
提供了具有 LRU 清理的内存和磁盘缓存。它作为一组可复用的类和协议实现,具有简洁并可扩展的 API。
DFCache
不打算作为NSURLCache
的替代品。如果您使用Foundation
URL 加载系统,应使用支持 HTTP 缓存 的NSURLCache
。
<NSCoding>
协议的对象。可以轻松扩展以支持更多协议和类UIImage
支持,包括背景图像解压iOS 6.0+ / watchOS 2.0+ / OS X 10.8+ / tvOS 9.0+
DFCache *cache = [[DFCache alloc] initWithName:@"image_cache"];
NSString *key = @"http://..."; // Key can by any arbitrary string.
UIImage *image = ...; // Instead of UIImage you can use the same API for objects conforming to NSCoding protocol
// Store image
[cache storeObject:image forKey:key];
// [cache storeObject:image forKey:key data:data]; - you can store original image data
// Retrieve decompressed image
[cache cachedObjectForKey:key completion:^(id object) {
// All disk IO operations are run on serial dispatch queue
// which guarantees that the object is retrieved successfully.
NSLog(@"Did retrieve cached object %@", object);
}];
[cache removeObjectForKey:key];
DFCache *cache = [[DFCache alloc] initWithName:@"sample_cache"];
NSDictionary *object = @{ @"key" : @"value" };
[cache storeObject:object forKey:@"key"];
[cache setMetadata:@{ @"revalidation_date" : [NSDate date] } forKey:@"key"];
NSDictionary *metadata = [cache metadataForKey:@"key"];
DFCache *cache = ...;
[cache batchCachedObjectsForKeys:keys completion:^(NSDictionary *batch) {
for (NSString *key in keys) {
id object = batch[key];
// Do something with an object.
}
}];
DFFileStorage *storage = [[DFFileStorage alloc] initWithPath:path error:nil];
[storage setData:data forKey:@"key"];
[storage dataForKey:@"key"];
DFFileStorage *storage = [[DFFileStorage alloc] initWithPath:path error:nil];
NSArray *resourceKeys = @[ NSURLContentModificationDateKey, NSURLFileAllocatedSizeKey ];
NSArray *contents = [storage contentsWithResourceKeys:resourceKeys];
for (NSURL *fileURL in contents) {
// Use file URL and pre-fetched file attributes.
}
NSURL *fileURL = [NSURL fileURLWithPath:path];
[fileURL df_setExtendedAttributeValue:@"value" forKey:@"attr_key"];
NSString *value = [fileURL df_extendedAttributeValueForKey:@"attr_key" error:NULL];
[fileURL df_removeExtendedAttributeForKey];
类 | 描述 |
---|---|
DFCache | 异步的内存和磁盘缓存,具有 LRU 清理。使用 NSCache 进行内存缓存,使用 DFDiskCache 进行磁盘缓存。提供将元数据与缓存条目关联的 API。 |
<DFValueTransforming> | 用于描述编码和解码对象的方法的协议。 |
<DFValueTransformerFactory> | 用于匹配对象与值转换器的协议。 |
DFFileStorage | 键值文件存储。 |
DFDiskCache | DFDiskCache 通过提供 LRU(最近最少使用)清理扩展文件存储功能。 |
NSURL (DFExtendedFileAttributes) | Objective-c 对 UNIX 扩展文件属性的封装。扩展属性扩展了文件系统(文件、目录、符号链接等)中与文件和目录关联的基本属性。它们作为名称:数据对存储在文件系统对象中。请参阅 setxattr(2)。 |
DFCache (DFCacheExtended) | 一组方法,通过允许您批量检索缓存的条目来扩展 DFCache 功能。 |
NSCache
的自动移除策略在 iOS 7.0 的发布后已经更改。请确保您使用合理的总成本限制或数量限制。否则,NSCache
无法正确地释放内存。通常,明显的成本是对象的大小(以字节为单位)。请注意,DFCache
会自动在内存警告时为您从内存缓存中删除所有对象。
DFCache 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。