{"id":17234404,"url":"https://github.com/yysskk/memorycache","last_synced_at":"2025-04-14T02:31:37.185Z","repository":{"id":52416396,"uuid":"167139025","full_name":"yysskk/MemoryCache","owner":"yysskk","description":"LRU, type-safe, thread-safe memory cache class in Swift","archived":true,"fork":false,"pushed_at":"2021-04-29T20:25:37.000Z","size":7766,"stargazers_count":78,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-27T16:49:20.635Z","etag":null,"topics":["carthage","cocoapods","expiration","ios","lru-cache","memory-cache","swift","utility"],"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/yysskk.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":"2019-01-23T07:37:28.000Z","updated_at":"2025-03-18T21:38:13.000Z","dependencies_parsed_at":"2022-08-21T04:50:37.068Z","dependency_job_id":null,"html_url":"https://github.com/yysskk/MemoryCache","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/yysskk%2FMemoryCache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yysskk%2FMemoryCache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yysskk%2FMemoryCache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yysskk%2FMemoryCache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yysskk","download_url":"https://codeload.github.com/yysskk/MemoryCache/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248810883,"owners_count":21165195,"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":["carthage","cocoapods","expiration","ios","lru-cache","memory-cache","swift","utility"],"created_at":"2024-10-15T05:29:20.803Z","updated_at":"2025-04-14T02:31:32.174Z","avatar_url":"https://github.com/yysskk.png","language":"Swift","funding_links":["https://www.paypal.me/yysskk/980jpy"],"categories":[],"sub_categories":[],"readme":"# MemoryCache\n\n[![CI Status](https://img.shields.io/travis/yysskk/MemoryCache.svg?style=for-the-badge)](https://travis-ci.org/yysskk/MemoryCache)\n[![codecov](https://img.shields.io/codecov/c/github/yysskk/MemoryCache.svg?style=for-the-badge)](https://codecov.io/gh/yysskk/MemoryCache)\n[![Version](https://img.shields.io/cocoapods/v/MemoryCache.svg?style=for-the-badge)](https://cocoapods.org/pods/MemoryCache)\n[![License](https://img.shields.io/cocoapods/l/MemoryCache.svg?style=for-the-badge)](https://cocoapods.org/pods/MemoryCache)\n[![Platform](https://img.shields.io/cocoapods/p/MemoryCache.svg?style=for-the-badge)](https://cocoapods.org/pods/MemoryCache)\n\n## Overview\nMemoryCache is a memory cache class in swift. \n\n- The MemoryCache class incorporates **LRU** policies, which ensure that a cache doesn’t use too much of the system’s memory. If memory is needed by other applications, it removes some items from the cache, minimizing its memory footprint.\n- You can add, remove, and query items with **expiration** in the cache from different threads without having to lock the cache yourself. ( **thread-safe** )\n- Unlike the NSCache class, the cache guarantees a type by its key. ( **type-safe** )\n\n```swift\nlet memoryCache = MemoryCache.default // or initialize\n\n// Defining a string (or hash) key for a dog value.\nlet dogKey = StringKey\u003cDog\u003e(\"dog\")\n\n// Setting a dog value in memoryCache.\nmemoryCache.set(dog, for: dogKey)\n\n// Getting a cached dog value in memoryCache.\nlet cachedDog = try? memoryCache.value(for: dogKey)\n\n// Removing a cached dog value in memoryCache.\nmemoryCache.remove(for: dogKey)\n```\n\n## Usage\n### Basic\n####  Defining keys\n```swift\nlet dogKey = StringKey\u003cDog\u003e(\"dog\")\n```\n\n#### Adding a Cached Value\n```swift\nmemoryCache.set(dog, for: dogKey)\n```\n\n#### Getting a Cached Value\n```swift\nlet dog = try? memoryCache.value(for: dogKey)\n```\n\n#### Removing Cached Values\n- Removes the cache of the specified key.\n```swift\nmemoryCache.remove(for: dogKey)\n```\n\n- Removes the cache of the specified key if it expired.\n```swift\nmemoryCache.removeIfExpired(for: dogKey)\n```\n\n- Removes All. \n```swift\nmemoryCache.removeAll()\n```\n\n### Others\n#### Properties\n```swift\n/// The maximum total cost that the memoryCache can hold before it starts evicting caches.\nvar totalCostLimit: Int\n\n/// The maximum number of caches the memoryCache should hold.\nvar countLimit: Int\n\n/// The total cost of values in the memoryCache.\nvar totalCost: Int\n\n/// The number of values in the memoryCache.\nvar count: Int\n\n/// A Boolean value indicating whether the memoryCache has no values.\nvar isEmpty: Bool\n```\n\n#### Implement delegate\n\n```swift\nimport MemoryCache\n\nclass SomeClass: NSObject, MemoryCacheDelegate {\n\n    let memoryCache: MemoryCache\n    \n    init() {\n        memoryCache = MemoryCache.default\n        \n        ...\n        \n        super.init()\n\n        memoryCache.delegate = self\n    }\n    \n    func memoryCache(_ memoryCache: MemoryCache, willEvict cache: Any) {\n        // Called when an cache is about to be evicted or removed from the memoryCache.\n    }\n}\n```\n\n#### Expiration date\nYou can specify expiration date for cache. The default expiration is `.never`.\n\n```swift\n/// The expiration date is `.never`.\nmemoryCache.set(dog, for: dogKey, expiration: .never)\n\n/// The expiration date is `.seconds(\"\"\"10s\"\"\")`.\nmemoryCache.set(dog, for: dogKey, expiration: .seconds(10))\n\n/// The expiration date is `.date(\"\"\"TOMORROW\"\"\")`.\nmemoryCache.set(dog, for: dogKey, expiration: .date(Date().addingTimeInterval(60 * 60 * 24)))\n\n/// Remove the cache of the specified key if it expired.\nmemoryCache.removeIfExpired(for: dogKey)\n```\n\n## Requirements\n- Swift 4.2  ~\n- Xcode 10.1 ~\n\n## Installation\n### CocoaPods\n\nMemoryCache is available through [CocoaPods](https://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod 'MemoryCache'\n```\n\n### Carthage\n\nYou can integrate via [Carthage](https://github.com/carthage/carthage), too.\nAdd the following line to your `Cartfile` :\n\n```\ngithub \"yysskk/MemoryCache\"\n```\n\nand run `carthage update`\n\n## Author\n### Yusuke Morishita\n- [Github](https://github.com/yysskk)\n- [Twitter](https://twitter.com/_yysskk)\n\n[![Support via PayPal](https://cdn.rawgit.com/twolfson/paypal-github-button/1.0.0/dist/button.svg)](https://www.paypal.me/yysskk/980jpy)\n\n\n## License\n\n`MemoryCache` is available under the MIT license. See the [LICENSE](./LICENSE) file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyysskk%2Fmemorycache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyysskk%2Fmemorycache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyysskk%2Fmemorycache/lists"}