/CacheIt

An iOS caching framework capable of caching objects in memory and to disk.

Primary LanguageSwiftOtherNOASSERTION

CacheIt makes it easy to add caching capabilities to your iOS app!

Features

  • Fast and lightweight
  • Ability to store 'Any' data type
  • Operates similar to a Dictionary using key/value pairs
  • Support for both in memory and to disk cache
  • Each piece of data cached can have its own expiration

Requirements

  • Swift 5.0
  • iOS 10.0+

Installation

Swift Package Manager

To integrate CacheIt into your Xcode project using SPM:

  1. In xCode select File → Add Package Dependency
  2. Enter the repository URL - git@github.com:zillow/CacheIt.git
  3. Under "version" select the latest version

Cocoapods

To integrate CacheIt into your Xcode project using CocoaPods, specify it in your Podfile:

platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'CacheIt'
end

Then, run the following command:

$ pod install

Using default parameters

  1. Import CacheIt Module
import CacheIt
  1. Store data to disk using the static class function referencing the data by the key "names" using the default storage parameters.
// Blocking call
PersistentCache["names"] = ["Brett" : "Hamlin"]
//
// Non blocking call
PersistentCache.setValue(value: TestData.cacheTestDataValue, forKey: TestData.cacheTestDataKey) { }
  1. Retrieve data from disk using the static class function referencing it by the key "names".
// Blocking call
let myNames: [String:String]? = PersistentCache["names"]
//
// Non blocking call
PersistentCache.value(forKey: TestData.cacheTestDataKey) { (val: String?) in }

Using custom parameters

  1. Import CacheIt Module
import CacheIt
  1. Create a custom cache type with your own parameters.
var cache = PersistentCache(expiration: 30)
  1. Store data in memory referencing the data by the key "names" using custom storage parameters.
cache["names"] = ["Brett", "Art", "Dan"]
  1. Retrieve data from memory referencing it by the key "names".
let myNames: [String]? = cache["names"]

Configure Logging

Logging utilizes Apple's native OSLog. You can enable logging by setting a logging level of .debug, .info, or .none.

CacheController.shared.loggingLevel = .debug

Objective-C Support

CacheIt provides two static class types to store and retrieve data - NSPersistentCache & NSTransientCache

  1. Import CacheIt Module
@import CacheIt;
  1. Store data to disk using the static class function referencing the data by the key "names" using the default storage parameters.
// Blocking call
[NSPersistentCache setValue:@"Brett Hamlin" forKey:@"name"];
  1. Retrieve data from disk using the static class function referencing it by the key "names".
// Blocking call
NSString *myName = [NSPersistentCache valueForKey:@"name"];

Details, details, details

Caching Types: CacheIt supports Transient (to memory) and Persistent (to disk) caching. You can reference the static class subscript methods using default parameters or create your own instance of the class to pass in your own default parameters. The two classes are: PersistentCache and TransientCache respectively.

License

CacheIt is available under the Apache License, Version 2.0.