{"id":819,"url":"https://github.com/aschuch/AwesomeCache","last_synced_at":"2025-08-06T13:31:47.263Z","repository":{"id":47732008,"uuid":"21776379","full_name":"aschuch/AwesomeCache","owner":"aschuch","description":"Delightful on-disk cache (written in Swift)","archived":false,"fork":false,"pushed_at":"2021-08-11T10:19:24.000Z","size":153,"stargazers_count":1269,"open_issues_count":37,"forks_count":169,"subscribers_count":28,"default_branch":"master","last_synced_at":"2024-11-30T07:08:01.286Z","etag":null,"topics":["cache","disk-cache","expiry","swift"],"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/aschuch.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":"2014-07-12T20:56:08.000Z","updated_at":"2024-11-18T00:21:43.000Z","dependencies_parsed_at":"2022-07-26T16:45:10.119Z","dependency_job_id":null,"html_url":"https://github.com/aschuch/AwesomeCache","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschuch%2FAwesomeCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschuch%2FAwesomeCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschuch%2FAwesomeCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aschuch%2FAwesomeCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aschuch","download_url":"https://codeload.github.com/aschuch/AwesomeCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228576914,"owners_count":17939645,"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":["cache","disk-cache","expiry","swift"],"created_at":"2024-01-05T20:15:32.099Z","updated_at":"2024-12-09T14:30:37.930Z","avatar_url":"https://github.com/aschuch.png","language":"Swift","funding_links":[],"categories":["Cache","Libs","HarmonyOS","Swift","Data and Storage","Cache [🔝](#readme)"],"sub_categories":["Cache","Other free courses","Windows Manager","Getting Started","Utility"],"readme":"# Awesome Cache\n\n[![Build Status](https://travis-ci.org/aschuch/AwesomeCache.svg)](https://travis-ci.org/aschuch/AwesomeCache)\n[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/AwesomeCache.svg)](https://img.shields.io/cocoapods/v/AwesomeCache.svg)\n![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)\n![Swift 3.0](https://img.shields.io/badge/Swift-3.0-orange.svg)\n![Platform](https://img.shields.io/badge/platform-iOS%20%7C%20watchOS%20%7C%20tvOS-lightgrey.svg)\n\nDelightful on-disk cache (written in Swift).\nBacked by NSCache for maximum performance and support for expiry of single objects.\n\n\n## Usage\n\n```swift\ndo {\n    let cache = try Cache\u003cNSString\u003e(name: \"awesomeCache\")\n\n    cache[\"name\"] = \"Alex\"\n    let name = cache[\"name\"]\n    cache[\"name\"] = nil\n} catch _ {\n    print(\"Something went wrong :(\")\n}\n```\n\n### Sync by design\n\nAwesomeCache \u003e= 3.0 is designed to have a sync API, making it easy to reason about the actual contents of the cache. This decision has been made based on [feedback from the community](issues/33), to keep the API of AwesomeCache small and easy to use. \n\nThe internals of the cache use a concurrent dispatch queue, that syncs reads and writes for thread safety. In case a particular caching operation blocks your main thread for too long, consider offloading the read and write operations to a different thread.\n\n```swift\ndispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {\n\tcache[\"name\"] = \"Alex\"\n}\n```\n\n### Cache expiry\n\nObjects can also be cached for a certain period of time.\n\n```swift\ncache.setObject(\"Alex\", forKey: \"name\", expires: .never) // same as cache[\"name\"] = \"Alex\"\ncache.setObject(\"Alex\", forKey: \"name\", expires: .seconds(2)) // name expires in 2 seconds\ncache.setObject(\"Alex\", forKey: \"name\", expires: .date(Date(timeIntervalSince1970: 1428364800))) // name expires on 4th of July 2015\n```\n\nIf an object is accessed after its expiry date, it is automatically removed from the cache and deleted from disk.\nHowever, you are responsible to delete expired objects regularly by calling `removeExpiredObjects` (e.g. on app launch).\n\n### Awesome API Caching\n\nAPI responses are usually cached for a specific period of time. AwesomeCache provides an easy method to cache a block of asynchronous tasks.\n\n```swift\ncache.setObject(forKey: \"name\", cacheBlock: { success, failure in\n  // Perform tasks, e.g. call an API\n  let response = ...\n\n  success(response, .seconds(300)) // Cache response for 5 minutes\n  // ... or failure(error)\n}, completion: { object, isLoadedFromCache, error in\n\tif object {\n\t \t// object is now cached\n\t}\n})\n```\n\nIf the cache already contains an object, the `completion` block is called with the cached object immediately.\n\nIf no object is found or the cached object is already expired, the `cacheBlock` is called.\nYou may perform any tasks (e.g. network calls) within this block. Upon completion of these tasks, make sure to call the `success` or `failure` block that is passed to the `cacheBlock`. The cacheBlock will not be re-evaluated until the object is expired or manually deleted.\n\nThe completion block is invoked as soon as the cacheBlock is finished and the object is cached.\n\n## Version Compatibility\n\nCurrent Swift compatibility breakdown:\n\n| Swift Version | Framework Version |\n| ------------- | ----------------- |\n| 3.0           | 5.x               |\n| 2.3           | 4.x               |\n| 2.2           | 3.x               |\n\n[all releases]: https://github.com/aschuch/AwesomeCache/releases\n\n## Installation\n\n#### Carthage\n\nAdd the following line to your [Cartfile](https://github.com/Carthage/Carthage/blob/master/Documentation/Artifacts.md#cartfile).\n\n```\ngithub \"aschuch/AwesomeCache\"\n```\n\nThen run `carthage update`.\n\n#### CocoaPods\n\nAdd the following line to your Podfile.\n\n```\npod \"AwesomeCache\", \"~\u003e 3.0\"\n```\n\nThen run `pod install` with CocoaPods 0.36 or newer.\n\n#### Manually\n\nJust drag and drop the two `.swift` files as well as the `NSKeyedUnarchiverWrapper.h/.m` files in the `AwesomeCache` folder into your project.\nIf you are adding AwesomeCache to a Swift project, you also need to add an import for `NSKeyedUnarchiverWrapper.h` to your bridging header.\n\n## Tests\n\nOpen the Xcode project and press `⌘-U` to run the tests.\n\nAlternatively, all tests can be run in the terminal using [xctool](https://github.com/facebook/xctool).\n\n```bash\nxctool -scheme AwesomeCacheTests -sdk iphonesimulator test\n```\n\n## Contributing\n\n* Create something awesome, make the code better, add some functionality,\n  whatever (this is the hardest part).\n* [Fork it](http://help.github.com/forking/)\n* Create new branch to make your changes\n* Commit all your changes to your branch\n* Submit a [pull request](http://help.github.com/pull-requests/)\n\n\n## Contact\n\nFeel free to get in touch.\n\n* Website: \u003chttp://schuch.me\u003e\n* Twitter: [@schuchalexander](http://twitter.com/schuchalexander)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faschuch%2FAwesomeCache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faschuch%2FAwesomeCache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faschuch%2FAwesomeCache/lists"}