Latest 0.1.0
Homepage https://github.com/ysawa/TwoLevelCache
License MIT
Platforms ios 8.0
Authors

CI Status
Version
License
Platform

Customizable two-level cache for iOS (Swift). Level 1 is memory powered by NSCache and level 2 is filesystem which uses NSCachesDirectory.

All cache data are managed by OS level. Then you don’t have to consider the number of objects and the usage of memory or storage.

Usage

You can make an effective PNG cache with downloader as follows:

let cache = try! TwoLevelCache<UIImage>("effective-png-cache")
cache.downloader = { (key, callback) in
    let url = URL(string: key)!
    URLSession.shared.dataTask(with: url) { data, response, error in
        callback(data)
    }.resume()
}
cache.deserializer = { (data) in
    return UIImage(data: data)
}
cache.serializer = { (object) in
    return UIImagePNGRepresentation(object)
}

cache.findObject(forKey: "https://nzigen.com/static/img/common/logo.png") { (image, status) in
    DispatchQueue.main.sync {
        imageView.image = image
    }
}

TwoLevelCacheHitStatus

You can use enum TwoLevelCacheHitStatus to know how the object has been found.

.memory  // Level 1 (NSCache)
.file  // Level 2 (NSCachesDirectory)
.downloader  // Cache miss (your device has downloaded the object)
.error  // All caches return nil and downloading data has been failed

removing caches

System will remove unused resources automatically. However, you can also remove caches as follows:

cache.removeAllObjects()
cache.removeObject(forKey: "https://nzigen.com/static/img/common/logo.png")

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

TwoLevelCache is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'TwoLevelCache'

Author

Yoshihiro Sawa (Nzigen, Inc), yoshihirosawa at gmail.com

License

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

Latest podspec

{
    "name": "TwoLevelCache",
    "version": "0.1.0",
    "summary": "Customizable two-level cache for iOS (Swift).",
    "description": "Customizable two-level cache for iOS (Swift). Level 1 is memory powered by NSCache and level 2 is filesystem which uses NSCachesDirectory.nAll cache data are managed by OS level. Then you don't have to consider the number of objects and the usage of memory or storage.",
    "homepage": "https://github.com/ysawa/TwoLevelCache",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Yoshihiro Sawa": "[email protected]"
    },
    "source": {
        "git": "https://github.com/ysawa/TwoLevelCache.git",
        "tag": "0.1.0"
    },
    "social_media_url": "https://twitter.com/yoshiiiine",
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "TwoLevelCache/Classes/**/*",
    "pushed_with_swift_version": "4.0"
}

Pin It on Pinterest

Share This