{"id":19687872,"url":"https://github.com/engali94/codablepersist","last_synced_at":"2025-04-29T08:34:21.113Z","repository":{"id":56906215,"uuid":"256839023","full_name":"engali94/CodablePersist","owner":"engali94","description":"Store and Cache Anything Codable","archived":false,"fork":false,"pushed_at":"2020-08-30T20:43:10.000Z","size":183,"stargazers_count":18,"open_issues_count":3,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-15T18:53:58.732Z","etag":null,"topics":["cache","codable","disk-cache","ios","memory-cache","storage","swift","swift-codable","swift-codable-persistance"],"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/engali94.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":"2020-04-18T19:44:38.000Z","updated_at":"2025-02-23T10:49:37.000Z","dependencies_parsed_at":"2022-08-21T02:20:53.990Z","dependency_job_id":null,"html_url":"https://github.com/engali94/CodablePersist","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engali94%2FCodablePersist","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engali94%2FCodablePersist/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engali94%2FCodablePersist/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/engali94%2FCodablePersist/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/engali94","download_url":"https://codeload.github.com/engali94/CodablePersist/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251465151,"owners_count":21593836,"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","codable","disk-cache","ios","memory-cache","storage","swift","swift-codable","swift-codable-persistance"],"created_at":"2024-11-11T18:36:22.614Z","updated_at":"2025-04-29T08:34:20.766Z","avatar_url":"https://github.com/engali94.png","language":"Swift","readme":"\n\u003cp align=\"center\"\u003e\n\n\u003cimg width=\"200\" src=\"https://github.com/engali94/CodablePersist/blob/master/Assets/CodablePersistLogo.png\" alt=\"CodablePersist Logo\"\u003e\n\n\u003c/p\u003e\n\n  \n\n\u003cp align=\"center\"\u003e\n\n\u003ca href=\"https://developer.apple.com/swift/\"\u003e\n\n\u003cimg src=\"https://img.shields.io/badge/Swift-5.0-orange.svg?style=flat\" alt=\"Swift 5.0\"\u003e\n\n\u003c/a\u003e\n\n\u003ca href=\"http://cocoapods.org/pods/CodablePersist\"\u003e\n\n\u003cimg src=\"https://img.shields.io/cocoapods/v/CodablePersist.svg?style=flat\" alt=\"Version\"\u003e\n\n\u003c/a\u003e\n\n\u003ca href=\"http://cocoapods.org/pods/CodablePersist\"\u003e\n\n\u003cimg src=\"https://img.shields.io/cocoapods/p/CodablePersist.svg?style=flat\" alt=\"Platform\"\u003e\n\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/Carthage/Carthage\"\u003e\n\n\u003cimg src=\"https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat\" alt=\"Carthage Compatible\"\u003e\n\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/apple/swift-package-manager\"\u003e\n\n\u003cimg src=\"https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg\" alt=\"SPM\"\u003e\n\n\u003c/a\u003e\n\n\u003c/p\u003e\n\n  \n\n# CodablePersist\n\n  \n\n\u003cp align=\"center\"\u003e\n\nStore your awsome `Codable` objects and retrieve them with ease. `CodablePersist` gives you a convenient way to store your objects in a disk, UserDefaluts, and memory and manage them easily.\n\n\u003c/p\u003e\n\n  \n\n## Features\n\n  \n\n- ✅ Easily save and retrieve any `Codable` type.\n- 📆 Set expiration date to the objects.\n- 🔍Time-based storage filtring.\n- 🗃Retrieve, delete, save objects using subscript\n\n## Example\n\n  \n\nTo quickly show you how `CodablePersist` can be useful, consider the following use case:\n\n```swift\nclass PostFetcher { \n    typealias Handler = (Result\u003cPost, Error\u003e) -\u003e Void \n    private let cache = DiskStorage\u003cPost\u003e(storeName: \"postStorage\")\n    \n    func fetchPost(withID id: Post.ID, then handler: @escaping  Handler) {\n    // check if the post is cached or not\n    if let cached = cache[id] { return  handler(.success(cached)) } \n    // if not cached fetch it from the backend\n    performFetching { [weak self] result in \n        // load post and cache it\n        let post = try? result.get() post.map { self?.cache[id] = $0 }\n        //then return the result\n        handler(result) \n        } \n    } \n}\n```\n\n## Installation\n\n  \n\n### \u003csummary\u003eCocoaPods\u003c/summary\u003e\n\n  \n\nCodablePersist is available through [CocoaPods](http://cocoapods.org). To install\n\nit, simply add the following line to your Podfile:\n\n  \n\n```bash\n\npod 'CodablePersist'\n\n```\n\n  \n\n### Carthage\n\n  \n\n[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.\n\n  \n\nTo integrate CodablePersist into your Xcode project using Carthage, specify it in your `Cartfile`:\n\n  \n\n```ogdl\n\ngithub \"engali94/CodablePersist\"\n\n```\n\n  \n\nRun `carthage update` to build the framework and drag the built `CodablePersist.framework` into your Xcode project.\n\n  \n\nOn your application targets’ “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase” and add the Framework path as mentioned in [Carthage Getting started Step 4, 5 and 6](https://github.com/Carthage/Carthage/blob/master/README.md#if-youre-building-for-ios-tvos-or-watchos)\n\n  \n\n### Swift Package Manager\n\n  \n\nTo integrate using Apple's [Swift Package Manager](https://swift.org/package-manager/), add the following as a dependency to your `Package.swift`:\n\n  \n\n```swift\n\ndependencies: [\n\n.package(url: \"https://github.com/engali94/CodablePersist.git\", from: \"0.1\")\n\n]\n\n```\nNext, add `CodablePersist` to your targets as follows:\n```swift\n.target(\n    name: \"YOUR_TARGET_NAME\",\n    dependencies: [\n        \"CodablePersist\",\n    ]\n),\n```\nThen run `swift package update` to install the package.\n\n\nAlternatively navigate to your Xcode project, select `Swift Packages` and click the `+` icon to search for `CodablePersist`.\n\n  \n\n### Manually\n\n  \n\nIf you prefer not to use any of the aforementioned dependency managers, you can integrate CodablePersist into your project manually. Simply drag the `Sources` Folder into your Xcode project.\n\n  \n\n## Usage\n\n  ### 1. Prepare your data  \n  Make sure your type conforms to `Identifiable` protocol, and assign a unique `idKey` property like follows:\n  ``` swift\n  struct Post: Codable, Identifiable {\n     // Identifiable conformance\n     static var idKey = \\Post.id\n    \n     var title: String\n     var id: String // should be unique per post \n}\n  ``` \n\n\n ### 2. Initialize Storage\n\n- `DiskStorage`:  We can init a Disk Storage by passing a `storeName` and  `expiryDate` \n    ``` swift\n     let storage = try? DiskStorage\u003cPost\u003e(storeName: storageName, expiryDate: .minutes(interval: 10))\n    ```\n \n- `UserDefalutsStorage`:  Also we can init UserDefaults Storage by passing a `storeName` and  `expiryDate` \n  ``` swift\n  let storage = UserDefaultsStorage\u003cPost\u003e(storeName: storageName, expiryDate: .minutes(interval: 10))! \n  ```\n  \n- `MemoryStorage`:  This should be used with precaution, if any memory load happens the system will delete some or all objects to free memory. **Consider using `DiskStorage` or   `UserDefalutsStorage` for long term persistence.** \n we can init ``\n    ```swift\n     let storage = MemoryStorage\u003cPost\u003e(expiryDate: .minutes(interval: 10))\n    ```\n    ### 3. Storage!!\n    Now you are good to go... You can persist, retrieve and delete your `Codable` objects 😎\n    #### Saving\n    ```swift\n    let singlePost = Post(title: \"I'm persistable\", id: 1)\n    let posts: [Post] = [Post(title: \"I'm persistable2\", id: 2), Post(title: \"I'm persistable3\", id: 3)]\n    \n    //Save a single object\n    try? storage.save(singlePost)\n    \n    // Save single object by subscript\n    storage[1] = singlePost\n    \n    //Save mutliple objects\n    try? storage.save(posts)\n    \n    \n    ```\n    #### Retrieval \n    ```swift\n    // fetch single object\n    let post1 = try? storage.fetchObject(for: 1)\n    \n    // fetch by subscript\n    let post2 = storage[2]\n    \n    // fetch multiple objects\n    let multiPosts = try? storage.fetchObjects(for: [3,2])\n    \n    // fetch all objects in the store sorted by date ascendingly\n    let allObjets = try? storage.fetchAllObjects(descending: false)\n    \n    // fetch only objects saved in las ten minutes \n    // ------(10)++++++(now) -\u003e will only returns ++++++\n    let obejetsAfterTenMin = try? storage.fetchObjectsStored(inLast: .minutes(interval: 10)\n    \n    // fetch object stored before the last 10 minutes\n    // ++++++(10)------(now) -\u003e will only returns ++++++\n    let obejetsBeforeTemMin = try? storage.fetchObjectsStored(before: .minutes(interval: 10))\n    \n    // check if an object exists in the storage\n    storage.contains(2)\n    \n    // check the number of objects in the storage\n    storage.objectsCount\n    \n    ```\n    #### Deletion\n    ```swift\n    // delete a single object\n    try? storage.deleteObject(forKey: singlePost.id)\n    \n    // delete a single object by subscript\n    storage[singlePost.id] = nil\n    \n    // delete multiple posts\n    try? storage.deleteObjects(forKeys: [2,3])\n    \n    // delete all objects\n    try? storage.deleteAll()\n    \n    // Delete expired objects\n    storage.deleteExpired()\n    ```\n## Requirements\n\n- iOS 8.0+ / macOS 10.10+ / tvOS 9.0+ / watchOS 2.0+\n- Xcode 10.0+\n- Swift 4.2+\n\n## Contributing\n\nContributions are warmly welcomed 🙌\n\n  \n\n## License\n\nCodablePesist is released under the MIT license. See [LICENSE](https://github.com/engali94/CodablePersist/blob/master/LICENSE) for more information.  \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengali94%2Fcodablepersist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fengali94%2Fcodablepersist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fengali94%2Fcodablepersist/lists"}