{"id":18538143,"url":"https://github.com/githawkapp/flatcache","last_synced_at":"2025-08-05T04:16:18.171Z","repository":{"id":56911370,"uuid":"116085064","full_name":"GitHawkApp/FlatCache","owner":"GitHawkApp","description":"Implementation of Soroush Khanlou's Flat Cache.","archived":false,"fork":false,"pushed_at":"2018-10-30T19:32:07.000Z","size":30,"stargazers_count":173,"open_issues_count":2,"forks_count":13,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-18T06:06:05.559Z","etag":null,"topics":["cache","githawk","swift"],"latest_commit_sha":null,"homepage":"http://khanlou.com/2017/10/the-flat-cache/","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/GitHawkApp.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":"2018-01-03T03:09:50.000Z","updated_at":"2025-03-17T11:43:51.000Z","dependencies_parsed_at":"2022-08-21T03:20:17.710Z","dependency_job_id":null,"html_url":"https://github.com/GitHawkApp/FlatCache","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/GitHawkApp/FlatCache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHawkApp%2FFlatCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHawkApp%2FFlatCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHawkApp%2FFlatCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHawkApp%2FFlatCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GitHawkApp","download_url":"https://codeload.github.com/GitHawkApp/FlatCache/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GitHawkApp%2FFlatCache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268832591,"owners_count":24314433,"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","status":"online","status_checked_at":"2025-08-05T02:00:12.334Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","githawk","swift"],"created_at":"2024-11-06T19:42:31.003Z","updated_at":"2025-08-05T04:16:18.152Z","avatar_url":"https://github.com/GitHawkApp.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FlatCache\n\nStore any object in a flat cache by `String` id and notify listeners when changes occur.\n\nThis is an implementation of [Soroush Khanlou](https://twitter.com/khanlou)'s [The Flat Cache](http://khanlou.com/2017/10/the-flat-cache/) blog post used in [GitHawk](http://githawk.com).\n\n## Installation\n\nJust add `FlatCache` to your Podfile and pod install. Done!\n\n```\npod 'FlatCache'\n```\n\n## Usage\n\nAdd the `Cachable` protocol to models that you want to store in the cache. Conform to the protocol by returning a `String` identifier used for lookup.\n\n```swift\nstruct User {\n  let name: String\n}\n\nextension User: Cachable {\n  var id: String {\n    return name\n  }\n}\n```\n\n\u003e Don't worry about `id` collisions between objects. The cache \"namespaces\" different models by type.\n\nNow just create a `FlatCache` object and start reading \u0026 writing with it.\n\n```swift\nlet cache = FlatCache()\nlet user = User(name: \"ryan\")\ncache.set(value: user)\nif let cached = cache.get(id: user.id) as User? {\n  print(cached.name) // \"ryan\"\n}\n```\n\n\u003e `FlatCache` uses the type information with the `get(id:)` function to lookup the appropriate object. You must type the result somehow.\n\n### Listeners\n\nOne of the strengths of `FlatCache` is adding \"listeners\" to the cache to be notified when an object is changed. This powerful tool lets multiple systems respond to object changes.\n\n```swift\nlet cache = FlatCache()\nlet user = User(name: \"ryan\")\nlet listener = MyUserListener()\ncache.add(listener: listener, value: user)\n```\n\nNow whenever `user` is set again in the cache, `MyUserListener` will be notified with the following function:\n\n```swift\n\nfunc flatCacheDidUpdate(cache: FlatCache, update: FlatCache.Update) {\n  switch update {\n  case .item(let item):\n    // just a single object updated\n  case .list:\n    // a list of subscribed objects updated\n  }\n}\n```\n\n\u003e `FlatCache` coalesces so only a single event is delivered when something changes, no matter if an update has just a single object or hundreds.\n\n## Acknowledgements\n\n- Code used and inspired from [Soroush Khanlou](https://twitter.com/khanlou)'s [The Flat Cache](http://khanlou.com/2017/10/the-flat-cache/)\n- Created with ❤️ by [Ryan Nystrom](https://twitter.com/_ryannystrom)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithawkapp%2Fflatcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgithawkapp%2Fflatcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgithawkapp%2Fflatcache/lists"}