CacheIt makes it easy to add caching capabilities to your iOS app!
- 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
- Swift 5.0
- iOS 10.0+
To integrate CacheIt into your Xcode project using SPM:
- In xCode select File → Add Package Dependency
- Enter the repository URL - git@github.com:zillow/CacheIt.git
- Under "version" select the latest version
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
- Import CacheIt Module
import CacheIt
- 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) { }
- 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 }
- Import CacheIt Module
import CacheIt
- Create a custom cache type with your own parameters.
var cache = PersistentCache(expiration: 30)
- Store data in memory referencing the data by the key "names" using custom storage parameters.
cache["names"] = ["Brett", "Art", "Dan"]
- Retrieve data from memory referencing it by the key "names".
let myNames: [String]? = cache["names"]
Logging utilizes Apple's native OSLog. You can enable logging by setting a logging level of .debug
, .info
, or .none
.
CacheController.shared.loggingLevel = .debug
CacheIt provides two static class types to store and retrieve data - NSPersistentCache & NSTransientCache
- Import CacheIt Module
@import CacheIt;
- 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"];
- Retrieve data from disk using the static class function referencing it by the key "names".
// Blocking call
NSString *myName = [NSPersistentCache valueForKey:@"name"];
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.
CacheIt is available under the Apache License, Version 2.0.