• This repository has been archived on 06/Feb/2023
  • Stars
    star
    162
  • Rank 232,284 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 11 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Composite LRU cache with fast metadata using UNIX extended file attributes

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 a NSURLCache alternative. If you use Foundation URL loading system you should use NSURLCache that supports HTTP Caching.

Features

  • 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

Requirements

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

Usage

DFCache

Store, retrieve and remove object

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];

Set and read metadata

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)

Retrieve batch of objects

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

DFFileStorage

Write and read data

DFFileStorage *storage = [[DFFileStorage alloc] initWithPath:path error:nil];
[storage setData:data forKey:@"key"];
[storage dataForKey:@"key"];

Enumerate contents

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)

Set and read extended 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];

Design

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 on iOS 7.0

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.

Installation

CocoaPods

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"

Carthage

To install DFCache add a dependency to your Cartfile:

github "kean/DFCache"

Contacts

License

DFCache is available under the MIT license. See the LICENSE file for more info.

More Repositories

1

Nuke

Image loading system
Swift
8,112
star
2

Pulse

Network logger for Apple platforms
Swift
6,307
star
3

DFImageManager

Image loading, processing, caching and preheating
Objective-C
1,180
star
4

Get

Web API client built using async/await
Swift
941
star
5

Preheat

Automates prefetching of content in UITableView and UICollectionView
Swift
629
star
6

PulsePro

A macOS app for viewing logs from Pulse
Swift
482
star
7

Align

Intuitive and powerful Auto Layout library
Swift
350
star
8

Future

Streamlined Future<Value, Error> implementation
Swift
317
star
9

FetchImage

Makes it easy to download images using Nuke and display them in SwiftUI apps
Swift
212
star
10

Regex

Open source regex engine
Swift
211
star
11

Arranged

Open source replacement of UIStackView for iOS 8 (100% layouts supported)
Swift
208
star
12

VPN

Sample custom VPN client/server in Swift
Swift
182
star
13

Formatting

Swift
179
star
14

RxNuke

RxSwift extensions for Nuke
Swift
148
star
15

CreateAPI

Delightful code generator for OpenAPI specs
Swift
142
star
16

SwiftSQL

Swift API for SQLite
Swift
131
star
17

ThreeColumnNavigation

A minimal example of three-column navigation for iPad and macOS using SwiftUI
Swift
127
star
18

Stacks

A micro UIStackView convenience API inspired by SwiftUI
Swift
73
star
19

Nuke-FLAnimatedImage-Plugin

FLAnimatedImage plugin for Nuke
Swift
53
star
20

RxUI

Auto-binding for RxSwift inspired by SwiftUI
Swift
45
star
21

Nuke-Alamofire-Plugin

Alamofire plugin for Nuke
Swift
40
star
22

NukeDemo

Nuke Demo
Swift
34
star
23

DFJPEGTurbo

Objective-C libjpeg-turbo wrapper
C
33
star
24

ImagePublisher

Combine publishers for Nuke
Swift
25
star
25

NukeUI

Lazy image loading for Apple platforms: SwiftUI, UIKit, AppKit
Swift
19
star
26

articles

Articles for kean.github.io
19
star
27

URLQueryEncoder

URL query encoder with support for all OpenAPI serialization options
Swift
17
star
28

NukeBuilder

A fun and convenient way to use Nuke
Swift
14
star
29

ScrollViewPrefetcher

Prefetching for SwiftUI
Swift
14
star
30

PulseLogHandler

SwiftLog Extension for Pulse
Swift
12
star
31

HTTPHeaders

Parsing Simple HTTP Headers
Swift
11
star
32

OctoKit

GitHub API client built with Fuse
Swift
8
star
33

PulseApps

Base Pulse macOS and iOS apps and a few demo projects
Swift
7
star
34

kean

1
star