Code Monkey home page Code Monkey logo

allcache's Introduction

AllCache

License codebeat badge

A generic cache for swift

With AllCache you can store any instance (if you can represent it as Data) in a memory and/or disk.

Generic cache

If your class already conforms to Codable you can create a cache as follows:

let cache = try! Cache(identifier: "iceCream", serializer: JSONSerializer<Icecream>())

To store or get instances from the cache:

cache.set(IceCream(id: "1", flavor: "Vanilla"), forKey: "1")
let vanilla = try cache.instance(forKey: "1")

Fetcher

You can also make an asynchronous instance request from the cache and send a Fetcher instance, if the object doesn't exist in the cache the fetcher will fetch it or create it, you can conform to Fetcher and implement the func fetch() -> Promise<FetcherResult<T>> method, from there you can create or fetch the object:

struct IcecreamCreator: Fetcher {
    func fetch() -> Promise<FetcherResult<Icecream>> {
        let icecream = Icecream(id: "1", flavor: "Vanilla")
        return Promise().fulfill(with: FetcherResult(instance: icecream))
    }
}

You can then send the fetcher to the instance request, if the instance is not cached the IcecreamCreator will create it

cache.instance(forKey: "1", fetcher: IcecreamCreator()).onSuccess { icecream in
    print(icecream.flavor)
}

Cancel requests

All asynchronous requests return a Promise<T> object that you can cancel, add success or error handlers, or simply ignore them:

import ShallowPromises

let promise = cache.instance(forKey: "1", fetcher: IcecreamCreator()) { _ in }
promise.cancel()

The Promise<T> class belongs to the ShallowPromises framework and needs to be imported separately.

Processor

If you need to further process the fetched instance you can send a Processor<T> to the cache, you need to implement the process(_ instance: T) throws -> T method in your custom Processor:

class ToppingProcessor: Processor<Icecream> {
    
    override func process(_ instance: Icecream) throws -> Icecream {
        instance.topping = self.identifier
        return instance
    }
}

and then send a processor instance when you request an instance from the cache:

let fetcher = IcecreamCreator()
let processor = ToppingProcessor(identifier: "Oreo")
let promise = cache.instance(forKey: "1", fetcher: fetcher, processor: processor)

Every Processor object has a next property so you can chain more than one processor:

let processor = ToppingProcessor(identifier: "Oreo")
processor.next = ToppingProcessor(identifier: "Chocolate syrup")

Image cache

AllCache has a set of classes and extensions to make easier to fetch and cache images, the method requestImage(with:placeholder:processor:) was added to UIImageView in an extension, internally the imageView requests an image with it's current size from a shared Cache<UIImage> instance using an URL as a key, the image returned from the cache is then set to the UIImageView'

let url = URL(string: "https://en.wikipedia.org/wiki/Ice_cream#/media/File:Ice_Cream_dessert_02.jpg")!
imageView.requestImage(with: url)

additionally, you can send a placeholder image, or a processor

If the image fetched has a different size from the size requested, the image is resized to be the exact size as the UIImageView, the resizer is just a Processor<T> subclass, if you send a processor in the parameters, it will be assigned to the next property of the resizer and it will be applied after the resize, you can chain multiple processors using the this mechanism.

UIButton also has a method to request an image, the difference is that you need to send an UIControlState in the parameters.

allcache's People

Contributors

juanjoarreola avatar

Watchers

 avatar  avatar  avatar

allcache's Issues

Problem with imageView.requestImage

from your readme:

let url = URL(string: "https://en.wikipedia.org/wiki/Ice_cream#/media/File:Ice_Cream_dessert_02.jpg")!
imageView.requestImage(with: url)

But it produces warning Result of call to 'requestImage(with:placeholder:processor:)' is unused.

You should mark it with @discardableResult. Otherwise I must always import the second module - AsyncRequest because AllCache doesn't have a definition for Request<UIImage> which seems to be incorrect.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.