/MKCacheStorage

Storage for saving objects persistent on disk for speed up loading processes and offline usage

Primary LanguageSwiftApache License 2.0Apache-2.0

MKCacheStorage

Build Status Platform

A framework for saving objects persistent on disk. Extended by a dictionary that saves seen objects for faster delivery on duplicate requests.

Roadmap

  • Saving & retrieving objects with NSCoding protocol on disk
  • Cache used objects in dictionary for usage on runtime
  • Include GCD for async requests and saving to don't block the main thread
  • Use singleton instance for usage outside the framework
  • Secondary indices for categorization of objects
  • Own protocol for object serialization Using new Codable for object serialization
  • Own index class
  • Reduce used disk space amount
  • Caching algorithm for most used object and for reducing memory usage
  • Auto save secondary indices on disk
  • Tests for every important action

Performance

This table shows the peformance of the framework, including the number of shown objects and several ways of receiving the entire amount back:

function / # of objects 100 1.000 10.000 50.000
read direct from disk (test case) ~0 0,343 3,3 s -
read from memory (test case) ~0 0,00268 s 0,0303 s -
read mixed async 0,028 0,272 s 2,673 s -
read labeld objects (n/2) 0,001 s 0,001 s 0,008 s -
write objects without labels 0,088 s 0,874 s 8,764 s -
write objects with labels 0,089 s 0,910 s 11,901 s -
Test with plain objects with two basic attributes
used disk space ~50MB 3,9 MB 39 MB
used memory space 400 KB ~120 MB ~700 MB

Usage

Init MKCacheStorage

You can initiate an MKCacheStorage instance via the singleton pattern, with the shared variable shown below:

let mkcstorage: MKCacheStorage = MKCacheStorage.shared

Save objects

The save method stores objects in the framework; if desired, you can also give an array with labels for the object:

  • save(object: NSObject, under identifier: String, result:@escaping (Bool) -> ())
  • save(object:NSObject, under identifier: String, with labels: [String], result:@escaping (Bool) -> ())
//saving objects
let id = "User1"
let object = MyObject()

mkcstorage.save(object: object, under: id, result: { success in
    //do stuff
})

//saving objects with given labels
let labels = ["friends, "contacts"]

mkcstorage.save(object: object, under: id, with: [labels], result: { success in
    //do stuff
})

Retrieve objects

There are two methods for retrieving objects:

  • get(identifier: String, result:@escaping (NSObject?) -> ())
  • get(label: String, result:@escaping ([NSObject]) -> ())

Code example:

//get object by id
let id = "User1"

mkcstorage.get(identifier: id, result: { (object: MyObject?) in
    if let retrievedObj = object as? MyObject {
        //do stuff with object
    }
})

//get objects by label
let label = "contacts"

mkcstorage.get(label: label) { (objects: [MyObject]) in
    //do stuff with objects
}

Object preparation

To store an object in MKCacheStorage, you'll need to (auto)implement the Codable protocol:

class MyObject: Codable {
    var name: String
    var age: Int

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