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")


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

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 }

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


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") = 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: "")!
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.


  • Swift Tools 5.2.0
View More Packages from this Author


Last updated: Wed Nov 22 2023 07:46:45 GMT-1000 (Hawaii-Aleutian Standard Time)