Skip to content

LeanCache πŸ’ – a lean, mean, and simple caching machine

License

Notifications You must be signed in to change notification settings

asowers1/LeanCache

Repository files navigation

LeanCache πŸ’

Platforms Languages License Carthage compatible Cocoapods compatible

LeanCache allows you to cache and persist plain old objects that conform to NSCoding. To use LeanCache with value based struct models, simply create a class that implements NSCoding and wrap your model.

With LeanCache, you're in control of your own desteny! This is because LeanCache makes few assumptions about how you'd like to cache things and tasks you with creating your own object caching lifecycle.

Swift 4

use the swift-4 branch.

Getting started – example caching lifecycle

All you need to do is start with a model like the following, but it should be readily apparent that it'd be trivial to modify this flow to fit your needs.

struct ExampleObject {
    let name: String
    let age: Int
    let favoriteLanguage: String

    init(name: String, age: Int, favoriteLanguage: String) {
        self.name = name
        self.age = age
        self.favoriteLanguage = favoriteLanguage
    }
}

Next, you'd create a cache type protocol and implement a coder for your model

protocol ExampleObjectCacheType {
    func get() -> ExampleObject?
    func set(object: ExampleObject)
}
class ExampleObjectCoder: NSObject, NSCoding {
    let exampleObject: ExampleObject?

    init(exampleObject: ExampleObject) {
        self.exampleObject = exampleObject
    }

    required init?(coder: NSCoder) {
        if let name = coder.decodeObjectForKey("name") as? String,
            let age = coder.decodeObjectForKey("age") as? Int,
            let favoriteLanguage = coder.decodeObjectForKey("favorite_language") as? String {
                self.exampleObject = ExampleObject(name: name, age: age, favoriteLanguage: favoriteLanguage)
            } else {
                self.exampleObject = nil
        }
    }

    func encodeWithCoder(coder: NSCoder) {
        coder.encodeObject(self.exampleObject?.name, forKey: "name")
        coder.encodeObject(self.exampleObject?.age, forKey: "age")
        coder.encodeObject(self.exampleObject?.favoriteLanguage, forKey: "favorite_language")
    }
}

After that, create a caching wrapper for your model

class ExampleObjectCache: NSObject, ExampleObjectCacheType {
    let cache: Cache<ExampleObjectCoder>

    override init() {
        self.cache = Cache(name: "exampleObject")
    }

    func get() -> ExampleObject? {
        return self.cache.get()?.exampleObject
    }

    func set(object: ExampleObject) {
        let coder = ExampleObjectCoder(exampleObject: object)
        self.cache.set(coder)
    }
}

Finally, you can interact with your cache and persist new objects

let andrew = ExampleObject(name: "Andrew", age: 25, favoriteLanguage: "Swift")
let mariah = ExampleObject(name: "Mariah", age: 22, favoriteLanguage: "Objective-C")

let cache = ExampleObjectCache()
cache.set(andrew)
print(cache.get()?.name) // "Andrew"

//get model from persistent cache
cache.set(mariah)
print(ExampleObjectCache().get()?.name) // "Mariah"

Authored by Klaas Pieter https://github.com/klaaspieter and migrated to Swift 3 / podified by Andrew Sowers https://github.com/asowers1

About

LeanCache πŸ’ – a lean, mean, and simple caching machine

Resources

License

Stars

Watchers

Forks

Packages

No packages published