{"id":28922763,"url":"https://github.com/nashysolutions/cache","last_synced_at":"2025-10-24T13:51:56.516Z","repository":{"id":49306645,"uuid":"516948817","full_name":"nashysolutions/cache","owner":"nashysolutions","description":"A lightweight Swift library for caching Identifiable values with optional expiry, supporting both in-memory and file-backed storage. Designed for testability, composability, and use with swift-dependencies.","archived":false,"fork":false,"pushed_at":"2025-06-15T03:50:51.000Z","size":1061,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-06-22T08:05:56.655Z","etag":null,"topics":["cache","caching","codable","dependency-injection","disk-cache","expiry","file-system","in-memory-cache","swift","swift-concurrency","swift-dependencies","swift-package","testable"],"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/nashysolutions.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","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":"2022-07-23T03:36:11.000Z","updated_at":"2025-06-15T03:50:54.000Z","dependencies_parsed_at":"2022-09-15T17:13:22.821Z","dependency_job_id":null,"html_url":"https://github.com/nashysolutions/cache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/nashysolutions/cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashysolutions%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashysolutions%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashysolutions%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashysolutions%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nashysolutions","download_url":"https://codeload.github.com/nashysolutions/cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashysolutions%2Fcache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261257105,"owners_count":23131520,"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","caching","codable","dependency-injection","disk-cache","expiry","file-system","in-memory-cache","swift","swift-concurrency","swift-dependencies","swift-package","testable"],"created_at":"2025-06-22T08:05:57.642Z","updated_at":"2025-10-24T13:51:56.511Z","avatar_url":"https://github.com/nashysolutions.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cache\n\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fnashysolutions%2Fcache%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/nashysolutions/cache)\n[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fnashysolutions%2Fcache%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/nashysolutions/cache)\n\n**Cache** is a Swift library for caching `Identifiable` values with optional expiry logic. It supports both **in-memory** and **file-backed** storage, making it suitable for short-lived data, offline persistence, or resource caching.\n\n---\n\n## Features\n\n- ✅ Type-safe caching for any `Identifiable` type\n- 📦 Two interchangeable storage implementations:\n  - `VolatileCache`: fast in-memory storage\n  - `FileSystemCache`: persistent, file-backed storage\n- 💡 Expiry support: `.short` or `.custom(Date)`\n- 🧪 Testable without delays (no need for `sleep`)\n- 🕹 Native async/await support. Fully thread safe and sendable.\n- 🧩 Easily injectable via `swift-dependencies`\n\n---\n\n## Usage\n\nSee the QuickStart guide in the `Documentation.docc` catalogue.\n\n### Example\n\n```swift\nstruct Cheese: Identifiable {\n    let id: Int\n    let name: String\n}\n\nlet cache = VolatileCache\u003cCheese\u003e()\ntry await cache.stash(Cheese(id: 0, name: \"Brie\"), duration: .short)\n```\n\n---\n\n### Dependency Injection\n\nAn example using [`swift-dependencies`](https://github.com/pointfreeco/swift-dependencies).\n\n```swift\nimport Dependencies\nimport Cache\n\nextension DependencyValues {\n\n    /// A cache for storing and retrieving `Cheese` models.\n    var cheeseCache: any Cache\u003cCheese\u003e {\n        get { self[CheeseCacheKey.self] }\n        set { self[CheeseCacheKey.self] = newValue }\n    }\n}\n\nprivate enum CheeseCacheKey: DependencyKey {\n    static let liveValue: any Cache\u003cCheese\u003e = FileSystemCache(.caches, subfolder: \"Cheeses\")\n}\n```\n\nThen use it like this:\n\n```swift\nstruct MyModel {\n\n    @Dependency(\\.cheeseCache) var cheeseCache\n\n    fucn loadCheese(id: Int) throws -\u003e Cheese? {\n        try await cheeseCache.resource(for: id)\n    }\n}\n\n#Preview {\n    withDependencies {\n        $0.cheeseCache = MockCache()\n    } operation: {\n        ContentView()\n    }\n\n}\n\nprivate actor MockCache: Cache {\n    \n    private var store: [Int: Cheese] = [:]\n\n    func stash(_ item: Cheese, duration: Expiry) async throws {\n        // Ignore `duration` for now; just stash in memory\n        store[item.id] = item\n    }\n\n    func removeResource(for identifier: Int) async throws {\n        store.removeValue(forKey: identifier)\n    }\n\n    func resource(for identifier: Int) async throws -\u003e Cheese? {\n        store[identifier]\n    }\n\n    func reset() async throws {\n        store.removeAll()\n    }\n}\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnashysolutions%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnashysolutions%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnashysolutions%2Fcache/lists"}