Latest 0.1.4
Homepage https://github.com/nayanda1/iOSNativefier
License MIT
Platforms ios 9.3
Dependencies HandyJSON
Authors

iOSNativefier

Cache library for iOS


Changelog

for changelog check here


Features

  • [x] Builder pattern
  • [x] Default Cache for Image and any HandyJSON object https://github.com/alibaba/HandyJSON
  • [x] Support synchronous or asynchronous operation
  • [x] Delegate when object is about to remove from cache
  • [x] Custom fetcher closure to get object if object is not present in the cache
  • [x] Dual cache on Disk and Memory

    Requirements

  • Swift 4.2 or higher

Installation

CocoaPods

pod ‘Nativefier’, ‘~> 0.1.3’

or just

pod ‘Nativefier’

Manually

  1. Clone this repository.
  2. Added to your project.
  3. Congratulation!

About Nativefier

The basic algorithm of nativefier is very simple

Stored object

  1. Object will be stored on memory and then disk asynchronously
  2. If memory is already full, the least and oldest accessed object will be removed to give room for the new object
  3. If Disk is already full, the least and oldest accessed object will be removed to give room for the new object

Getting the object

  1. It will be return the stored object if the object is already in memory
  2. If the object is not present in the memory, it will be search in the disk and will stored the object found in the memory as new object
  3. If the object is not found, it will return nil
  4. If you’re using the getOrFetch method, then it will fetch the object using your custom fetcher if the object is not found anywhere and stored the object from fetcher as new object to memory and disk

Usage Example

HandyJSON and Image

Build the object using NativefierBuilder

  • containerName is name of cache folder in disk
  • maxRamCount is max number of object can stored in memory
  • maxDiskCount is max number of object can stored in disk
//HandyJSON object cache
let handyJSONCache = NativefierBuilder.getForHandyJSON<MyObject>().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).build()

//Image cache
let imageCache = NativefierBuilder.getForImage().set(maxRamCount: 100).set(maxDiskCount: 200).build()

Any Object

If you prefer custom object you can create your own serializer for your cache

class MyOwnSerializer : NativefierSerializerProtocol{

    func serialize(obj : AnyObject) -> Data? {
        guard let myObject : MyObject = obj as? MyObject else {
            return nil
        }
        //ANY CODE TO CONVERT YOUR OBJECT TO DATA
        return serializedData
    }

    func deserialize(data : Data) -> AnyObject? {
        //ANY CODE TO CONVERT DATA TO YOUR OBJECT
        return deserializedObject
    }

}

And then create your cache

let myObjectCache = NativefierBuilder.getForAnyObject<MyObject>().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).set(serializer: MyOwnSerializer()).build()

Create Fetcher

Fetcher is closure that will be executed if the object you want to get is not present in memory or disk. The object returned from the fetcher will be stored in cache

let myObjectCache = NativefierBuilder.getForAnyObject<MyObject>().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).set(serializer: MyOwnSerializer())
    .set(fetcher: { key in
        //ANY CODE TO FETCH THE OBJECT USING THE GIVEN KEY
        return fetchedObject
    }).build()

Fetch will be considered failed if it’s return nil, you can make nativefier automatically retry if fetch is failed on the builder or the object itself

let myObjectCache = NativefierBuilder.getForAnyObject<MyObject>().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).set(serializer: MyOwnSerializer())
    .set(fetcher: { key in
        //ANY CODE TO FETCH THE OBJECT USING THE GIVEN KEY
        return fetchedObject
    })
    .set(maxRetryCount: 3) //RETRY 3 TIMES
    .build()

myObjectCache.maxRetryCount = 5 //RETRY 5 TIMES

Using The Nativefier

Using the nativefier is very easy. just use it like you use Dictionary object.
But remember, if you want to using fetcher, its better to do it asynchronously so it wouldn’t block your execution if fetch take to long

let object = myCache["myKey"]
myCache["newKey"] = myNewObject

//Using fetcher if its not found anywhere
let fetchedObject = myCache.getOrFetch(forKey : "myKey")

//Using async, it will automatically using fetcher if the object is not found anywhere and you have fetcher.
myCache.asyncGet(forKey: "myKey", onComplete: { object in
    guad let object : MyObject = object as? MyObject else {
        return
    }
    //DO SOMETHING WITH YOUR OBJECT
})

Using Delegate

If you need to use delegate, you need to implement the delegate and then put it in your cache is it will executed by the cache.
The delegate method you can use is :

  • nativefier(_ nativefier : Any , onFailedFecthFor key: String) -> Any?

will be executed if fetcher failed to get the object, you can return any default object and it will not stored in the cache

  • nativefier(_ nativefier : Any, memoryWillRemove singleObject: Any)

will be executed if nativefier is about to remove some object from memory

  • nativefierWillClearMemory(_ nativefier : Any)

will be executed if nativefier will clear the memory

  • nativefier(_ nativefier : Any, diskWillRemove singleObject: Any)

will be executed if nativefier is about to remove some object from disk

  • nativefierWillClearDisk(_ nativefier : Any)

will be executed if nativefier will clear the disk

All method are optional, use the one you need

// on build
let imageCache = NativefierBuilder.getForImage().set(maxRamCount: 100).set(maxDiskCount: 200).set(delegate : self).build()

// or directly to the Nativefier Object
imageCache.delegate = self

Contribute

We would love you for the contribution to iOSNativefier, just contact me to [email protected] or just pull request

Latest podspec

{
    "name": "Nativefier",
    "version": "0.1.4",
    "summary": "Cache library for iOS",
    "description": "Featuresn  n  - Builder patternn  - Default Cache for Image and any HandyJSON object n  - Support synchronous or asynchronous operationn  - Delegate when object is about to removed from cachen  - Custom fetcher closure to get object if object is not present in the cachen  - Dual cache on Disk and MemorynnRequirementsn  n  - Swift 4.2 or highern  nAbout Nativefiernn  The basic algorithm of nativefier is very simplen  n  Stored objectn  1. Object will be stored on memory and then disk asynchronouslyn  2. If memory is already full, the least and oldest accessed object will be removed to give room for the new objectn  3. If Disk is already full, the least and oldest accessed object will be removed to give room for the new objectn  n  Getting the objectn  1. It will be return the stored object if the object is already in memoryn  2. If the object is not present in the memory, it will be search in the disk and will stored the object found in the memory as new objectn  3. If the object is not found, it will return niln  4. If you're using the getOrFetch method, then it will fetch the object using your custom fetcher if the object is not found anywhere and stored the object from fetcher as new object to memory and diskn  nUsage Examplen  HandyJSON and Imagen  Build the object using HttpRequestBuilder and then executen  n  - containerName is name of cache folder in diskn  - maxRamCount is max number of object can stored in memoryn  - maxDiskCount is max number of object can stored in diskn  n  code :n  //HandyJSON object cachen  let handyJSONCache = NativefierBuilder.getForHandyJSON().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).build()n  n  //Image cachen  let imageCache = NativefierBuilder.getForImage().set(maxRamCount: 100).set(maxDiskCount: 200).build()n  n  n  Any Objectn  If you prefer custom object you can create your own serializer for your cachen  n  code:n  class MyOwnSerializer : NativefierSerializerProtocol{n      n      func serialize(obj : AnyObject) -> Data? {n          guard let myObject : MyObject = obj as? MyObject else {n              return niln          }n          //ANY CODE TO CONVERT YOUR OBJECT TO DATAn          return serializedDatan      }n      n      func deserialize(data : Data) -> AnyObject? {n          //ANY CODE TO CONVERT DATA TO YOUR OBJECTn          return deserializedObjectn      }n      n  }n  n  And then create your cachen  n  code:n  let handyJSONCache = NativefierBuilder.getForAnyObject().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).set(serializer: MyOwnSerializer()).build()n  n  n  Create Fetchern  Fetcher is closure that will be executed if the object you want to get is not present in memory or disk. The object returned from the fetcher will be stored in cachen  n  code:n  let handyJSONCache = NativefierBuilder.getForAnyObject().set(containerName: "myobject").set(maxRamCount: 100).set(maxDiskCount: 200).set(serializer: MyOwnSerializer())n  .set(fetcher: { key inn       //ANY CODE TO FETCH THE OBJECT USING THE GIVEN KEYn       return fetchedObjectn       }).build()n       n       n  Using The Nativefiern  Using the nativefier is very easy. just use it like you use Dictionary object.n  But remember, if you want to using fetcher, its better to do it asynchronously so it wouldn't block your execution if fetch take to longn           n  code:n  let object = myCache["myKey"]n  myCache["newKey"] = myNewObjectn           n  //Using fetcher if its not found anywheren  let fetchedObject = myCache.getOrFetch(forKey : "myKey")n           n  //Using async, it will automatically using fetcher if the object is not found anywhere and you have fetcher.n  myCache.asyncGet(forKey: "myKey", onComplete: { object inn        guad let object : MyObject = object as? MyObject else {n            returnn        }n        //DO SOMETHING WITH YOUR OBJECTn  })n  n                            n  Using Delegaten  If you need to use delegate, you need to implement the delegate and then put it in your cache is it will executed by the cache.n  The delegate method you can use is :n  - nativefier(_ nativefier : Any , onFailedFecthFor key: String) -> Any?n    will be executed if fetcher failed to get the object, you can return any default object and it will not stored in the cachen  - nativefier(_ nativefier : Any, memoryWillRemove singleObject: Any, for key: String)n    will be executed if nativefier is about to remove some object from memoryn  - nativefierWillClearMemory(_ nativefier : Any)n    will be executed if nativefier will clear the memoryn  - nativefier(_ nativefier : Any, diskWillRemove singleObject: Any, for key: String)n    will be executed if nativefier is about to remove some object from diskn  - nativefierWillClearDisk(_ nativefier : Any)n    will be executed if nativefier will clear the diskn    n  All method are optional, use the one you needn                            n  code:n  // on buildn  let imageCache = NativefierBuilder.getForImage().set(maxRamCount: 100).set(maxDiskCount: 200).set(delegate : self).build()n                            n  // or directly to the Nativefier Objectn  imageCache.delegate = self",
    "homepage": "https://github.com/nayanda1/iOSNativefier",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "nayanda1": "[email protected]"
    },
    "source": {
        "git": "https://github.com/nayanda1/iOSNativefier.git",
        "tag": "0.1.4"
    },
    "platforms": {
        "ios": "9.3"
    },
    "source_files": "Nativefier/Classes/**/*",
    "swift_version": "4.2",
    "dependencies": {
        "HandyJSON": [
            "~> 4.2.0"
        ]
    }
}

Pin It on Pinterest

Share This