{"id":13490018,"url":"https://github.com/nuudles/CacheIsKing","last_synced_at":"2025-03-28T05:31:44.384Z","repository":{"id":56905568,"uuid":"50454108","full_name":"nuudles/CacheIsKing","owner":"nuudles","description":"A simple cache that can hold anything, including Swift items","archived":false,"fork":false,"pushed_at":"2017-01-31T18:24:46.000Z","size":24,"stargazers_count":13,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-20T15:11:09.790Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/nuudles.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-01-26T19:40:34.000Z","updated_at":"2018-01-22T08:14:39.000Z","dependencies_parsed_at":"2022-08-21T02:50:35.981Z","dependency_job_id":null,"html_url":"https://github.com/nuudles/CacheIsKing","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nuudles%2FCacheIsKing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nuudles%2FCacheIsKing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nuudles%2FCacheIsKing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nuudles%2FCacheIsKing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nuudles","download_url":"https://codeload.github.com/nuudles/CacheIsKing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222351162,"owners_count":16970632,"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":[],"created_at":"2024-07-31T19:00:39.406Z","updated_at":"2024-10-31T03:31:05.416Z","avatar_url":"https://github.com/nuudles.png","language":"Swift","funding_links":[],"categories":["Libs"],"sub_categories":["Utility"],"readme":"# CacheIsKing\n\n\u003ca href=\"https://github.com/Carthage/Carthage/issues/179\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat\"\u003e\n\u003c/a\u003e\n\n`CacheIsKing` is a simple cache that allows you to store any item, including objects, pure Swift structs, enums (with associated values), etc. Simply put, it's designed to act like an `NSCache` for everything, including Swift variables.\n\n## Features\n\n- Simply set, get, and remove items based on any key that is `Hashable`\n- Remove items for any key that matches a filter\n- The cache is cleared when the app receives a memory warning\n- Similar to `NSCache`, the cache is cleared when the app enters the background\n- Subscripts are supported for `String`, `Int`, and `Float` keys\n- `item(for:)` uses generics so you don't have to cast the return value when the type is inferred correctly\n- Similar to `NSCache`, the cache can have a `countLimit` set to ensure that the cache doesn't get too large\n\n## Requirements\n\n- iOS 8.0+\n- tvOS 9.0+\n- Xcode 7+\n\n## Installation using CocoaPods\n\n[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects.\n\nBecause `CacheIsKing ` is written in Swift, you must use frameworks.\n\nTo integrate `CacheIsKing ` into your Xcode project using CocoaPods, specify it in your `Podfile`:\n\n```ruby\nsource 'https://github.com/CocoaPods/Specs.git'\nplatform :ios, '8.0'\nuse_frameworks!\n\npod 'CacheIsKing'\n```\n\nThen, run the following command:\n\n```bash\n$ pod install\n```\n\n## Installation using Carthage\n\nAdd this to your `Cartfile`:\n\n```\ngithub \"nuudles/CacheIsKing\"\n```\n\n## Usage\n\nSimply use the `KingCache` class similar to how you'd use a `NSCache`. Using the `setItem` and `itemForKey` methods allow you to use type inference to get the values you want.\n\n```swift\nlet cache = KingCache()\ncache.set(item: 123, for: \"123\")\n\nif let item: Int = cache.item(for: 456) {\n\tdoSomethingWithItem(item)\n}\n```\n\nYou can also use subscripts to set/get items from the cache. Unfortunately since Swift doesn't support subscript methods with generics yet, you'll have to cast your items as necessary. Also currently only `String`, `Int`, and `Float` keys are supported:\n\n```swift\nlet cache = KingCache()\ncache[\"123\"] = 123\n\nif let item = cache[456] as? Int {\n\tdoSomethingWithItem(item)\n}\n```\n\nThe `KingCache` also has a `countLimit` property, which allows you to set the maximum number of items in the cache. It currently evicts randomly until the `countLimit` is met.\n\n```swift\nlet cache = KingCache()\ncache.countLimit = 2\n\ncache[123] = 123\ncache[234] = 234\ncache[345] = 345\n\nprint(\"\\(cache.count)\") // shows a count of 2\n```\n\n## TODO\n\n- Refine eviction algorithm (currently evicts randomly)\n- Update with better subscript support once Swift supports subscripts with generics","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnuudles%2FCacheIsKing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnuudles%2FCacheIsKing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnuudles%2FCacheIsKing/lists"}