{"id":15293515,"url":"https://github.com/juanjoarreola/allcache","last_synced_at":"2025-10-17T14:56:52.539Z","repository":{"id":56901877,"uuid":"51183145","full_name":"JuanjoArreola/AllCache","owner":"JuanjoArreola","description":"Instance caching library ","archived":false,"fork":false,"pushed_at":"2021-12-04T14:30:25.000Z","size":191,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-01T20:23:30.989Z","etag":null,"topics":["asynchronous","cache","disk-cache","image-cache","promise","swift","thread-safe"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JuanjoArreola.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-02-06T00:42:21.000Z","updated_at":"2023-04-13T01:32:46.000Z","dependencies_parsed_at":"2022-08-20T18:50:26.525Z","dependency_job_id":null,"html_url":"https://github.com/JuanjoArreola/AllCache","commit_stats":null,"previous_names":[],"tags_count":41,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanjoArreola%2FAllCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanjoArreola%2FAllCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanjoArreola%2FAllCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JuanjoArreola%2FAllCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JuanjoArreola","download_url":"https://codeload.github.com/JuanjoArreola/AllCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245294769,"owners_count":20591909,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asynchronous","cache","disk-cache","image-cache","promise","swift","thread-safe"],"created_at":"2024-09-30T16:49:49.921Z","updated_at":"2025-10-17T14:56:47.507Z","avatar_url":"https://github.com/JuanjoArreola.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AllCache\n\n![License](https://img.shields.io/github/license/JuanjoArreola/AllCache)\n[![codebeat badge](https://codebeat.co/badges/edafed3a-62b7-4617-b9fb-556f46efeeef)](https://codebeat.co/projects/github-com-juanjoarreola-allcache-master)\n\n### A generic cache for swift\n\nWith AllCache you can store any instance (if you can represent it as `Data`) in a memory and/or disk.\n\n#### Generic cache\nIf your class already conforms to `Codable` you can create a cache as follows:\n\n```swift\nlet cache = try! Cache(identifier: \"iceCream\", serializer: JSONSerializer\u003cIcecream\u003e())\n```\n\nTo store or get instances from the cache:\n\n```swift\ncache.set(IceCream(id: \"1\", flavor: \"Vanilla\"), forKey: \"1\")\nlet vanilla = try cache.instance(forKey: \"1\")\n```\n\n#### Fetcher\n\nYou 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() -\u003e Promise\u003cFetcherResult\u003cT\u003e\u003e` method, from there you can create or fetch the object:\n\n```swift\nstruct IcecreamCreator: Fetcher {\n    func fetch() -\u003e Promise\u003cFetcherResult\u003cIcecream\u003e\u003e {\n        let icecream = Icecream(id: \"1\", flavor: \"Vanilla\")\n        return Promise().fulfill(with: FetcherResult(instance: icecream))\n    }\n}\n```\nYou can then send the fetcher to the instance request, if the instance is not cached the `IcecreamCreator` will create it\n```swift\ncache.instance(forKey: \"1\", fetcher: IcecreamCreator()).onSuccess { icecream in\n    print(icecream.flavor)\n}\n```\n\n#### Cancel requests\nAll asynchronous requests return a `Promise\u003cT\u003e` object that you can cancel, add success or error handlers, or simply ignore them:\n\n```swift\nimport ShallowPromises\n\nlet promise = cache.instance(forKey: \"1\", fetcher: IcecreamCreator()) { _ in }\npromise.cancel()\n\n```\nThe `Promise\u003cT\u003e` class belongs to the [ShallowPromises](https://github.com/JuanjoArreola/ShallowPromises) framework and needs to be imported separately.\n\n#### Processor\nIf you need to further process the fetched instance you can send a `Processor\u003cT\u003e` to the cache, you need to implement the `process(_ instance: T) throws -\u003e T` method in your custom `Processor`:\n\n```swift\nclass ToppingProcessor: Processor\u003cIcecream\u003e {\n    \n    override func process(_ instance: Icecream) throws -\u003e Icecream {\n        instance.topping = self.identifier\n        return instance\n    }\n}\n```\nand then send a processor instance when you request an instance from the cache:\n```swift\nlet fetcher = IcecreamCreator()\nlet processor = ToppingProcessor(identifier: \"Oreo\")\nlet promise = cache.instance(forKey: \"1\", fetcher: fetcher, processor: processor)\n```\n\nEvery `Processor` object has a `next` property so you can chain more than one processor:\n\n```swift\nlet processor = ToppingProcessor(identifier: \"Oreo\")\nprocessor.next = ToppingProcessor(identifier: \"Chocolate syrup\")\n```\n\n#### Image cache\n\nAllCache 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\u003cUIImage\u003e` instance using an `URL` as a key, the image returned from the cache is then set to the `UIImageView`'\n\n```swift\nlet url = URL(string: \"https://en.wikipedia.org/wiki/Ice_cream#/media/File:Ice_Cream_dessert_02.jpg\")!\nimageView.requestImage(with: url)\n```\nadditionally, you can send a placeholder image, or a processor\n\nIf 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\u003cT\u003e` 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.\n\n`UIButton` also has a method to request an image, the difference is that you need to send an `UIControlState` in the parameters.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanjoarreola%2Fallcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuanjoarreola%2Fallcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanjoarreola%2Fallcache/lists"}