DFCache
provides composite in-memory and on-disk cache with LRU cleanup. It is implemented as a set of reusable classes and protocols with concise and extensible API.
DFCache
is not intended to be used as aNSURLCache
alternative. If you useFoundation
URL loading system you should useNSURLCache
that supports HTTP Caching.
- LRU cleanup (discards least recently used items first)
- Metadata implemented on top on UNIX extended file attributes
- Builtin support for objects conforming to
<NSCoding>
protocol. Can be easily extended to support more protocols and classes - First class
UIImage
support including background image decompression - Batch methods to retrieve cached entries
- Thoroughly tested and well-documented
iOS 6.0+ / watchOS 2.0+ / OS X 10.8+ / tvOS 9.0+
DFCache *cache = [[DFCache alloc] initWithName:@"image_cache"];
NSString *key = @"https://..."; // 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];
Class | Description |
---|---|
DFCache | Asynchronous composite in-memory and on-disk cache with LRU cleanup. Uses NSCache for in-memory caching and DFDiskCache for on-disk caching. Provides API for associating metadata with cache entries. |
<DFValueTransforming> | Protocol for describing a way of encoding and decoding objects. |
<DFValueTransformerFactory> | Protocol for matching objects with value transformers. |
DFFileStorage | Key-value file storage. |
DFDiskCache | Disk cache extends file storage functionality by providing LRU (least recently used) cleanup. |
NSURL (DFExtendedFileAttributes) | Objective-c wrapper of UNIX extended file attributes. Extended attributes extend the basic attributes associated with files and directories in the file system. They are stored as name:data pairs associated with file system objects (files, directories, symlinks, etc). See setxattr(2). |
DFCache (DFCacheExtended) | Set of methods that extend DFCache functionality by allowing you to retrieve cached entries in batches. |
NSCache
auto-removal policies have change with the release of iOS 7.0. Make sure that you use reasonable total cost limit or count limit. Or else NSCache
won't be able to evict memory properly. Typically, the obvious cost is the size of the object in bytes. Keep in mind that DFCache
automatically removes all object from memory cache on memory warning for you.
To install DFCache add a dependency to your Podfile:
# source 'https://github.com/CocoaPods/Specs.git'
# use_frameworks!
# platform :ios, "6.0" / :watchos, "2.0" / :osx, "10.8" / :tvos, "9.0"
pod "DFCache"
To install DFCache add a dependency to your Cartfile:
github "kean/DFCache"
![](https://cloud.githubusercontent.com/assets/1567433/6521218/9c7e2502-c378-11e4-9431-c7255cf39577.png)
![](https://cloud.githubusercontent.com/assets/1567433/6521243/fb085da4-c378-11e4-973e-1eeeac4b5ba5.png)
![](https://cloud.githubusercontent.com/assets/1567433/6521256/20247bc2-c379-11e4-8e9e-417123debb8c.png)
DFCache is available under the MIT license. See the LICENSE file for more info.