DFCache 4.0.2

DFCache 4.0.2

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年12月

kean 维护。



DFCache 4.0.2

  • Alexander Grebenyuk

DFCache 提供了具有 LRU 清理的内存和磁盘缓存。它作为一组可复用的类和协议实现,具有简洁并可扩展的 API。

DFCache 不打算作为 NSURLCache 的替代品。如果您使用 Foundation URL 加载系统,应使用支持 HTTP 缓存NSURLCache

特性

  • LRU 清理(首先丢弃最近最少使用项)
  • 在 UNIX 扩展文件属性之上实现元数据
  • 内置支持符合 <NSCoding> 协议的对象。可以轻松扩展以支持更多协议和类
  • 一流的 UIImage 支持,包括背景图像解压
  • 批量检索缓存条目方法
  • 经过彻底的测试并具有完善的文档

要求

iOS 6.0+ / watchOS 2.0+ / OS X 10.8+ / tvOS 9.0+

用法

DFCache

存储、检索和删除对象

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 (DFCacheExtended)

检索对象批量

DFCache *cache = ...;
[cache batchCachedObjectsForKeys:keys completion:^(NSDictionary *batch) {
    for (NSString *key in keys) {
        id object = batch[key];
        // Do something with an object.
    }
}];

DFFileStorage

写入和读取数据

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 (DFExtendedFileAttributes)

设置和读取扩展文件属性

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 功能。

iOS 7.0 上的 NSCache

NSCache 的自动移除策略在 iOS 7.0 的发布后已经更改。请确保您使用合理的总成本限制或数量限制。否则,NSCache 无法正确地释放内存。通常,明显的成本是对象的大小(以字节为单位)。请注意,DFCache 会自动在内存警告时为您从内存缓存中删除所有对象。

安装

联系方式

许可

DFCache 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。